二叉树的实现

  1 #include<stack>
  2 #include<queue>
  3 #include<iostream>
  4 using namespace std;
  5 template<class T>
  6 struct BinaryTreeNode
  7 {
  8    T _data;
  9    BinaryTreeNode<T>* _left;
 10    BinaryTreeNode<T>* _right;
 11    BinaryTreeNode() : _left(NULL), _right(NULL)
 12    {}
 13    BinaryTreeNode(const T&x) :_data(x), _left(NULL), _right(NULL)
 14    {}
 15 };
 16 
 17 template<class T>
 18 class BinaryTree
 19 {
 20 public:
 21    BinaryTree() :_root(NULL)
 22    {}
 23    BinaryTree(const T*a, size_t size)
 24    {
 25       size_t index = 0;
 26       _root = _CreateTree(a, size, index);  //创建二叉树
 27    }
 28    BinaryTree(const BinaryTree<T>& t)
 29    {
 30       _root=_Copy(t._root);
 31    }
 32 
 33   void PrevOrder()  //前序遍历(递归)
 34 
 35   {   
 36 
 37     _PrevOrder(_root);  
 38 
 39   } 
 40 
 41    void PrevOrder_NonR()   //前序遍历(非递归)
 42 
 43   {   
 44 
 45     _PrevOrder_NonR(_root);
 46 
 47   }
 48 
 49   void InOrder()   //中序遍历(递归)
 50    {  
 51       _InOrder(_root);
 52   }
 53   void InOrder_NonR()    //中序遍历(非递归)
 54   {
 55       _InOrder_NonR(_root);
 56    }
 57 
 58  void PostOrder()  //后序遍历(递归)
 59 
 60    {
 61           _PostOrder(_root);
 62    }
 63   void PostOrder_NonR() //后序遍历(非递归)
 64   {
 65     _PostOrder_NonR(_root);
 66   }
 67   void LevelOrder()  //层次遍历
 68   {
 69     _LevelOrder(_root);
 70   }
 71 
 72 int size() //二叉树大小
 73  {
 74      return _size(_root);
 75  }
 76  int Depth()   //二叉树深度
 77  {
 78     return _Depth(_root);
 79  } 
 80  int GetLeafNum()  //叶子结点的个数
 81  {
 82     return _GetLeafNum(_root);
 83  }
 84  BinaryTreeNode<T>* Find(const T&x)  //在二叉树查找某个数
 85  {
 86     return _Find(_root,x);
 87  }
 88  ~BinaryTree()
 89  {
 90     _Destory(_root);
 91     _root = NULL;
 92  }
 93 
 94 protected:
 95  BinaryTreeNode<T>* _CreateTree(const T*a, size_t size, size_t &index)  
 96  {
 97     BinaryTreeNode<T>* root = NULL;
 98     if (index < size&&a[index] != '#')
 99     {
100        root = new BinaryTreeNode<T>(a[index]);
101        root->_left = _CreateTree(a, size, ++index);
102        root->_right = _CreateTree(a, size, ++index);
103     }
104     return root;
105 
106 }
107  void _Destory(BinaryTreeNode<T>*& root)
108  {
109     if (root == NULL)
110      return;
111      if (root->_left == NULL&&root->_right == NULL)
112     {
113        delete root;
114        root = NULL;
115        return;
116     }
117     _Destory(root->_left);
118     _Destory(root->_right);
119     delete root;
120  }
121 
122 void _PrevOrder(BinaryTreeNode<T> *root)
123  {
124     if (root == NULL)
125     {
126        return;
127     }
128     else
129     {
130        cout << root->_data << " ";
131        _PrevOrder(root->_left);
132        _PrevOrder(root->_right);
133     }
134  }
135 
136 void _PrevOrder_NonR(BinaryTreeNode<T>* root)  
137 
138 {   
139 
140   stack<BinaryTreeNode<T>*> s;   
141 
142   if (root);   
143 
144   s.push(root);   
145 
146   while (!s.empty())   
147 
148   {    
149 
150     BinaryTreeNode<T>* top = s.top();    
151 
152     cout << top->_data << " ";    
153 
154     s.pop();
155 
156       if (top->_right)    
157 
158     {     
159 
160        s.push(top->_right);    
161 
162     }    
163 
164     if (top->_left)   
165 
166      {   
167 
168         s.push(top->_left);   
169 
170      }   
171 
172   }
173 
174    cout << endl;  
175 
176 }
177 
178 void _InOrder(BinaryTreeNode<T> *root)
179  {
180     if (root == NULL)
181     {
182        return;
183     }
184     else
185     {
186        _InOrder(root->_left);
187        cout << root->_data << " ";
188        _InOrder(root->_right);
189     }
190  }
191 
192 void _InOrder_NonR(BinaryTreeNode<T> *root)
193  {
194     stack<BinaryTreeNode<T>*> s;
195     BinaryTreeNode<T>* cur = root;
196     while (!s.empty()||cur)
197     {
198        while (cur)
199        {
200           s.push(cur);
201           cur = cur->_left;
202        }
203        if(!s.empty())
204        {
205           BinaryTreeNode<T>* top = s.top();
206           cout << top->_data << " ";
207           s.pop();
208           cur = top->_right;
209        }
210     }
211  }
212 
213 void _PostOrder(BinaryTreeNode<T> *root) 
214 
215  {  
216 
217      if (root == NULL)   
218 
219     {    
220 
221         return;   
222 
223     }   
224 
225     else   
226 
227     {    
228 
229       _PostOrder(root->_left);   
230 
231        _PostOrder(root->_right);   
232 
233        cout << root->_data << " ";
234 
235       }  
236 
237 }
238 
239 void _PostOrder_NonR(BinaryTreeNode<T> *root)  
240 
241 {
242 
243       stack<BinaryTreeNode<T>*> s;  
244 
245      BinaryTreeNode<T>* cur = root;  
246 
247      BinaryTreeNode<T>* prevVisted = NULL;   
248 
249     while (!s.empty() || cur)   
250 
251     {    
252 
253       while (cur)    
254 
255       {     
256 
257         s.push(cur);     
258 
259         cur = cur->_left;   
260 
261        }    
262 
263       BinaryTreeNode<T>* top = s.top();   
264 
265        if (top->_right==NULL||top->_right==prevVisted)    
266 
267       {     
268 
269         cout << top->_data << " ";     
270 
271         prevVisted = top;     
272 
273         s.pop();     
274 
275       }    
276 
277       else    
278 
279       {     
280 
281         cur = top->_right;   
282 
283        }   
284 
285     }  
286 
287 }
288 
289 int _size(BinaryTreeNode<T>* root)
290  {
291       if (root == NULL)
292         return 0;
293       return _size(root->_left) + _size(root->_right) + 1;
294  }
295  int _Depth(BinaryTreeNode<T>* root)
296  {
297       if (root == NULL)
298        return 0;
299       int LeftDepth = _Depth(root->_left);
300       int RightDepth = _Depth(root->_left);
301       return  LeftDepth>RightDepth ? LeftDepth+1 : RightDepth + 1;
302  }
303 
304 int _GetLeafNum(BinaryTreeNode<T>* root)
305  {
306       if (root == NULL)
307         return 0;
308       if (root->_left == NULL&&root->_right == NULL)
309        return 1;
310       else
311       return _GetLeafNum(root->_left) + _GetLeafNum(root->_right);
312  }
313  BinaryTreeNode<T> *_Find(BinaryTreeNode<T>* root, const T&x)
314  {
315       if (root == NULL)
316         return NULL;
317       else if (root->_data == x)
318       {
319          return root;
320       }
321       else
322       {
323          BinaryTreeNode<T> *ret= _Find(root->_left, x);
324          if (ret)
325          return ret;
326          else
327          {
328             return _Find(root->_right,x);
329          }
330       }
331  }
332 
333 void _LevelOrder(BinaryTreeNode<T>* root)
334  {
335     queue<BinaryTreeNode<T>*> q;
336     if (root == NULL)
337      return;
338     q.push(root);
339     while (!q.empty())
340     {
341         BinaryTreeNode<T> *front = q.front();
342         q.pop();
343         cout << front->_data << " ";
344         if (front->_left)
345          {
346             q.push(front->_left);
347          }
348          if (front->_right)
349          {
350             q.push(front->_right);
351          }
352     }
353     cout<< endl;
354  }
355  BinaryTreeNode<T>* _Copy(BinaryTreeNode<T>* root)
356  {
357       if (root == NULL)
358        return NULL; 
359       BinaryTreeNode<T>* newRoot=new BinaryTreeNode<T>(root->_data);
360       newRoot->_left=_Copy(root->_left);
361       newRoot->_right=_Copy(root->_right);
362       return newRoot;
363  }
364 
365 BinaryTreeNode<T>& operator=(BinaryTreeNode<T>*& t)  
366 
367 {   
368 
369     if (this != &t)   
370 
371     {    
372 
373       this->_Destory(_root);    
374 
375       _root = _Copy(t._root);   
376 
377     }    
378 
379     return *this;  
380 
381 }  
382 
383 /*BinaryTreeNode<T>& operator=(BinaryTreeNode<T> t)  
384 
385 {   
386 
387     swap(_root, t._root);   
388 
389     return *this;  
390 
391 }*/
392 
393 protected:  BinaryTreeNode<T> *_root; 
394 
395 };
396 
397 int main() 
398 
399 {
400 
401     int a[10] = { 1, 2, 3, '#', '#', 4, '#','#', 5, 6 }; 
402 
403       BinaryTree<int> t(a, 10);  
404 
405     BinaryTree<int> t1(t);  
406 
407     BinaryTree<int> t2; 
408 
409      t2 = t;  
410 
411     cout << t.size() << endl;  
412 
413     cout << t.Depth() << endl;  
414 
415     cout << t1.size() << endl;  
416 
417     cout << t1.Depth() << endl;  
418 
419     cout << t2.size() << endl;  
420 
421     cout << t.Find(3)->_data << endl;  
422 
423     cout << t.Find(5)->_data << endl;  
424 
425     cout << t.Find(12) << endl;  
426 
427     cout<<t.GetLeafNum();  
428 
429     cout<< endl;    
430 
431     t.InOrder();  
432 
433     cout << endl;  
434 
435     t.InOrder_NonR();  
436 
437     cout << endl;  
438 
439     t.PostOrder(); 
440 
441      cout << endl; 
442 
443      t.PostOrder_NonR();  
444 
445     cout << endl; 
446 
447      t.PrevOrder();  
448 
449     cout << endl;  
450 
451     t.PrevOrder_NonR();  
452 
453     cout << endl;  
454 
455     t.LevelOrder();
456 
457     system("pause");  
458 
459     return 0; 
460 
461 }
462 
463  
原文地址:https://www.cnblogs.com/yuanshuang/p/5371310.html