中序线索二叉树_【每日一题音频讲解版 —— 数据结构 day88(考点:树和二叉树)】...

题目

【题目】写出按后序序列遍历中序线索树的算法。

【解析】函数LeftMost和RightMost求结点的最左和最右子孙,为了判定是否是从右子树返回,再设一函数IsRightChild。

BiThrTree LeftMost(BiThrTree t) //求结点t最左子孙的左线索{  BiThrTree p=t;  while(p->ltag==0)     p=p->lchild; //沿左分枝向下  if (p->lchild!=null)     return(p->lchild);   else return(null);}//LeftMostBiThrTree RightMost(BiThrTree t)//求结点t最右子孙的右线索{  BiThrTree p=t;  while(p->rtag==0)     p=p->rchild;   //沿右分枝向下  if (p->rchild!=null)     return (p->rchild);   else return(null);}//RightMostint IsRightChild(BiThrTree t,father)   //若t是father 的右孩子,返回1,否则返回0{  father=LeftMost(t);  if(father &&f ather->rchild==t)     return(1);   else return(0);}//Is RightChild;void PostOrderInThr (BiThrTree bt) //后序遍历中序线索二叉树bt{  BiThrTree father, p=bt;  int flag;  while(p!=null) {   while(p->ltag==0 )      p=p->lchild; // 沿左分枝向下   if(p->rtag==0)      flag=0;//左孩子为线索,右孩子为链,相当从左返回   else      flag=1;          //p为叶子,相当从右返回        while(flag==1)   {     visit(*p);//访问结点     if(IsRightChild(p,father))      {       p=father;        flag=1;      } //修改p指向双亲     else   //p是左子女,用最右子孙的右线索找双亲     {        p=RightMost(p);       if(p&&p->rtag==1)          flag=1;        else flag=0;        }     }// while(flag==1)   if(flag==0 && p!=null)      p=p->rchild; //转向当前结点右分枝   } //while(p!=null)}//结束PostOrderInThr

981214921a5c38af64cb1d6236f329de.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值