二叉排序树的建立问题

Ver 2011-12-20 11:45:27
请大家帮我看看,为什么在这个程序中bt无法建立,而且insertBST函数指针无法回传,可能问题很简单,但我要弄明白为什么。
源代码如下:
#define N 100
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int key;
struct node *lch,*rch;
}BSTnode;

int insertBST(BSTnode *p,int k)
{
if(p==NULL)
{
p=(BSTnode *)malloc(sizeof(BSTnode));printf(" %d ",p);
p->key=k;
p->lch=p->rch=NULL;
return 1;
}
else if(k==p->key)
return 0;
else if(k<p->key)
return insertBST(p->lch,k);
else
return insertBST(p->rch,k);
}

BSTnode *creatBST(int a[],int n)
{
int i=0;
BSTnode *bt;bt=(BSTnode*)malloc(sizeof(BSTnode));
/* while(i<n)
{
insertBST(bt,a[i]);
i++;printf(" %d ",bt);
}*/
return bt;
}

BSTnode *search(BSTnode *q,int k)
{
if(q==NULL||q->key==k)
return q;
if(k<q->key)
return search(q->lch,k);
else
return search(q->rch,k);
}

void inorder(BSTnode *bt)
{
if(bt!=NULL)
{
inorder(bt->lch);
printf("**%d**",bt->key);
inorder(bt->rch);
}
}

main()
{
int i,n=3,k,a[]={3,1,2};
// printf("input n:");
// scanf("%d",&n);
for(i=0;i<n;i++)
{
// a[i]=rand()%100;
printf("%5d",a[i]);
}


inorder(creatBST(a,n));
printf("input key k:");
scanf("%d",&k);
// bt=search(bt,k);
}
...全文
393 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ver 2011-12-25
  • 打赏
  • 举报
回复
我把return insertBST((*p)->lch,k); //修改10
else
return insertBST((*p)->rch,k); //修改11
改为:return insertBST(&(*p)->lch,k); //修改10
else
return insertBST(&(*p)->rch,k); //修改11
就能运行了,谢谢各位啊!
seucs 2011-12-21
  • 打赏
  • 举报
回复
需要二级指针才能将修改后的指针带出函数,你自己对照注释看我给你改了哪些地方

#define N 100
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int key;
struct node *lch,*rch;
}BSTnode;

int insertBST(BSTnode **p,int k) //修改1
{
if(*p==NULL) //修改2
{
*p=(BSTnode *)malloc(sizeof(BSTnode));printf(" %d ",*p); //修改3、4
(*p)->key=k; //修改5
(*p)->lch=(*p)->rch=NULL; //修改6、7
return 1;
}
else if(k==(*p)->key) //修改8
return 0;
else if(k<(*p)->key) //修改9
return insertBST((*p)->lch,k); //修改10
else
return insertBST((*p)->rch,k); //修改11
}

BSTnode *creatBST(int a[],int n)
{
int i=0;
BSTnode *bt;bt=(BSTnode*)malloc(sizeof(BSTnode));
/* while(i<n)
{
insertBST(&bt,a[i]); //修改12
i++;printf(" %d ",bt);
}*/
return bt;
}

BSTnode *search(BSTnode *q,int k)
{
if(q==NULL||q->key==k)
return q;
if(k<q->key)
return search(q->lch,k);
else
return search(q->rch,k);
}

void inorder(BSTnode *bt)
{
if(bt!=NULL)
{
inorder(bt->lch);
printf("**%d**",bt->key);
inorder(bt->rch);
}
}

main()
{
int i,n=3,k,a[]={3,1,2};
// printf("input n:");
// scanf("%d",&n);
for(i=0;i<n;i++)
{
// a[i]=rand()%100;
printf("%5d",a[i]);
}


inorder(creatBST(a,n));
printf("input key k:");
scanf("%d",&k);
// bt=search(bt,k);
}
Ver 2011-12-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 seucs 的回复:]

需要二级指针才能将修改后的指针带出函数,你自己对照注释看我给你改了哪些地方
C/C++ code

#define N 100
#include"stdio.h"
#include"stdlib.h"
typedef struct node
{
int key;
struct node *lch,*rch;
}BSTnode;

int insertBST(BSTnode **p,i……
[/Quote]呵呵……
原来是这样啊,我是照着书上打出来的,书上也没提过二级指针
我运行了一下,又出现下面的问题了:
22:return insertBST((*p)->lch,k); //修改后
24:return insertBST((*p)->rch,k); //修改后
bst.c(22)warning C4047:'function' : 'struct node **' differs in levels of indirection from 'struct node *'
bst.c(22)warning C4024:'insertBST' : 'different types for formal and actual parameter 1
bst.c(24)warning C4047:'function' : 'struct node **' differs in levels of indirection from 'struct node *'
bst.c(24)warning C4024:'insertBST' : 'different types for formal and actual parameter 1
不知道该如何解决,还请帮忙再看看,万胜感激!!!
Ver 2011-12-21
  • 打赏
  • 举报
回复
呵呵……
原来是这样啊,我是照着书上打出来的,书上也没提过二级指针
我运行了一下,又出现下面的问题了:
22:return insertBST((*p)->lch,k); //修改后
24:return insertBST((*p)->rch,k); //修改后
bst.c(22)warning C4047:'function' : 'struct node **' differs in levels of indirection from 'struct node *'
bst.c(22)warning C4024:'insertBST' : 'different types for formal and actual parameter 1
bst.c(24)warning C4047:'function' : 'struct node **' differs in levels of indirection from 'struct node *'
bst.c(24)warning C4024:'insertBST' : 'different types for formal and actual parameter 1
不知道该如何解决,还请帮忙再看看,万胜感激!!!

70,022

社区成员

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

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