数据结构之线索二叉树的前序,中序和后序遍

   二叉树是一种非线性结构,在之前实现的二叉树遍历中不管是递归还是非递归用二叉树作为存储结构时只能取到该结点的左孩子和右孩子,不能得到该结点的前驱和后继。为了保存这种在遍历中需要的信息,同时也为了充分利用结点中的空指针域我们利用二叉树中指向左右子树的空指针来存放结点的前驱和后继.同时在有n个结点的二叉链表中必定存在n+1个空链域.

     那仫问题来了如何充分的利用这些空链域来实现线索化呢?

     假设做如下规定:若结点有左子树则其左孩子域指向该结点的左孩子,否则另左孩子指向其前驱;若结点有右子树则其右孩子域指向该结点的右孩子,否则另右孩子域指向其后继.为了实现这种构想我们需要增加两个标志域_leftTag,_rightTag.

     

     一.线索化及中序遍历一棵树

        要线索化一颗二叉树,最重要的就是在访问了当前结点并确定当前结点的左孩子域或者右孩子域为空后如何找到当前结点的前一个结点也就是它的前驱,和当前结点的后继.在这里我们用到了一个_prev指针用来保存你访问结点的前一个结点--如果当前结点的左子树域为空则使当前结点的左孩子域指向_prev所指向的结点也就是当前结点的前驱;我们知道中序是按照左-根-右的顺序遍历的,如果当前结点的左访问过了,根也访问过了按照中序遍历的顺序接下来就应该访问右了-_prev指针指向的是当前结点的前一个结点要访问右则只需判断_prev的右孩子域是否为空就可以了,如果为空则使_prev的右孩子域指向它的后继.

        

      

[cpp]  view plain  copy
  1. void _InOrderThread(Node *root)  
  2. {  
  3.     if(root == NULL)  
  4.         return ;  
  5.     //左子树  
  6.     _InOrderThread(root->_left);  
  7.     //根  
  8.     if (root->_left == NULL)  
  9.     {  
  10.         root->_leftTag=THREAD;  
  11.         root->_left=_prev;  
  12.     }  
  13.     if (_prev  && _prev->_right == NULL)  
  14.     {  
  15.         _prev->_rightTag=THREAD;  
  16.         _prev->_right=root;  
  17.     }  
  18.     _prev=root;  
  19.     //右子树  
  20.     _InOrderThread(root->_right);  
  21. }  


 

     

      中序线索化一颗二叉树后如何遍历呢?和之前非递归的方法类似,使得cur指针指向该树的左路结点,如果该结点有右子树则访问右子树,如果没有右子树则继续修改cur的值,使其指向当前结点的后继.

       

     

[cpp]  view plain  copy
  1. void _InOrder(Node *root)  
  2. {  
  3.     if(root == NULL)  
  4.         return ;  
  5.     Node *cur=root;  
  6.     while (cur)  
  7.     {  
  8.         //一路向左找到最左的结点.  
  9.         while (cur->_leftTag == LINK)  
  10.         {  
  11.             cur=cur->_left;  
  12.         }  
  13.         cout<<cur->_data<<" ";  
  14.         //有后继  
  15.         while (cur && cur->_rightTag == THREAD)  
  16.         {  
  17.             cur=cur->_right;  
  18.             cout<<cur->_data<<" ";  
  19.         }  
  20.         //没有后继有右子树  
  21.         cur=cur->_right;  
  22.     }  
  23. }  


 

   

     二.线索化及前序遍历一棵树>

       前序遍历的线索化类似中序遍历的线索化,但是因为前序的顺序是根-左-右,会因为递归太深导致栈溢出的问题,所以在递归线索化左子树和右子树的时候需要判断当前结点的左右标志是否为LINK类型-是才递归线索化左子树和右子树.

      

      

[cpp]  view plain  copy
  1. void _PrevOrderThread(Node *root)  
  2. {  
  3.     if(root == NULL)  
  4.         return ;  
  5.     if (root->_left == NULL)  
  6.     {  
  7.         root->_leftTag=THREAD;  
  8.         root->_left=_prev;  
  9.     }  
  10.     if (_prev && _prev->_right == NULL)  
  11.     {  
  12.         _prev->_rightTag=THREAD;  
  13.         _prev->_right=root;  
  14.     }  
  15.   
  16.     _prev=root;  
  17.   
  18.     if(root->_leftTag == LINK)   //防止栈溢出  
  19.         _PrevOrderThread(root->_left);  
  20.     if(root->_rightTag == LINK)  
  21.         _PrevOrderThread(root->_right);  
  22. }  


 

