大家帮我看一下我的查找树程序
下面是我编的查找树,但打印时只输出根节点,不知是为什么,请大家帮忙看一下
#include<iostream.h>
#define NULL 0
typedef struct Tree{
int info;
Tree *left;
Tree *right;
}Tnode;
Tnode* init(Tnode *T){ /*初始根节点*/
T = new Tnode;
int i;
cout<<"please input the value (end when put '#'):"<<endl;
cin>>i;
T->info = i;
T->left = T->right = NULL;
return T;
}
Tnode* insert(Tnode *T, int value){ /*根据大小插入节点, 错误应该就在这里*/
Tnode *p = T;
while(p != NULL){
if(value >= p->info )
p = p->right;
else p = p->left;
}
Tnode *q = new Tnode;
q->info = value;
q->left = q->right = NULL;
p = q;
return T;
}
int sort( Tnode *T, int target){ /*输入一个数在查找树中查找,这跟此程序的错误无关*/
Tnode *p = T;
while(p != NULL ){
if(target > p->info)
p = p->right;
else if(target < p->info)
p = p->left;
else return p->info;
}
cout<<"the num is not in"<<endl;
return 0;
}
void Tshow( Tnode *T){ /*中序打印出来*/
if(T == NULL)return ;
Tshow(T->left);
cout<<T->info<<endl;
Tshow(T->right);
}
void main()
{
Tnode *T_sort = 0;
T_sort =init(T_sort);
int c;
cin>>c;
while(c != 0){/*建立查找树*/
T_sort = insert(T_sort, c);
cin>>c;
}
cout<<"do you want to see the tree( y or n)"<<endl;
char cc;
cin>>cc;
if(cc == 'y'){
Tnode *p = T_sort;
Tshow(p);
}
cout<<"input the num you want to sort:"<<endl;
int i;
cin>>i;
cout<<sort(T_sort, i);
}
自己实在看不出错在哪里,所以麻烦大家看一下,指点迷津,不甚感激!!