二叉排序树 问题求指教~

lianglevel 2012-05-23 09:41:16
问题:中序遍历无法实现,以及二叉检索树的检索(查找)一直出现未找到该元素

#include<stdio.h>
#include<stdlib.h>
typedef int keytype;
/*二叉排序表的存储结构的描述*/
typedef struct node
{
keytype key;
struct node *lchild,*rchild;
}bstnode;
typedef bstnode * bstlist;
/*二叉检索树的检索(查找)*/
int bstsearch(bstlist bt,keytype k)
{
if (bt==NULL)
return 0;
else if(bt->key==k)
return 1;
else if(bt->key<k)
return bstsearch(bt->rchild,k);
else
return bstsearch(bt->lchild,k);
}
/*二叉检索树的插入*/
int bstinsert(bstlist bt,keytype k)
{
if(bt==NULL)
{
bt=(bstlist)malloc(sizeof(bstnode));
bt->key=k;
bt->lchild=bt->rchild=NULL;
return 1;
}
else if(bt->key==k)
return 0;
else if(bt->key<k)
return bstinsert(bt->rchild,k);
else
return bstinsert(bt->lchild,k);
}
/*构造二叉排序树*/
bstlist createbst(bstlist bt,int n)
{
int i;
int key;
bt=NULL;
for(i=0;i<n;i++)
{
printf("请输入二叉排序树的元素:");
scanf("%d",&key);
bstinsert(bt,key);
}
return bt;
}
/*中序遍历*/
void inorder(bstlist bt)
{
if(bt)
{
inorder(bt->lchild); // 遍历左子树
printf("%d ",bt->key); // 访问结点
inorder(bt->rchild); // 遍历右子树
}
}
/*菜单栏*/
void menu()
{
printf("\n***************************\n");
printf("[1]新建二叉排序树 \n");
printf("[2]二叉检索树的检索 \n");
printf("[3]二叉检索树的插入 \n");
printf("[4]遍历整个二叉排序树 \n");
printf("[0]退出 \n");
printf("\n***************************\n");
printf("请输入命令:\n");
}
/*主函数*/
void main()
{
bstlist bt=(bstnode *)malloc(sizeof(bstnode));
int k; //元素
int flag; //标志
int order; //命令
//char ans; //(Y/N)
int n;
while(1)
{
menu();
//ans='n';
scanf("%d",&order);
switch(order)
{
case 1: //新建二叉排序树
printf("请输入二叉排序树的个数:");
scanf("%d",&n);
fflush(stdin); //清空键盘缓冲区(过滤回车)
bt=createbst(bt,n);
break;
case 2: //二叉检索树的检索
printf("请输入要检索的元素:");
scanf("%d",&k);
fflush(stdin); //清空键盘缓冲区(过滤回车)
flag=bstsearch(bt,k);
if(flag) printf("找到该元素!");
else printf("未找到该元素!");
break;
case 3: //二叉检索树的插入
printf("请输入要插入的元素:");
scanf("%d",&k);
fflush(stdin); //清空键盘缓冲区(过滤回车)
flag=bstinsert(bt,k);
if(flag!=0) printf("插入成功!");
else printf("插入不成功!");
break;
case 4: //遍历整个二叉排序树
inorder(bt);
break;
case 0:
exit(0);
default:
printf("您输入的命令有误!\n");
}
}
}
...全文
69 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lianglevel 2012-05-23
  • 打赏
  • 举报
回复
发现真的是指针,问题,该下了。只能显示最后一个输入的~其他函数无问题~
#include<stdio.h>
#include<stdlib.h>
typedef int keytype;
/*二叉排序表的存储结构的描述*/
typedef struct node
{
keytype key;
struct node *lchild,*rchild;
}bstnode;
typedef bstnode * bstlist;
/*二叉检索树的检索(查找)*/
int bstsearch(bstlist &bt,keytype k)
{
if (bt==NULL)
return 0;
else if(bt->key==k)
return 1;
else if(bt->key<k)
return bstsearch(bt->rchild,k);
else
return bstsearch(bt->lchild,k);
}
/*二叉检索树的插入*/
int bstinsert(bstlist &bt,keytype k)
{
if(bt==NULL)
{
bt=(bstlist)malloc(sizeof(bstnode));
bt->key=k;
bt->lchild=bt->rchild=NULL;
return 1;
}
else if(bt->key==k)
return 0;
else if(bt->key<k)
return bstinsert(bt->rchild,k);
else
return bstinsert(bt->lchild,k);
}
/*构造二叉排序树*/
void createbst(bstlist &bt,int k)
{
bt=NULL;
if(bt==NULL)
{
bt=(bstlist)malloc(sizeof(bstnode));
bt->key=k;
bt->lchild=NULL;
bt->rchild=NULL;
}
else if(k<bt->key)
createbst(bt->lchild,k);
else
createbst(bt->rchild,k);
//bstinsert(bt,key);
}
/*中序遍历*/
void inorder(bstlist &bt)
{
if(bt)
{
inorder(bt->lchild); // 遍历左子树
printf("%d ",bt->key); // 访问结点
inorder(bt->rchild); // 遍历右子树
}
}
/*菜单栏*/
void menu()
{
printf("\n***************************\n");
printf("[1]新建二叉排序树 \n");
printf("[2]二叉检索树的检索 \n");
printf("[3]二叉检索树的插入 \n");
printf("[4]遍历整个二叉排序树 \n");
printf("[0]退出 \n");
printf("\n***************************\n");
printf("请输入命令:\n");
}
/*主函数*/
void main()
{
bstlist bt=(bstnode *)malloc(sizeof(bstnode));
int k; //元素
int flag; //标志
int order; //命令
int n;
while(1)
{
menu();
scanf("%d",&order);
switch(order)
{
case 1: //新建二叉排序树
while(1)
{
printf("请输入二叉排序树的元素:");
scanf("%d",&k);
if(k==-1) break;
createbst(bt,k);
}
break;
case 2: //二叉检索树的检索
printf("请输入要检索的元素:");
scanf("%d",&k);
fflush(stdin); //清空键盘缓冲区(过滤回车)
flag=bstsearch(bt,k);
if(flag) printf("找到该元素!");
else printf("未找到该元素!");
break;
case 3: //二叉检索树的插入
printf("请输入要插入的元素:");
scanf("%d",&k);
fflush(stdin); //清空键盘缓冲区(过滤回车)
flag=bstinsert(bt,k);
if(flag!=0) printf("插入成功!");
else printf("插入不成功!");
break;
case 4: //遍历整个二叉排序树
inorder(bt);
break;
case 0:
exit(0);
default:
printf("您输入的命令有误!\n");
}
}
}
lianglevel 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

int bstinsert(bstlist bt,keytype k)
需要传递指针的指针来做。否则你在局部函数内无法修改指针的内存的。
int bstinsert(bstlist* bt,keytype k)
然后依次修改你的bitinsert
[/Quote]

这样声明不可以么》typedef bstnode * bstlist;
lianglevel 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

int bstinsert(bstlist bt,keytype k)
需要传递指针的指针来做。否则你在局部函数内无法修改指针的内存的。
int bstinsert(bstlist* bt,keytype k)
然后依次修改你的bitinsert
[/Quote]

这样声明不可以么》typedef bstnode * bstlist;
W170532934 2012-05-23
  • 打赏
  • 举报
回复
int bstinsert(bstlist bt,keytype k)
需要传递指针的指针来做。否则你在局部函数内无法修改指针的内存的。
int bstinsert(bstlist* bt,keytype k)
然后依次修改你的bitinsert

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