中序线索二叉树的实现

#include
typedef char Elemtype;
typedef struct TbNode{
    Elemtype info;
TbNode *Lchild,*Rchild;
int Ltag,Rtag;
}TBTNode;
TBTNode *root=0;
TBTNode *prev;
void insert(Elemtype el){
     
TBTNode *pre=0,*p=root,*newNode;
newNode=new TBTNode;
newNode->info=el;
    //printf("newNode:%c\n",newNode->info);
newNode->Lchild=0;
newNode->Rchild=0;
if(root==0){
root=newNode;
return;
    }
while(p!=0){
pre=p;
if(elinfo)
p=p->Lchild;
else
p=p->Rchild;
}
if(elinfo)
pre->Lchild=newNode;
else
pre->Rchild=newNode;


}



void creaTBTNode(TBTNode * &b,Elemtype *str){
char ch;
int j=0;
    ch=str[j];
while(ch!='\0'){
//printf("%c\n",ch);
if(ch!=',')
            insert(ch);
        j++;
ch=str[j];
}
    b=root;
}

void showtree(TBTNode *b){
if(b!=0){
printf("%c",b->info);
if(b->Lchild!=0||b->Rchild!=0){
printf("(");
showtree(b->Lchild);
if(b->Rchild!=0)
                printf(",");
showtree(b->Rchild);
printf(")");

}
}

void threat(TBTNode * &p){
    if(p!=0){
threat(p->Lchild);
if(p->Lchild==0){
p->Lchild=prev;
p->Ltag=1;
}
else
p->Ltag=0;
if(prev->Rchild==0){
prev->Rchild=p;
prev->Rtag=1;
}
else
prev->Rtag=0;
prev=p;
threat(p->Rchild);
}
}

TBTNode *creaThreat(TBTNode * b){
TBTNode *newroot;
newroot=new TBTNode;
newroot->Lchild=b;
newroot->Ltag=0;
newroot->Rtag=1;
if(b==0)
    newroot->Lchild=newroot;
else{
newroot->Lchild=b;
prev=newroot;
threat(b);
prev->Rchild=newroot;
prev->Rtag=1;
newroot->Rchild=prev;

}
return newroot;
}

void THinorder(TBTNode *tb){
TBTNode *p=tb->Lchild;
    printf("\n");
while(p!=tb){
while(p->Ltag==0)
p=p->Lchild;
printf("%c ",p->info);
while(p->Rtag==1&&p->Rchild!=tb){
            p=p->Rchild;
printf("%c ",p->info);
}
p=p->Rchild;

}

}

int main(){
TBTNode *b,*st;
creaTBTNode(b,"B,A,C,H,J,L,F,U,O,Y,T,I");
showtree(b);
    st=creaThreat(b);
THinorder(st);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值