首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于线索 二叉树,为何只遍历了根节点的左子树?

2013-10-25 
关于线索 二叉树,为什么只遍历了根节点的左子树???#includeiostream#includestdlib.husing namespace

关于线索 二叉树,为什么只遍历了根节点的左子树???
关于线索 二叉树,为何只遍历了根节点的左子树?关于线索 二叉树,为何只遍历了根节点的左子树?


#include<iostream>
#include<stdlib.h>
using namespace std;
typedef char ElemType;
typedef enum{link,thread}PointerTag;//link==0:指针,thread==1:线索
typedef struct TNode
{
ElemType data;//数据域
struct TNode *lch,*rch;//左右孩子指针
PointerTag LTag,RTag;//标志域
}TNode,*Bitree;
Bitree pre;
Bitree CreatT(Bitree &T)//创建二叉树
{
ElemType ch;
cout<<"  ";
cin>>ch;
if(ch=='#')
T = NULL;
else
{
if(!(T=new TNode))
exit(OVERFLOW);
T->data = ch;
CreatT(T->lch);
CreatT(T->rch);
}
return T;
}
void InThreading(Bitree p)//中序遍历进行中序线索化
{
if(p)
{
InThreading(p->lch);
if(!p->lch)
{
p->LTag = thread;
p->lch = pre;
}
else
p->LTag = link;
if(!pre->rch)
{
pre->RTag = thread;
pre->rch = p;
}
else
p->RTag = link;
pre = p;
InThreading(p->rch);
}
}
void InOrderThreading(Bitree &thr,Bitree T)//建立头结点,中序线索二叉树
{
if(!(thr=new TNode))
exit(OVERFLOW);
thr->LTag = link;
thr->RTag = thread;
thr->rch = thr;
//thr->RTag = link;
if(!T)
thr->lch = thr;
else
{
thr->lch = T;
pre = thr;
InThreading(T);
pre->rch = thr;
pre->RTag = thread;
thr->rch = pre;
}
}
void InOrderTraverse(Bitree T)
{
Bitree p = T->lch;
while(p!=T)//空树或遍历结束时,p==T
{
while(p->LTag==link)
p = p->lch;
cout<<p->data<<"  ";
while(p->RTag==thread&&p->rch!=T)
{
p = p->rch;
cout<<p->data<<"  ";
}
p = p->rch;
}
}
int main()
{
Bitree T,thr;
cout<<"Enter data of binary tree:\n";//输入ABDH###E##CF##G##
CreatT(T);
InOrderThreading(thr,T);
cout<<"Print data of binary tree:\n";//只输出了根节点的左子树HDBE
InOrderTraverse(T);
cout<<endl;
return 0;
}
二叉树 遍历 c C++ 算法
[解决办法]
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试是程序员必须掌握的技能之一。

[解决办法]
支持老是 单步调试吧 

热点排行