线索二叉树的前序、中序

  

,,,,二叉树是一种非线性结构,遍历二叉树几乎都是通过递归或者用栈辅助实现非递归的遍历。用二叉树作为存储结构时,取到一个节点,只能获取节点的左孩子和右孩子,不能直接得到节点的任一遍历序列的前驱或者后继。

,,,,而线索二叉树利用二叉树中来存放节点的和信息

结点信息如下

enum  PointerTag{线程的不同之处是,LINK };      template   struct  BinaryTreeNodeThd   {   T  _data;,,,,,,,,,,,,,,,,,,,,,,//数据   BinaryTreeNodeThd *, _left;,,//左孩子   BinaryTreeNodeThd *, _right;,//右孩子   PointerTag  _leftTag;,,,,,,,,,,//左孩子线索标志   PointerTag  _rightTag;,,,,,,,,,//右孩子线索标志   };

其前序结构如下

线索二叉树的前序、中序

其中序结构如下


线索二叉树的前序、中序”> <br/> </p> <p>程序实现:</p> <pre类= # include      using  namespace 性传播疾病;      enum  PointerTag{线程的不同之处是,LINK };      template   struct  BinaryTreeNodeThd   {   T  _data;,,,,,,,,,,,,,,,,,,,,,,//数据   BinaryTreeNodeThd *, _left;,,//左孩子   BinaryTreeNodeThd *, _right;,//右孩子   PointerTag  _leftTag;,,,,,,,,,,//左孩子线索标志   PointerTag  _rightTag;,,,,,,,,,//右孩子线索标志      BinaryTreeNodeThd (const  T&, x)   :_data (x)   ,_left(空)   ,_right(空)   ,_leftTag(链接)   ,_rightTag(链接)   {}   };      template   class  BinaryTreeThd   {   typedef  BinaryTreeNodeThd< T>,节点;   公众:   BinaryTreeThd ()   :_root(空)   {}   BinaryTreeThd (const  T *, size_t 大小,const  T&,无效)   {   size_t  index =, 0;   _root =, _CreateTree(一个,大小,指数,无效);   }   void  InOrderThreading()//中序线索化   {   节点* prev =,空;   _InOrderThreading (_root,上一页);   }   void  PrevOderThreading()//前序线索化   {   节点* prev =,空;   _PrevOderThreading (_root,上一页);   }      void  InOrderThd()//中序遍历   {   _InOrderThd (_root);   }   void  PrevOrderThd()//前序遍历   {   _PrevOrderThd (_root);   }   保护:   节点*,_CreateTree (const  T *, size_t 大小,size_t&,指数,const  T&,无效)   {   节点*,_root =,空;      if  (index  & lt;, size&和一个(指数),!=,无效)   {   时间=_root  new 节点((指数));   _root→_left =, _CreateTree(体积,,,+ +指数,无效);   _root→_right =, _CreateTree(体积,,,+ +指数,无效);   }   return  _root;   }      void  _PrevOderThreading(节点*,根,,节点*,,上一页)//前序线索化   {   if  (root ==, NULL)   返回;      if (根→_left ==, NULL)   {   根→_leftTag =,线程;   根→_left=,上一页;   }      if  (prev&和上一页→_right ==, NULL)   {   prev→_rightTag =,线程;   prev→_right =,根;   }   时间=prev 根源;   if (根→_leftTag ==,链接)//递归   {   _PrevOderThreading(根→_left上一页);//线索化左子树   }      if (根→_rightTag ==,链接)   {   _PrevOderThreading(根→_right上一页);//线索化右子树   }   }      void  _PrevOrderThd(节点*,根)   {   节点* cur =,根;      while (坏蛋)   {   while  (cur→_leftTag ==,链接)   {   cout  & lt; & lt;,坏蛋→_data  & lt; & lt;,“,”;   时间=cur  cur→_left;   }   cout  & lt; & lt;,坏蛋→_data  & lt; & lt;,“,”;   时间=cur  cur→_right;   }   }/*方法二   void  _PrevOrderThd(节点*,根)   {   节点* cur =,根;      while (坏蛋)   {   while  (cur→_leftTag==链接)   {   cout  & lt; & lt;,坏蛋→_data  & lt; & lt;,“,”;   时间=cur  cur→_left;   }   cout  & lt; & lt;,坏蛋→_data  & lt; & lt;,“,”;   while  (cur→_rightTag ==,线程)   {   时间=cur  cur→_right;   cout  & lt; & lt;,坏蛋→_data  & lt; & lt;,“,”;   }   if  (cur→_leftTag ==,链接)   {   时间=cur  cur→_left;   }   其他的   {   时间=cur  cur→_right;   }   }   } */void  _InOrderThreading(节点*,_root,节点*,和上一页)//中序线索化   {   if  (_root ==, NULL)   {   返回;   }   if  (_root→_leftTag==链接)   _InOrderThreading (_root→_left上一页);//线索化   if  (_root→_left ==, NULL)//左孩子为空   {   时间=_root→_leftTag 螺纹;   时间=_root→_left  prev;   }   if  (prev  !=, NULL&和上一页→_right ==, NULL)//前驱的右孩子为空   {   prev→_rightTag =,线程;   prev→_right =, _root;   }   时间=prev  _root;      if  (_root→_rightTag==链接)//线索化右孩子   _InOrderThreading (_root→_right上一页);   }      null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

线索二叉树的前序、中序