[cpp]  view plain  copy
  1. void _PrevOrder(Node *root)  
  2. {  
  3.     if(root == NULL)  
  4.         return ;  
  5.     Node *cur=root;  
  6.     while (cur)  
  7.     {  
  8.         while (cur && cur->_leftTag == LINK)  
  9.         {  
  10.             cout<<cur->_data<<" ";  
  11.             cur=cur->_left;  
  12.         }  
  13.         cout<<cur->_data<<" ";  
  14.         //将该树的后继当成子树来访问  
  15.         cur=cur->_right;  
  16.   
  17.         //while (cur && cur->_rightTag == THREAD)  
  18.         //{  
  19.         //  cur=cur->_right;  
  20.         //  cout<<cur->_data<<" ";  
  21.         //}  
  22.         //cur=cur->_right;  
  23.     }  
  24. }  


 

      

     三.线索化及后序遍历一棵树>

        在后序线索树中找一个结点的前驱很容易直接用_prev就可以找到,那仫如何找一个结点的后继呢?这时就比较棘手了

        

       

      也就是说,在后序线索化树上找后继时需要知道结点双亲,此时我们可以使用三叉链表做存储结构找到其双亲结点,其实还有一种思路就是逆向思维的方式----我们知道在后序遍历线索二叉树时有的时候是不容易找到一个结点的后继的,如果我们按照根-右-左的顺序逆序遍历一颗线索二叉树就会变的非常简单了.

      在实现中我只实现第二种思路即倒着访问一颗后序线索化的二叉树.

      

        程序源代码>

        BinaryTreeThd.h

        

