大家帮我看一下我的查找树程序

zjlxysunman 2005-03-23 05:14:23
下面是我编的查找树,但打印时只输出根节点,不知是为什么,请大家帮忙看一下
#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);
}

自己实在看不出错在哪里,所以麻烦大家看一下,指点迷津,不甚感激!!
...全文
71 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicknide 2005-03-23
  • 打赏
  • 举报
回复
cout<<"do you want to see the tree( y or n):"<<endl;
cin>>*(char*)&c;

char cc;
cin>>cc;

这个地方有问题啦,我做实验的时候留下的,发的时候忘了删除上面的了
zjlxysunman 2005-03-23
  • 打赏
  • 举报
回复
听君一习话,胜读十年书啊;
不但帮我解决的问题,还教了我这么多的技巧,
让我受益匪浅啊!!!
谢谢了!!
nicknide 2005-03-23
  • 打赏
  • 举报
回复

#include<iostream>

using namespace std;

typedef
struct Tree
{
Tree(int v=0,Tree* l=0,Tree* r=0):info(v),left(l),right(r){}
int info;
Tree *left;
Tree *right;
}Tnode;

/*
Tnode*
init(int data) // 给的参数是误导,修改
{ /*初始根节点 : 你有什么需要特别处理的?
// 说明与功能脱节上的纰漏,这个函数分明只插入一次数据
//cout<<"please input the value (end when put '#'):"<<endl;
return new Tnode(data);
// 而且,后面的insert函数可以执行初始化功能,这个东西其实可以去掉
}
*/

Tnode*
insert(Tnode *node, int value)
{
if ( !node )
{
return new Tnode(value);
}

Tnode *p = node; // 原始的这个函数,没有将新节点插入到老的树中,错误
Tnode* b=0; // 跟踪最后元素

while(p != 0)
if(value >= p->info )
b = p, p = p->right;
else
b = p, p = p->left;

if ( value >= b->info )
b->right = new Tnode(value);
else
b->left = new Tnode(value);

return node;
}


Tnode*
Find( Tnode *node, int target)
{ /*输入一个数在查找树中查找,这跟此程序的错误无关*/
// 说明,不要武断的断定某个地方没有错误,要用怀疑的眼光看自己的程序
// 而且这个查找,从逻辑上来说也是没有意义的,不能用sort这个名字
Tnode *p = node;

while(p)
{
if(target > p->info)
p = p->right;
else if(target < p->info)
p = p->left;
else return p;
}

// 从设计上来说,底层函数尽量少露出尾巴
// cout<<"the num is not in"<<endl;
return 0;
}


void
Tshow( Tnode *node)
{ /*中序打印出来*/
if(node == 0)return ;
Tshow(node->left);
cout<<node->info<<endl;
Tshow(node->right);

}


int
main()
{

cout<<"please input the value (end when put '0'):"<<endl;

int c;
cin>>c;
if (!c)
return 0;

// 还是刚才那个问题,定义数据不要太急
Tree *tree = 0;
// 注意 do while ; while ,哪个更加合适
do
{/*建立查找树*/
tree = insert(tree, c);
cin>>c;
}while(c);

cout<<"do you want to see the tree( y or n):"<<endl;
cin>>*(char*)&c;

char cc;
cin>>cc;
if((char)cc == 'y')
{
Tnode *p = tree;
Tshow(p);
}

// 这个地方要保持一致,不是sort, 而是Find
cout<<"input the num you want to Find:"<<endl;
cin>>c;
Tnode* temp = Find(tree, c);
if (temp)
cout<<temp->info<<endl;
else
cout<<"Not Find:"<<c<<"here"<<endl;

return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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