为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > 二叉树的建立,查找,删除,遍历(精)

二叉树的建立,查找,删除,遍历(精)

2017-11-23 13页 doc 27KB 19阅读

用户头像

is_751406

暂无简介

举报
二叉树的建立,查找,删除,遍历(精)二叉树的建立,查找,删除,遍历(精) #include #include # define max 100 # define null 0 struct node *inserttree(struct node *tree); struct node *findtree(struct node *tree, int x); void correct(struct node *findi); struct node * deltree(struct node *tree); void preorder(struc...
二叉树的建立,查找,删除,遍历(精)
二叉树的建立,查找,删除,遍历(精) #include #include # define max 100 # define null 0 struct node *inserttree(struct node *tree); struct node *findtree(struct node *tree, int x); void correct(struct node *findi); struct node * deltree(struct node *tree); void preorder(struct node *tree); void midorder(struct node *tree); void postorder(struct node *tree); struct node { int data; struct node *llink; struct node *rlink; }; int main() { int choose; struct node *tree, *findi; int flag, x; flag=1; tree=null; do { printf("*************************\n"); printf("Please choose your choice\n1----------creattree\n2----------findtree\n3---------- correct\n4----------deltree\n"); printf("5----------preorder\n6----------midorder\n7----------postorder\n"); printf("*************************\n"); scanf("%d",&choose); switch(choose) { case 1:tree=inserttree(tree); break; case 2:printf("Please input the number you want find!\n"); scanf("%d",&x); findi=findtree(tree,x); if(findi==null) printf("There is not a number in dinary tree \n"); else printf("The number you want to find=%d\n",findi->data); break; case 3:printf("Please input the number you want to replace:"); scanf("%d",&x); findi=findtree(tree,x); correct(findi); break; case 4:tree=deltree(tree); break; case 5: printf("priororder:\n"); preorder(tree); break; case 6: printf("midororder:\n"); midorder(tree); break; case 7: printf("postororder:\n"); postorder(tree); break; default: printf("error\n"); } printf("Do you want to contiue\n1----------YES\n0----------NO\n"); scanf("%d",&flag); }while(flag); return (0); } struct node* inserttree(struct node *tree) { struct node *p,*q; int x, i, n; printf("Please input the n!\n"); scanf("%d",&n); for(i=0;idata=x; p->rlink=null; p->llink=null; tree=p; } else { p=tree; while(p!=null) { q=p; if(xdata) p=p->llink; else p=p->rlink; } p=(struct node*)malloc(sizeof(struct node)); p->data=x; p->rlink=null; p->llink=null; if(xdata) q->llink=p; else q->rlink=p; } } return (tree); } struct node *findtree(struct node *tree, int x) { struct node *p, *q; int flag; flag=0; p=tree; while((p!=null)&&(!flag)) { if(p->data==x) flag=1; else if( xdata) p=p->llink; else p=p->rlink; } if(flag) q=p; else q=null; return(q); } void correct(struct node *findi) { int x; printf("Please input the number repalce the find-number:"); scanf("%d",&x); findi->data=x; } struct node * deltree(struct node *tree) { struct node *q, *s, *p,*f; int flag, flag2; int x; flag=flag2=0; p=tree; printf("Please input the number you want to delete:"); scanf("%d",&x); while((p!=null)&&(!flag2)) { if(p->data==x) flag2=1; else if( xdata) { f=p; p=p->llink; } else { f=p; p=p->rlink; } } if(flag2) { if((p->llink==null)||(p->rlink==null)) { if(p->llink==null) { if(p==tree) tree=p->rlink; else { s=p->rlink; flag=1; } } else { if(p==tree) tree=p->llink; else { s=p->llink; flag=1; } } } else { q=p; s=q->rlink; while(s->llink!=null) { q=s; s=s->llink; } s->llink=p->llink; if(q!=p) { q->llink=s->rlink; s->rlink=p->rlink; } if(q==p) tree=s; else flag=1; } if(flag) { if(p==f->llink) f->llink=s; else f->rlink=s; } free(p); } else printf("Don't have the number!\n"); return tree; } void preorder(struct node *tree) { struct node *p; struct node *s[max]; int top; top=-1; p=tree; do { while(p!=null) { printf("%4d",p->data); if(p->rlink!=null) { top=top+1; s[top]=p->rlink; } p=p->llink; } if(top!=-1) { p=s[top]; top=top-1; } }while((p!=null)||(top!=-1)); } void midorder(struct node *t) { struct node *p; struct node *s[max]; int top; top=-1; p=t; do { while (p!=null) /*扫描左节点 */ { top=top+1; s[top]=p; p=p->llink; } if ( top>=0) { p=s[top]; /* p所指的节点为无左子树或其左子树已经遍历过*/ top=top-1; printf("%4d",p->data); /*访问节点*/ p=p->rlink ; /*扫描右子树*/ } }while((p!=null)||(top!=-1)); } void postorder(struct node *t) { struct node *p,*pre;//pre保存刚访问过节点 struct node *s[max]; int top=-1; p=t; pre=null; while(p||top>-1) { //沿着左子树走到最底端 while(p!=null) { top=top+1; s[top]=p; p=p->llink; } p=s[top]; if((p->rlink==null)||(p->rlink==pre)) { //如果没有右子树或者右子树刚被访问过 printf("%4d",p->data); top=top-1; pre=p; p=null; } else p=p->rlink; } }
/
本文档为【二叉树的建立,查找,删除,遍历(精)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索