[cpp]  view plain  copy
  1. #pragma once  
  2.   
  3. enum PointerTag  
  4. {  
  5.     THREAD,   //0:线索  
  6.     LINK      //1:指针  
  7. };  
  8. template<typename T>  
  9. struct BinaryTreeThdNode  
  10. {  
  11.     BinaryTreeThdNode(const T& x=T())  
  12.         :_data(x)  
  13.         ,_left(NULL)  
  14.         ,_right(NULL)  
  15.         ,_leftTag(LINK)  
  16.         ,_rightTag(LINK)  
  17.     {}  
  18.     T _data;  
  19.     BinaryTreeThdNode<T> *_left;       
  20.     BinaryTreeThdNode<T> *_right;  
  21.     PointerTag _leftTag;       //左孩子线索化标志  
  22.     PointerTag _rightTag;      //右孩子线索化标志  
  23. };  
  24.   
  25. template<typename T>  
  26. class BinaryTreeThd  
  27. {  
  28.     typedef BinaryTreeThdNode<T> Node;  
  29. public:  
  30.     BinaryTreeThd(const T *a,size_t size,const T& invalid)  
  31.         :_root(NULL)  
  32.         ,_prev(NULL)  
  33.     {  
  34.         size_t index=0;  
  35.         _root=_CreatTreeThd(a,size,index,invalid);  
  36.     }  
  37. public:  
  38.     void PrevOrderThread()    //前序线索化二叉树  
  39.     {  
  40.         _PrevOrderThread(_root);  
  41.     }  
  42.     void PrevOrder()         //前序遍历二叉树  
  43.     {  
  44.         _PrevOrder(_root);  
  45.         cout<<endl;  
  46.     }  
  47.     void InOrderThread()    //中序线索化二叉树  
  48.     {  
  49.         _InOrderThread(_root);  
  50.     }  
  51.     void InOrder()          //中序遍历二叉树  
  52.     {  
  53.         _InOrder(_root);  
  54.         cout<<endl;  
  55.     }  
  56.     void PostOrderThread()  //后序线索化二叉树  
  57.     {  
  58.         _PostOrderThread(_root);  
  59.     }  
  60.     void PostOrder()  
  61.     {  
  62.         _PostOrder(_root);  
  63.         cout<<endl;  
  64.     }  
  65. protected:  
  66.     Node *_CreatTreeThd(const T *a,size_t size,size_t& index,const T& invalid)  
  67.     {  
  68.         assert(a);  
  69.         Node *root=NULL;  
  70.         if (index < size && a[index] != invalid)  
  71.         {  
  72.             root=new Node(a[index]);  
  73.             root->_left=_CreatTreeThd(a,size,++index,invalid);  
  74.             root->_right=_CreatTreeThd(a,size,++index,invalid);  
  75.         }  
  76.         return root;  
  77.     }  
  78. protected:  
  79.     void _PrevOrderThread(Node *root)  
  80.     {  
  81.         if(root == NULL)  
  82.             return ;  
  83.         if (root->_left == NULL)  
  84.         {  
  85.             root->_leftTag=THREAD;  
  86.             root->_left=_prev;  
  87.         }  
  88.         if (_prev && _prev->_right == NULL)  
  89.         {  
  90.             _prev->_rightTag=THREAD;  
  91.             _prev->_right=root;  
  92.         }  
  93.   
  94.         _prev=root;  
  95.   
  96.         if(root->_leftTag == LINK)   //防止栈溢出  
  97.             _PrevOrderThread(root->_left);  
  98.         if(root->_rightTag == LINK)  
  99.             _PrevOrderThread(root->_right);  
  100.     }  
  101.     void _InOrderThread(Node *root)  
  102.     {  
  103.         if(root == NULL)  
  104.             return ;  
  105.         //左子树  
  106.         _InOrderThread(root->_left);  
  107.         //根  
  108.         if (root->_left == NULL)  
  109.         {  
  110.             root->_leftTag=THREAD;  
  111.             root->_left=_prev;  
  112.         }  
  113.         if (_prev  && _prev->_right == NULL)  
  114.         {  
  115.             _prev->_rightTag=THREAD;  
  116.             _prev->_right=root;  
  117.         }  
  118.         _prev=root;  
  119.         //右子树  
  120.         _InOrderThread(root->_right);  
  121.     }  
  122.     void _PostOrderThread(Node *root)  
  123.     {  
  124.         if(root == NULL)  
  125.             return ;  
  126.         _PostOrderThread(root->_left);  
  127.         _PostOrderThread(root->_right);  
  128.         if (root->_left == NULL)  
  129.         {  
  130.             root->_leftTag=THREAD;  
  131.             root->_left=_prev;  
  132.         }  
  133.         if (_prev && _prev->_right == NULL)  
  134.         {  
  135.             _prev->_rightTag=THREAD;  
  136.             _prev->_right=root;  
  137.         }  
  138.         _prev=root;  
  139.     }  
  140.     void _PrevOrder(Node *root)  
  141.     {  
  142.         if(root == NULL)  
  143.             return ;  
  144.         Node *cur=root;  
  145.         while (cur)  
  146.         {  
  147.             while (cur && cur->_leftTag == LINK)  
  148.             {  
  149.                 cout<<cur->_data<<" ";  
  150.                 cur=cur->_left;  
  151.             }  
  152.             cout<<cur->_data<<" ";  
  153.             //将该树的后继当成子树来访问  
  154.             cur=cur->_right;  
  155.   
  156.             //while (cur && cur->_rightTag == THREAD)  
  157.             //{  
  158.             //  cur=cur->_right;  
  159.             //  cout<<cur->_data<<" ";  
  160.             //}  
  161.             //cur=cur->_right;  
  162.         }  
  163.     }  
  164.     void _InOrder(Node *root)  
  165.     {  
  166.         if(root == NULL)  
  167.             return ;  
  168.         Node *cur=root;  
  169.         while (cur)  
  170.         {  
  171.             //一路向左找到最左的结点.  
  172.             while (cur && cur->_leftTag == LINK)  
  173.             {  
  174.                 cur=cur->_left;  
  175.             }  
  176.             cout<<cur->_data<<" ";  
  177.             //有后继  
  178.             while (cur && cur->_rightTag == THREAD)  
  179.             {  
  180.                 cur=cur->_right;  
  181.                 cout<<cur->_data<<" ";  
  182.             }  
  183.             //没有后继有右子树  
  184.             cur=cur->_right;  
  185.         }  
  186.     }  
  187.     //倒着遍历后序线索化二叉树  
  188.     //根-右-左  
  189.     void _PostOrder(Node *root)  
  190.     {  
  191.         if(root == NULL)  
  192.             return ;  
  193.         Node *cur=root;  
  194.         while (cur)  
  195.         {  
  196.             while (cur && cur->_rightTag == LINK)  
  197.             {  
  198.                 cout<<cur->_data<<" ";  
  199.                 cur=cur->_right;  
  200.             }  
  201.             cout<<cur->_data<<" ";  
  202.             //有前驱  
  203.             while (cur->_left && cur->_leftTag == THREAD)  
  204.             {  
  205.                 cur=cur->_left;  
  206.                 cout<<cur->_data<<" ";  
  207.             }  
  208.             //有左子树  
  209.             cur=cur->_left;  
  210.         }  
  211.     }  
  212. protected:  
  213.     Node *_root;  
  214.     Node *_prev;    //保存当前结点的前一个结点  
  215. };  


 

 

       test.cpp

 

      

[cpp]  view plain  copy
  1. void testBinaryTreeThd()  
  2. {  
  3.     int array[15] = {1,2,'#',3,'#','#',4,5,'#',6,'#',7,'#','#',8};  
  4.     size_t size=sizeof(array)/sizeof(array[0]);  
  5.     BinaryTreeThd<int> bt1(array,size,'#');  
  6.     cout<<"线索化中序遍历>";  
  7.     bt1.InOrderThread();  
  8.     bt1.InOrder();        //3  2  1  5  6  7  4  8  
  9.   
  10.     BinaryTreeThd<int> bt2(array,size,'#');  
  11.     cout<<"线索化前序遍历>";  
  12.     bt2.PrevOrderThread();  
  13.     bt2.PrevOrder();      //1  2  3  4  5  6  7  8   
  14.   
  15.     BinaryTreeThd<int> bt3(array,size,'#');  
  16.     cout<<"线索化后序遍历>";  
  17.     bt3.PostOrderThread();  
  18.     bt3.PostOrder();      //1  4  8  5  6  7  2  3  
  19. }  


 

       


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值