题目
【题目】写出按后序序列遍历中序线索树的算法。
【解析】函数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