线索二叉树

今天,我们一起用C++实现一颗线索二叉树,具体代码如下:

ThreadInorderIterator.h具体内容如下:

[cpp]  view plain copy
  1. #include "ThreadTree.h"  
  2.   
  3. template<typename Type> class ThreadInorderIterator{  
  4. public:  
  5.     ThreadInorderIterator(ThreadTree<Type> &tree) :m_ptree(tree), m_pcurrent(tree.m_proot){  
  6.         //InThread(m_ptree.m_proot->m_pleft,m_ptree.m_proot);  
  7.     }  
  8.   
  9.     ThreadNode<Type> *First();  
  10.     ThreadNode<Type> *Prior();  
  11.     ThreadNode<Type> *Next();  
  12.   
  13.     void Print();  
  14.     void Print(ThreadNode<Type> *start, int n = 0);  
  15.     void InOrder();  
  16.     void InsertLeft(ThreadNode<Type> *left);  
  17.     void InsertRight(ThreadNode<Type> *right);  
  18.     ThreadNode<Type> *GetParent(ThreadNode<Type> *current);  
  19.   
  20.   
  21. private:  
  22.     ThreadTree<Type> &m_ptree;  
  23.     ThreadNode<Type> *m_pcurrent;  
  24.     void InThread(ThreadNode<Type> *current, ThreadNode<Type> *pre);  
  25. };  
  26.   
  27. template<typename Type> void ThreadInorderIterator<Type>::InThread(  
  28.     ThreadNode<Type> *current, ThreadNode<Type> *pre){  
  29.     if (current != m_ptree.m_proot){  
  30.         InThread(current->m_pleft, pre);  
  31.         if (current->m_pleft == NULL){  
  32.             current->m_pleft = pre;  
  33.             current->m_nleftthread = 1;  
  34.         }  
  35.         if (pre->m_pright == NULL){  
  36.             pre->m_pright = current;  
  37.             pre->m_nrightthread = 1;  
  38.         }  
  39.   
  40.         pre = current;  
  41.         InThread(current->m_pright, pre);  
  42.     }  
  43. }  
  44.   
  45. template<typename Type> ThreadNode<Type>* ThreadInorderIterator<Type>::First(){  
  46.     while (m_pcurrent->m_nleftthread == 0){  
  47.         m_pcurrent = m_pcurrent->m_pleft;  
  48.     }  
  49.     return m_pcurrent;  
  50. }  
  51.   
  52. template<typename Type> ThreadNode<Type>* ThreadInorderIterator<Type>::Prior(){  
  53.     ThreadNode<Type> *pmove = m_pcurrent->m_pleft;  
  54.     if (0 == m_pcurrent->m_nleftthread){  
  55.         while (0 == pmove->m_nrightthread){  
  56.             pmove = pmove->m_pright;  
  57.         }  
  58.     }  
  59.     m_pcurrent = pmove;  
  60.     if (m_pcurrent == m_ptree.m_proot){  
  61.         return NULL;  
  62.     }  
  63.     return m_pcurrent;  
  64. }  
  65.   
  66. template<typename Type> ThreadNode<Type>* ThreadInorderIterator<Type>::Next(){  
  67.     ThreadNode<Type> *pmove = m_pcurrent->m_pright;  
  68.     if (0 == m_pcurrent->m_nrightthread){  
  69.         while (0 == pmove->m_nleftthread){  
  70.             pmove = pmove->m_pleft;  
  71.         }  
  72.     }  
  73.     m_pcurrent = pmove;  
  74.     if (m_pcurrent == m_ptree.m_proot){  
  75.         return NULL;  
  76.     }  
  77.     return m_pcurrent;  
  78. }  
  79.   
  80. template<typename Type> void ThreadInorderIterator<Type>::InOrder(){  
  81.     ThreadNode<Type> *pmove = m_ptree.m_proot;  
  82.     while (pmove->m_pleft != m_ptree.m_proot){  
  83.         pmove = pmove->m_pleft;  
  84.     }  
  85.     m_pcurrent = pmove;  
  86.     cout << "root";  
  87.     while (pmove != m_ptree.m_proot&&pmove){  
  88.         cout << "--->" << pmove->m_data;  
  89.         pmove = this->Next();  
  90.     }  
  91.     cout << "--->end";  
  92. }  
  93.   
  94. template<typename Type> void ThreadInorderIterator<Type>::InsertLeft(ThreadNode<Type> *left){  
  95.     left->m_pleft = m_pcurrent->m_pleft;  
  96.     left->m_nleftthread = m_pcurrent->m_nleftthread;  
  97.     left->m_pright = m_pcurrent;  
  98.     left->m_nrightthread = 1;  
  99.     m_pcurrent->m_pleft = left;  
  100.     m_pcurrent->m_nleftthread = 0;  
  101.     if (0 == left->m_nleftthread){  
  102.         m_pcurrent = left->m_pleft;  
  103.         ThreadNode<Type> *temp = First();  
  104.         temp->m_pright = left;  
  105.     }  
  106.     m_pcurrent = left;  
  107. }  
  108.   
  109. template<typename Type> void ThreadInorderIterator<Type>::InsertRight(ThreadNode<Type> *right){  
  110.     right->m_pright = m_pcurrent->m_pright;  
  111.     right->m_nrightthread = m_pcurrent->m_nrightthread;  
  112.     right->m_pleft = m_pcurrent;  
  113.     right->m_nleftthread = 1;  
  114.     m_pcurrent->m_pright = right;  
  115.     m_pcurrent->m_nrightthread = 0;  
  116.     if (0 == right->m_nrightthread){  
  117.         m_pcurrent = right->m_pright;  
  118.         ThreadNode<Type> *temp = First();  
  119.         temp->m_pleft = right;  
  120.     }  
  121.     m_pcurrent = right;  
  122. }  
  123.   
  124. template<typename Type> ThreadNode<Type>* ThreadInorderIterator<Type>::GetParent(  
  125.     ThreadNode<Type> *current){  
  126.     ThreadNode<Type> *pmove = current;  
  127.     while (0 == pmove->m_nleftthread){  
  128.         pmove = pmove->m_pleft;  
  129.     }  
  130.     pmove = pmove->m_pleft;  
  131.     if (pmove == m_ptree.m_proot){  
  132.         if (pmove->m_pleft == current){  
  133.             return NULL;  
  134.         }  
  135.     }  
  136.     if (pmove->m_pright == current){  
  137.         return pmove;  
  138.     }  
  139.     pmove = pmove->m_pright;  
  140.     while (pmove->m_pleft != current){  
  141.         pmove = pmove->m_pleft;  
  142.     }  
  143.     return pmove;  
  144. }  
  145.   
  146. template<typename Type> void ThreadInorderIterator<Type>::Print(ThreadNode<Type> *start, int n){  
  147.     if (start->m_nleftthread&&start->m_nrightthread){  
  148.         for (int i = 0; i < n; i++){  
  149.             cout << "     ";  
  150.         }  
  151.         if (n >= 0){  
  152.             cout << start->m_data << "--->" << endl;  
  153.         }  
  154.   
  155.         return;  
  156.     }  
  157.     if (start->m_nrightthread == 0){  
  158.         Print(start->m_pright, n + 1);  
  159.     }  
  160.     for (int i = 0; i < n; i++){  
  161.         cout << "     ";  
  162.     }  
  163.     if (n >= 0){  
  164.         cout << start->m_data << "--->" << endl;  
  165.     }  
  166.     if (start->m_nleftthread == 0){  
  167.         Print(start->m_pleft, n + 1);  
  168.     }  
  169. }  
  170.   
  171. template<typename Type> void ThreadInorderIterator<Type>::Print(){  
  172.     Print(m_ptree.m_proot->m_pleft);  
  173. }  
