主题:二叉排序树的问题
此程序在插入结点时有问题,大家麻烦看看啊!
#include "stdio.h"
struct bitree
{int data;
struct bitree *left;
struct bitree *right;
};
void search (struct bitree *b,int x)
{if(b==NULL)
printf("can not find this elem!\n");
else
{if(b->data==x) printf("this elem exists in this tree!\n");
if(x<b->data) search(b->left,x);
else search(b->right,x);
}
}
void insert(struct bitree *b,struct bitree *s)
{if(b==NULL) b=s;
else if(s->data<b->data) insert(b->left,s);
else if(s->data>b->data||s->data==b->data)
insert(b->right,s);
}
void create(struct bitree *b)
{int x,n,i;
struct bitree *s;b=NULL;
printf("please input the num of elem:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{scanf("%d",&x);
s=(struct bitree *)malloc(sizeof(struct bitree));
s->data=x;s->left=NULL;s->right=NULL;
insert(b,s);}
}
void postorder (struct bitree *b)
{if(!b) return;
else
{postorder(b->left);
postorder(b->right);
printf("%2d",b->data);
}
}
main()
{struct bitree *b;
b=(struct bitree *)malloc(sizeof(struct bitree));
create(b);
postorder(b);
search(b,10);
}
#include "stdio.h"
struct bitree
{int data;
struct bitree *left;
struct bitree *right;
};
void search (struct bitree *b,int x)
{if(b==NULL)
printf("can not find this elem!\n");
else
{if(b->data==x) printf("this elem exists in this tree!\n");
if(x<b->data) search(b->left,x);
else search(b->right,x);
}
}
void insert(struct bitree *b,struct bitree *s)
{if(b==NULL) b=s;
else if(s->data<b->data) insert(b->left,s);
else if(s->data>b->data||s->data==b->data)
insert(b->right,s);
}
void create(struct bitree *b)
{int x,n,i;
struct bitree *s;b=NULL;
printf("please input the num of elem:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{scanf("%d",&x);
s=(struct bitree *)malloc(sizeof(struct bitree));
s->data=x;s->left=NULL;s->right=NULL;
insert(b,s);}
}
void postorder (struct bitree *b)
{if(!b) return;
else
{postorder(b->left);
postorder(b->right);
printf("%2d",b->data);
}
}
main()
{struct bitree *b;
b=(struct bitree *)malloc(sizeof(struct bitree));
create(b);
postorder(b);
search(b,10);
}