Chinaunix首页 | 论坛 | 博客
  • 博客访问: 265269
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-03-22 13:03:58


  1. #include <stack>
  2. #pragma once

  3. enum PointerTag{THREAD,LINK};

  4. template<class T>
  5. struct BinaryTreeNode_Thd
  6. {
  7.     BinaryTreeNode_Thd(const T& data)
  8.         :_data(data)
  9.         ,_left(NULL)
  10.         ,_right(NULL)
  11.         ,_leftTag(LINK)
  12.         ,_rightTag(LINK)
  13.     {}
  14.     T _data;
  15.     BinaryTreeNode_Thd<T> *_left;
  16.     BinaryTreeNode_Thd<T> *_right;
  17.     PointerTag _leftTag;
  18.     PointerTag _rightTag;

  19. };
  20. template<class T>
  21. class BinaryTree_Thd
  22. {
  23. public:
  24.     BinaryTree_Thd()
  25.         :_root(NULL)
  26.     {}
  27.     BinaryTree_Thd(const T *a,const size_t size)
  28.     {
  29.         int index = 0;
  30.         _root = _CreatTree(a,size,index);
  31.     }
  32.     void PrevOrder_Threading()
  33.     {
  34.         BinaryTreeNode_Thd<T> *prev = NULL;
  35.         _PrevOrder_Threading(_root,prev);
  36.     }
  37.     void InOrder_Threading()
  38.     {
  39.         BinaryTreeNode_Thd<T> *prev = NULL;
  40.         _InOrder_Threading(_root,prev);
  41.     }
  42.     void PostOrder_Threading()
  43.     {
  44.         BinaryTreeNode_Thd<T> *prev = NULL;
  45.         _PostOrder_Threading(_root,prev);
  46.     }
  47.     void InOrder()
  48.     {
  49.         if(_root == NULL)
  50.         {
  51.             return;
  52.         }
  53.         BinaryTreeNode_Thd<T> *cur = _root;
  54.         while (cur)
  55.         {
  56.             while (cur->_left != NULL && cur->_leftTag==LINK)
  57.             {
  58.                 cur = cur->_left;
  59.             }
  60.             cout << cur->_data << " ";
  61.             while(cur->_rightTag == THREAD)//用while支持连跳
  62.             {
  63.                 cur = cur->_right;
  64.                 cout << cur->_data << " ";
  65.             }
  66.             cur = cur->_right;
  67.         }
  68.     }
  69.     void PreOrder()
  70.     {
  71.         if(_root == NULL)
  72.         {
  73.             return;
  74.         }
  75.         BinaryTreeNode_Thd<T> *cur = _root;
  76.         while(cur)
  77.         {
  78.             while (cur->_left != NULL && cur->_leftTag==LINK)
  79.             {
  80.                 cout<<cur->_data<<" ";
  81.                 cur = cur->_left;
  82.             }
  83.             cout<<cur->_data<<" ";
  84.             
  85.             cur = cur->_right;
  86.         }
  87.     }
  88.     void PostOrder()
  89.     {
  90.         BinaryTreeNode_Thd<T> *cur = _root;
  91.         stack<BinaryTreeNode_Thd<T> *> s;
  92.         while(cur)
  93.         {
  94.             while (cur->_right != NULL && cur->_rightTag==LINK)
  95.             {
  96.                 s.push(cur);
  97.                 cur = cur->_right;
  98.             }
  99.             s.push(cur);
  100.             cur = cur->_left;
  101.         }
  102.         while(!s.empty())
  103.         {
  104.             BinaryTreeNode_Thd<T> *tmp = s.top();
  105.             s.pop();
  106.             cout<<tmp->_data<<" ";
  107.         }
  108.     }
  109. protected:
  110.     BinaryTreeNode_Thd<T>* _CreatTree(const int *a,size_t size,int &index)
  111.     {
  112.         BinaryTreeNode_Thd<T> *root = NULL;
  113.         if(index < size && a[index] != '#')
  114.         {
  115.             root = new BinaryTreeNode_Thd<T>(a[index]);
  116.             root->_left = _CreatTree(a,size,++index);
  117.             root->_right = _CreatTree(a,size,++index);
  118.         }
  119.         return root;
  120.     }
  121.     void _PostOrder_Threading(BinaryTreeNode_Thd<T> *cur,BinaryTreeNode_Thd<T> * &prev)
  122.     {
  123.         if(cur == NULL)
  124.         {
  125.             return;
  126.         }
  127.         _PostOrder_Threading(cur->_left,prev);
  128.         _PostOrder_Threading(cur->_right,prev);
  129.         if(!cur->_left)
  130.         {
  131.             cur->_leftTag = THREAD;
  132.             cur->_left = prev;
  133.         }
  134.         if(prev && !prev->_right)
  135.         {
  136.             prev->_rightTag = THREAD;
  137.             prev->_right = cur;
  138.         }
  139.         prev = cur;
  140.     }
  141.     void _PrevOrder_Threading(BinaryTreeNode_Thd<T> *cur,BinaryTreeNode_Thd<T> * &prev)
  142.     {
  143.         if(cur == NULL)
  144.         {
  145.             return;
  146.         }
  147.         if(!cur->_left)
  148.         {
  149.             cur->_leftTag = THREAD;
  150.             cur->_left = prev;
  151.         }
  152.         if(prev && !prev->_right)
  153.         {
  154.             prev->_rightTag = THREAD;
  155.             prev->_right = cur;
  156.         }
  157.         prev = cur;

  158.         if(cur->_leftTag == LINK)
  159.         {
  160.             _PrevOrder_Threading(cur->_left,prev);
  161.         }
  162.         if(cur->_rightTag == LINK)
  163.         {
  164.             _PrevOrder_Threading(cur->_right,prev);
  165.         }
  166.     }
  167.     void _InOrder_Threading(BinaryTreeNode_Thd<T> *cur,BinaryTreeNode_Thd<T> *&prev)//记得加引用
  168.     {
  169.         if(cur == NULL)
  170.         {
  171.             return;
  172.         }
  173.         _InOrder_Threading(cur->_left,prev);
  174.         if(!cur->_left)
  175.         {
  176.             cur->_leftTag = THREAD;
  177.             cur->_left = prev;
  178.         }
  179.         if(prev && !prev->_right)
  180.         {
  181.             prev->_rightTag = THREAD;
  182.             prev->_right = cur;
  183.         }
  184.         prev = cur;
  185.         _InOrder_Threading(cur->_right,prev);
  186.     }
  187. protected:
  188.     BinaryTreeNode_Thd<T> *_root;
  189. };
测试:

  1. #include <iostream>
  2. #include "BinaryTree_ThR.h"
  3. using namespace std;

  4. void test()
  5. {
  6.     int a[10] = {1,2,3,'#','#',4,'#','#',5,6};
  7.     BinaryTree_Thd<int> t1(a,10);
  8. //     t1.InOrder_Threading();
  9. //     t1.InOrder();

  10. //    t1.PrevOrder_Threading();
  11. //    t1.PreOrder();

  12.     t1.PostOrder_Threading();
  13.     t1.PostOrder();
  14. }
  15. int main()
  16. {
  17.     test();
  18.     return 0;
  19. }


阅读(1147) | 评论(0) | 转发(0) |
0

上一篇:my very first html

下一篇:堆Heap(最小堆)

给主人留下些什么吧!~~