#include<iostream>
using namespace std;
typedef struct bnd {
char data;
struct bnd *lch,*rch; /*左右孩子指针*/
int ltag,rtag; /*左右标志*/
bnd():data(' '),lch(NULL),rch(NULL),ltag(0),rtag(0){}
} bnd,*btr;
btr pre;
void create(btr &t){
char ch; cin>>ch;
if(ch=='#') t=NULL;
else{
t=new bnd;
t->data=ch;
create(t->lch);
create(t->rch);
}
}
void visit(btr q){
if(q->lch==NULL){
q->lch=pre;
q->ltag=1;
}
if(pre!=NULL&&pre->rch==NULL){
pre->rch=q;
pre->rtag=1;
}
pre=q;
}
void inthr(btr t){
if(t!=NULL){
inthr(t->lch);
visit(t);
inthr(t->rch);
}
}
void create_inthr(btr t){
pre=NULL;
if(t!=NULL){
inthr(t);
if(pre->rch==NULL)
pre->rtag=1;
}
}
btr firstnd(btr p){
while(p->ltag==0) p=p->lch;
return p;
}
btr nextnd(btr p){
if(p->rtag==0) return firstnd(p->rch);
else return p->rch;
}
void inorder(btr t){
for(btr p=firstnd(t);p!=NULL;p=nextnd(p)){
cout<<p->data<<' ';
}
}
int main() {
btr t;
btr th;
cout<<"Input the Threaded BinaryTree 's node:"<<endl;
create(t);
create_inthr(t);
cout<<"InOrderTraverse_Thr:"<<endl;
inorder(t);
return 0;
}
中序线索二叉树的建立与遍历(from: 王道考研)
最新推荐文章于 2024-08-26 12:12:01 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)