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

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-03-16 15:39:52


  1. #pragma once
  2. #include <queue>
  3. template<class T>
  4. struct BinaryTreeNode
  5. {
  6.     BinaryTreeNode(const T &data)
  7.         :_data(data)
  8.         ,_left(NULL)
  9.         ,_right(NULL)
  10.     {}
  11.     T _data;
  12.     BinaryTreeNode<T> *_left;
  13.     BinaryTreeNode<T> *_right;
  14. };
  15. template<class T>
  16. class BinaryTree
  17. {
  18. public:
  19.     BinaryTree()
  20.         :_root(NULL)
  21.     {}
  22.     BinaryTree(const T* a,size_t size)
  23.     {
  24.         int index = 0;
  25.         _root = _CreatTree(a,size,index);
  26.     }
  27.     BinaryTree(const BinaryTree<T> &t)
  28.     {
  29.         _root = _Copy(t._root);
  30.     }
  31.     BinaryTree<T>& operator=( BinaryTree<T> t)
  32.     {
  33.         swap(t._root,_root);
  34.         return *this;
  35.     }
  36.     void PrevOrder()
  37.     {
  38.         _PrevOrder(_root);
  39.         cout<<endl;
  40.     }
  41.     void InOrder()
  42.     {
  43.         _InOrder(_root);
  44.         cout<<endl;
  45.     }
  46.     void PostOrder()
  47.     {
  48.         _PostOrder(_root);
  49.         cout<<endl;
  50.     }
  51.     void LevelOrder()
  52.     {
  53.         _LevelOrder(_root);
  54.         cout<<endl;
  55.     }
  56.     size_t Size()
  57.     {
  58.         return _Size(_root);
  59.     }
  60.     size_t Depth()
  61.     {
  62.         return _Depth(_root);
  63.     }
  64.     BinaryTreeNode<T>* Find(const T& x)
  65.     {
  66.         return _Find(_root, x);
  67.     }

  68.     ~BinaryTree()
  69.     {
  70.        _Destroy(_root);
  71.     }
  72. private:
  73.     BinaryTreeNode<T>*_CreatTree(const T* a,size_t size,int &index)
  74.     {
  75.         BinaryTreeNode<T> *root = NULL;
  76.         if(index < size && a[index] != '#')
  77.         {
  78.             root = new BinaryTreeNode<int>(a[index]);
  79.             root->_left = _CreatTree(a,size,++index);
  80.             root->_right = _CreatTree(a,size,++index);
  81.         }
  82.         return root;
  83.     }
  84.     BinaryTreeNode<T> *_Find(BinaryTreeNode<T> *root,const T& x)
  85.     {
  86.         if(root == NULL)
  87.         {
  88.             return NULL;
  89.         }
  90.         if(root->_data == x)
  91.         {
  92.             return root;
  93.         }
  94.         else
  95.         {
  96.             if(root->_left)
  97.             {
  98.                 BinaryTreeNode *tmp = _Find(root->_left,x);
  99.                 if(tmp)
  100.                     return tmp;
  101.                 else if(root->_right)
  102.                     return _Find(root->_right,x);
  103.         }
  104.         return NULL;
  105.     }
  106.     size_t _Size(BinaryTreeNode<T> *root)
  107.     {
  108.         if(root == NULL)
  109.         {
  110.             return 0;
  111.         }
  112.         return 1+_Size(root->_left)+_Size(root->_right);
  113.     }
  114.     size_t _Depth(BinaryTreeNode<T> *root)
  115.     {
  116.         if(root == NULL)
  117.         {
  118.             return 0;
  119.         }
  120.         size_t left = _Depth(root->_left);
  121.         size_t right = _Depth(root->_right);
  122.         if(left < right)
  123.         {
  124.             return 1+right;
  125.         }
  126.         return 1+left;
  127.     }
  128.     void _PrevOrder(BinaryTreeNode<T> *root)
  129.     {
  130.         if(root == NULL)
  131.         {
  132.             return;
  133.         }
  134.         cout<<root->_data<<" ";
  135.         _PrevOrder(root->_left);
  136.         _PrevOrder(root->_right);
  137.     }
  138.     void _InOrder(BinaryTreeNode<T> *root)
  139.     {
  140.         if(root == NULL)
  141.         {
  142.             return;
  143.         }
  144.         _InOrder(root->_left);
  145.         cout<<root->_data<<" ";
  146.         _InOrder(root->_right);
  147.     }
  148.     void _PostOrder(BinaryTreeNode<T> *root)
  149.     {
  150.         if(root == NULL)
  151.         {
  152.             return;
  153.         }
  154.         _PostOrder(root->_left);
  155.         _PostOrder(root->_right);
  156.         cout<<root->_data<<" ";
  157.     }
  158.     void _LevelOrder(BinaryTreeNode<T> *root)
  159.     {
  160.         if(root == NULL)
  161.         {
  162.             return;
  163.         }
  164.         queue<BinaryTreeNode<T> *> q;
  165.         q.push(root);
  166.         while (!q.empty())
  167.         {
  168.             BinaryTreeNode<T> *tmp = q.front();
  169.             q.pop();
  170.             cout << tmp->_data << " ";
  171.             if (tmp->_left)
  172.             {
  173.                 q.push(tmp->_left);
  174.             }
  175.             if (tmp->_right)
  176.             {
  177.                 q.push(tmp->_right);
  178.             }
  179.         }
  180.     }
  181.     BinaryTreeNode<T>* _Copy(BinaryTreeNode<T> *root)
  182.     {
  183.         BinaryTreeNode<T> *Newroot = NULL;
  184.      if(root)
  185.         {
  186.             Newroot = new BinaryTreeNode<T>(root->_data);
  187.             Newroot->_left = _Copy(root->_left);
  188.             Newroot->_right = _Copy(root->_right);
  189.         }
  190.         return Newroot;
  191.     }
  192.     void _Destroy(BinaryTreeNode<T> *&root)
  193.     {
  194.         if(root == NULL)
  195.         {
  196.             return;
  197.         }
  198.             _Destroy(root->_left);
  199.             _Destroy(root->_right);
  200.             delete root;
  201.     }
  202. private:
  203.     BinaryTreeNode<T> *_root;
  204. };
 测试:

  1. void test()
  2. {
  3.     int a[10] = {1,2,3,'#','#',4,'#','#',5,6};
  4.     BinaryTree<int> t1(a,10);
  5.     BinaryTree<int> t2;

  6.     t2 = t1;

  7.     t1.PrevOrder();
  8.     t1.InOrder();
  9.     t1.PostOrder();
  10.     t1.LevelOrder();

  11.     t2.PostOrder();

  12.     cout<<t2.Find(2)->_data<<endl;
  13. }


阅读(915) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~