知识大全 数据结构考研分类复习真题 第六章 答案 (五)[49]
Posted 结点
篇首语:一箫一剑平生意,负尽狂名十五年。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 数据结构考研分类复习真题 第六章 答案 (五)[49]相关的知识,希望对你有一定的参考价值。
.[题目分析]第 题已讨论了在中序线索树中查找结点p的后序后继问题 本题要求在中序线索树上进行后序遍历 因后序遍历是 左右根 最后访问根结点 即只有从右子树返回时才能访问根结点 为此设一标志returnflag 当其为 时表示从右侧返回 可以访问根结点 为了找当前结点的后继 需知道双亲结点的信息 在中序线索树中 某结点最左子孙的左线索和最右子孙的右线索均指向其双亲 因此设立两个函数LeftMost和RightMost求结点的最左和最右子孙 为了判定是否是从右子树返回 再设一函数IsRightChild
BiThrTree LeftMost(BiThrTree t) //求结点t最左子孙的左线索 BiThrTree p=t; while(p >ltag== ) p=p >lchild; //沿左分枝向下 if (p >lchild!=null) return(p >lchild); else return(null); //LeftMost BiThrTree RightMost(BiThrTree t)//求结点t最右子孙的右线索 BiThrTree p=t; while(p >rtag== ) p=p >rchild; //沿右分枝向下 if (p >rchild!=null) return (p >rchild); else return(null); //RightMost int IsRightChild(BiThrTree t father) //若t是father 的右孩子 返回 否则返回 father=LeftMost(t); if(father &&f ather >rchild==t) return( ); else return( ); //Is RightChild; void PostOrderInThr (BiThrTree bt) //后序遍历中序线索二叉树bt BiThrTree father p=bt; int flag; while(p!=null) while(p >ltag== ) p=p >lchild; // 沿左分枝向下 if(p >rtag== ) flag= ;//左孩子为线索 右孩子为链 相当从左返回 else flag= ; //p为叶子 相当从右返回 while(flag== ) visit(*p);//访问结点 if(IsRightChild(p father)) p=father; flag= ; //修改p指向双亲 else //p是左子女 用最右子孙的右线索找双亲 p=RightMost(p); if(p&&p >rtag== ) flag= ; else flag= ; // while(flag== ) if(flag== && p!=null) p=p >rchild; //转向当前结点右分枝 //结束PostOrderInThr
cha138/Article/program/sjjg/201311/23691相关参考