ThreadNode.h具体内容如下:

[cpp]  view plain copy
  1. template<typename Type> class ThreadTree;  
  2. template<typename Type> class ThreadInorderIterator;  
  3.   
  4. template<typename Type> class ThreadNode{  
  5. public:  
  6.     friend class ThreadTree < Type > ;  
  7.     friend class ThreadInorderIterator < Type > ;  
  8.     ThreadNode() :m_nleftthread(1), m_nrightthread(1){  
  9.         m_pleft = this;  
  10.         m_pright = this;  
  11.     }  
  12.     ThreadNode(const Type item) :m_data(item), m_pleft(NULL), m_pright(NULL)  
  13.         , m_nleftthread(0), m_nrightthread(0){}  
  14.   
  15. private:  
  16.     int m_nleftthread, m_nrightthread;  
  17.     ThreadNode<Type> *m_pleft, *m_pright;  
  18.     Type m_data;  
  19. };  
ThreadTree.h具体内容如下:

[cpp]  view plain copy
  1. #include "ThreadNode.h"  
  2.   
  3. template<typename Type> class ThreadInorderIterator;  
  4.   
  5. template<typename Type> class ThreadTree{  
  6. public:  
  7.     friend class ThreadInorderIterator < Type > ;  
  8.     ThreadTree() :m_proot(new ThreadNode<Type>()){}  
  9.   
  10. private:  
  11.     ThreadNode<Type> *m_proot;  
  12. };  
main.cpp具体内容如下:

[cpp]  view plain copy
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. #include "ThreadInorderIterator.h"  
  6.   
  7. int main(){  
  8.     ThreadTree<int> tree;  
  9.     ThreadInorderIterator<int> threadtree(tree);  
  10.     int init[10] = { 3, 6, 0, 2, 8, 4, 9, 1, 5, 7 };  
  11.     for (int i = 0; i < 10;){  
  12.         threadtree.InsertLeft(new ThreadNode<int>(init[i++]));  
  13.         threadtree.InsertRight(new ThreadNode<int>(init[i++]));  
  14.     }  
  15.     threadtree.Print();  
  16.     cout << endl << endl;  
  17.   
  18.     threadtree.InOrder();  
  19.     return 0;  
  20. }  
运行效果如图1所示:

图1 运行效果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值