在线索二叉树里爬上爬下

线索二叉树就是在遍历二叉链表的过程中添加各种判断,修改空指针,使之成为一个线索链表。

而线索二叉树的数据结构本身就和二叉树有着本质的区别

typedef emun flag {child,thread}

typedef struct BinThrNode{

    TElemtype data;

    BinThrNode *lchild,*rchild;

    flag LTag,RTag;

}BinThrNode,*BinThrTree;


在线索过程中,需要在二叉树的线索链表上添加一个头结点,它的data域为空;令其lchild域的指针指向二叉树的根节点;rchild域的指针指向中序遍历时访问的最后一个节点。

指针p指向当前访问的节点,且p在递归的过程中不断变化

利用pre指针存储刚刚访问过的节点

thrt其实只在开头和结尾有点用。。。


Status InOrderThreading(BinThrTree &Thrt,BinThrTree T){
  if(!(Thrt=(BinThrTree)malloc(sizeof(BinThrNode))))    exit(OVERFLOW);
  Thrt->LTag=Child;
  Thrt->RTag=Thread;               
  Thrt->rchild=Thrd;               //右指针回指         
  if(!T) Thrt->lchild=Thrt;     //若二叉树空,则左指针回指
  else{
      Thrt->lchild=T;  pre=Thrt;
      InThreading(T);
      pre->rchild=Thrt;  pre->RTag=Thread;      //最后一个结点线索化
      Thrt->rchild=pre;
    }
}

void InThreading(BinThrTree){
  if(p){
    InThreading(p->lchild);                    //左子树线索化
    if(!p->lchild) { p->LTag=Thread; p->lchild=pre; }          // 前趋线索
    if(!pre->rchild) {pre->RTag=Thread; pre->rchild=p; }      // 后继线索
    pre=p;                             // 保持pre指向p的前趋
    InThreading(p->rchild);            // 右子树线索化
    }
}
    
    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值