请大家帮我改一下!

zhangpingfly 2006-12-22 03:45:48
题目是实现二叉树的每个结点的祖先
我的这个程序只有输入最左边的结点才会有真确的答案的
#include<iostream.h>
struct bitree
{
int data;
bitree *lchild,*rchild;
};
bitree *createbitree()//构造树
{
bitree *q;
int ch;
cout<<"请输入数据:";
cin>>ch;
if(ch==0)q=NULL;
else
{
q=new(struct bitree);
q->data=ch;
q->lchild=createbitree();
q->rchild=createbitree();
}
return q;
}
void Search(bitree *bt,int x)
{
typedef struct
{
bitree *t;
int tag;
}stack;
stack s[100];
int top,i;
while(bt!=NULL||top>0)
{
top=0;
while(bt!=NULL&&bt->data!=x)
{
s[++top].t=bt;
s[top].tag=0;
bt=bt->lchild;
}
if(bt->data==x)
{
cout<<"所查结点的所有祖先结点的值为:";
for(i=1;i<=top;i++)
cout<<s[i].t->data<<" ";
return;
}
while(top!=0&&s[top].tag==1)top--;
if(top!=0)
{
s[top].tag=1;
bt=s[top].t->rchild;
}
}
}
void main()
{
bitree *bt;
int x;
bt=createbitree();
cout<<"请输入结点x:";
cin>>x;
Search(bt,x);
}
...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangpingfly 2006-12-25
  • 打赏
  • 举报
回复
kingbo2006(韫知)你的这个程序也也有错误的, 比如说输入124005003600700输出 126
cjq87 2006-12-25
  • 打赏
  • 举报
回复
//楼主的程序只有两个小错误,改正后测试是可以的
#include<iostream.h>
struct bitree
{
int data;
bitree *lchild,*rchild;
};
bitree *createbitree()//构造树
{
bitree *q;
int ch;
cout<<"请输入数据:";
cin>>ch;
if(ch==0)q=NULL;
else
{
q=new(struct bitree);
q->data=ch;
q->lchild=createbitree();
q->rchild=createbitree();
}
return q;
}
void Search(bitree *bt,int x)
{
typedef struct
{
bitree *t;
int tag;
}stack;
stack s[100];
int top,i;
top=0; //top的清零应该再整个循环外
while(bt!=NULL||top>0)
{
while(bt!=NULL&&bt->data!=x)
{
s[++top].t=bt;
s[top].tag=0;
bt=bt->lchild;
}
if(bt)    //用bt来判断 而不用bt->data bt可能是null
{
cout<<"所查结点的所有祖先结点的值为:";
for(i=1;i<=top;i++)
cout<<s[i].t->data<<" ";
return;
}
while(top!=0&&s[top].tag==1)top--;
if(top!=0)
{
s[top].tag=1;
bt=s[top].t->rchild;
}
}
}
void main()
{
bitree *bt;
int x;
bt=createbitree();
cout<<"请输入结点x:";
cin>>x;
Search(bt,x);
}
cjq87 2006-12-23
  • 打赏
  • 举报
回复
你用了栈为什么还用tag 做标记
我觉得只用栈就可以了
kingbo2006 2006-12-23
  • 打赏
  • 举报
回复
经过我仔细考细,现将程序修改如下:(所有情况都通过测试)
#include<iostream.h>
struct bitree
{
int data;
bitree *lchild,*rchild;
};
bitree *createbitree()//构造树
{
bitree *q;
int ch;
cout<<"请输入数据:";
cin>>ch;
if(ch==0)
q=NULL;
else
{
q=new(struct bitree);
q->data=ch;
q->lchild=createbitree();
q->rchild=createbitree();
}
return q;
}
void Search(bitree *bt,int x)
{
typedef struct
{
bitree *t;
int tag;
}
stack;
stack s[100];
int top=0,i;
if(bt!=NULL)
{
s[++top].t=bt;
s[top].tag=0;
}
while(top>0)
{
while(s[top].t!=NULL&&s[top].tag==0)
{
bt=s[top].t;
cout<<bt->data<<endl;
cout<<"top="<<top<<endl;
if(bt->data!=x)
{
s[++top].t=bt->lchild;
s[top].tag=0;

}
else
{
cout<<"top="<<top<<endl;
break;
}
}
if(bt->data=x)
break;
else
{
if(s[--top].tag==0)
{
bt=s[top].t;
s[top].tag=1;
s[++top].t=bt->rchild;
}
else
--top;
}
}
if(top>1)
{
cout<<"祖先是:";
for(int i=1;i<top;i++)
cout<<s[i].t->data<<" ";
cout<<endl;
}
else
cout<<"没有祖先或数据不存在";
}
int main()
{
bitree *bt;
int x;
bt=createbitree();
cout<<"请输入结点x:";
cin>>x;
Search(bt,x);
return 0;
}

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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