查找一个二叉树的所有祖先问题

zhzh89 2009-05-02 05:09:42
/*调不出来*/
#define MAXSIZE 256
#include<stdio.h>
typedef struct bnode{
char data;
struct bnode *lchild,*rchild;
}Bnode,*BTree;/*定义二叉树*/
typedef struct{
BTree node;
int flag;
}DataType;
typedef struct{
DataType data[MAXSIZE];
int top;
}SeqStack,*PSeqStack;/*栈的定义*/
PSeqStack Init_SeqStack(void)
{
PSeqStack S;
S=(PSeqStack)malloc(sizeof(SeqStack));
if(S)
S->top=-1;
return S;
}
int Empty_SeqStack(PSeqStack S)
{
if(S->top==-1)
return 1;
else return 0;
}
int Push_SeqStack(PSeqStack S,DataType x)/*入栈*/
{
if(S->top==MAXSIZE-1)
return 0;
else
{
S->top++;
S->data[S->top]=x;
return 1;
}
}
int Pop_SeqStack(PSeqStack S,DataType *x)/*出栈*/
{
if(Empty_SeqStack(S))
return 0;
else
{
*x=S->data[S->top];
S->top--;
return 1;
}
}
BTree CreatTree(void)/*建立二叉树*/
{
BTree t;
char ch;
ch=getchar();
if(ch==' ') t=NULL;
else
{
t=(BTree)malloc(sizeof(Bnode));
t->data=ch;
t->lchild=CreatTree();
t->rchild=CreatTree();
}
return t;
}
void PostOrder(BTree t,char x)/*查找X的祖先*/
{
PSeqStack S;
DataType Sq;
BTree p=t;
int found=0;
S=Init_SeqStack();
while(p||!Empty_SeqStack(S))
{
if(p)
{
Sq.flag=0;
Sq.node=p;
Push_SeqStack(S,Sq);
p=p->lchild;
}
else
{
Pop_SeqStack(S,&Sq);
p=Sq.node;
if(Sq.flag==0)
{
Sq.flag=1;
Push_SeqStack(S,Sq);/*再次入栈*/
p=p->rchild;
}
else
{
if(p->data==x)/*找到x*/
{
found=1;
break;
}
}
}
}
if(found)
{
while(!Empty_SeqStack(S))
{
Pop_SeqStack(S,&Sq);
printf("%c",(Sq.node)->data);
}
}
else
printf("The value is not found in the tree!\n");
}
int main()
{
char x;
BTree t;
printf("input a tree:\nexample:ab c \n");
t=CreatTree();
printf("input your seaching element:\n");
scanf("%c",&x);
PostOrder(t,x);
getch();
}
...全文
339 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq675927952 2009-05-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hairetz 的回复:]
每个节点加个parent节点成员就是了。
[/Quote]

up
kobemadi 2009-05-03
  • 打赏
  • 举报
回复
学习了
起点 2009-05-03
  • 打赏
  • 举报
回复
来学习!!
ChinaRocLee 2009-05-03
  • 打赏
  • 举报
回复
学习,请问一上两种有什么特点吗?器各自的优点与缺点是什么啊?
zhzh89 2009-05-03
  • 打赏
  • 举报
回复
重发一次:这个程序类似二叉树的后序遍历的非递归算法,通过两次进栈和出栈,可以访问到根结点,当访问到所要查找的结点时,将栈中元素弹栈,此时弹出的元素便是所要查找元素的所有祖先,DX们帮看看哪儿地方错了?
#define MAXSIZE 256
#include <stdio.h>
typedef struct bnode{
char data;
struct bnode *lchild,*rchild;
}Bnode,*BTree;/*定义二叉树*/
typedef struct{
BTree node;
int flag;
}DataType;
typedef struct{
DataType data[MAXSIZE];
int top;
}SeqStack,*PSeqStack;/*栈的定义*/
PSeqStack Init_SeqStack(void)
{
PSeqStack S;
S=(PSeqStack)malloc(sizeof(SeqStack));
if(S)
S->top=-1;
return S;
}
int Empty_SeqStack(PSeqStack S)
{
if(S->top==-1)
return 1;
else return 0;
}
int Push_SeqStack(PSeqStack S,DataType x)/*入栈*/
{
if(S->top==MAXSIZE-1)
return 0;
else
{
S->top++;
S->data[S->top]=x;
return 1;
}
}
int Pop_SeqStack(PSeqStack S,DataType *x)/*出栈*/
{
if(Empty_SeqStack(S))
return 0;
else
{
*x=S->data[S->top];
S->top--;
return 1;
}
}
BTree CreatTree(void)/*建立二叉树*/
{
BTree t;
char ch;
ch=getchar();
if(ch==' ') t=NULL;
else
{
t=(BTree)malloc(sizeof(Bnode));
t->data=ch;
t->lchild=CreatTree();
t->rchild=CreatTree();
}
return t;
}
void PostOrder(BTree t,char x)/*查找X的祖先*/
{
PSeqStack S;
DataType Sq;
BTree p=t;
int found=0;
S=Init_SeqStack();
while(p||!Empty_SeqStack(S))
{
if(p)
{
Sq.flag=0;
Sq.node=p;
Push_SeqStack(S,Sq);
p=p->lchild;
}
else
{
Pop_SeqStack(S,&Sq);
p=Sq.node;
if(Sq.flag==0)
{
Sq.flag=1;
Push_SeqStack(S,Sq);/*再次入栈*/
p=p->rchild;
}
else
{
if(p->data==x)/*找到x*/
{
found=1;
break;
}
}
}
} /*end while*/
if(found)
{
while(!Empty_SeqStack(S))
{
Pop_SeqStack(S,&Sq);
printf("%c",(Sq.node)->data);
}
}
else
printf("The value is not found in the tree!\n");
}/end PreOder/
int main()
{
char x;
BTree t;
printf("input a tree:\nexample:ab c \n");
t=CreatTree();
printf("input your seaching element:\n");
scanf("%c",&x);
PostOrder(t,x);
getch();
}
hw324306893 2009-05-02
  • 打赏
  • 举报
回复
学习
liliangbao 2009-05-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hairetz 的回复:]
每个节点加个parent节点成员就是了。
[/Quote]
up
  • 打赏
  • 举报
回复
每个节点加个parent节点成员就是了。
Paradin 2009-05-02
  • 打赏
  • 举报
回复
弄个直线父节点的指针

69,373

社区成员

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

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