二叉树的建立,查找,删除,遍历
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#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;i { printf("Please input the number"); scanf("%d",&x); if(tree==null) { p=q=(struct node*)malloc(sizeof(struct node)); p->data=x; p->rlink=null; p->llink=null; tree=p; } else { p=tree; while(p!=null) { q=p; if(x p=p->llink; else p=p->rlink; } p=(struct node*)malloc(sizeof(struct node)); p->data=x; p->rlink=null; p->llink=null;