Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8465
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-27 22:31
个人简介

风过竹林~那种感觉很神奇

文章分类

全部博文(7)

文章存档

2015年(3)

2014年(4)

我的朋友
最近访客

分类: C/C++

2015-01-08 14:29:00


点击(此处)折叠或打开

  1. // mySTLBinaryTreeNote.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"

  4. template <typename T>
  5. class BinaryTree
  6. {
  7. public:
  8.     BinaryTree();
  9.     ~BinaryTree(){makeEmpty(root);}
  10.     BinaryTree(const BinaryTree& rhs)
  11.     {
  12.         if (this == &rhs)    {    return *this;}
  13.         else this->root = rhs.root;
  14.         return this;
  15.     }

  16.     const BinaryTree& operator=(const BinaryTree& rhs)
  17.     {
  18.         if ( this != &rhs)
  19.         {
  20.             makeEmpty();
  21.             root = clone(rhs.root);
  22.         }
  23.         return *this;
  24.     }
  25.     bool contains (const T& data){    return contains(data, root);}
  26.     bool isEmpty() {return root == NULL;}
  27.     void insert (const T& data)    {    return insert(data, root);}
  28.     void remove(const T& data)    {    return remove(data, root);}

  29.     const T& findMax() const
  30.     {
  31.         BinaryNode* t = findMax(root);
  32.         return t->data;
  33.     }
  34.     const T& findMin() const
  35.     {
  36.         BinaryNode* t = findMin(root);
  37.         return t->data;
  38.     }

  39.     void printTree(ofstream& out = cout) const
  40.     {
  41.         if ( isEmpty())
  42.         {
  43.             out <<"Empty Tree"<<end;
  44.         }
  45.         else
  46.             printTree(out, root);
  47.     }
  48. private:
  49.     struct BinaryNode
  50.     {
  51.         T data;
  52.         BinaryNode* left;
  53.         BinaryNode* right;
  54.         BinaryNode(const T& data, BinaryNode* lt, BinaryNode* rt):data(data), left(lt), right(rt){}
  55.     };

  56.     BinaryNode* root;

  57.     BinaryNode* finMax(BinaryNode* t) const //递归实现
  58.     {
  59.         if ( t == NULL){return NULL;}
  60.         else if ( t->right){return t;}
  61.         findMax(t->right);
  62.     }
  63.     BinaryNode* findMin(BinaryNode* t) const // 非递归实现
  64.     {
  65.         if (t != NULL)
  66.             while(t->left != NULL) t= t->left;
  67.         return t;
  68.     }

  69.     bool contains(const T& data, BinaryNode* t ) const
  70.     {
  71.         if (t == NULL){return false;}
  72.         else if (data > t->data){return contains(data, t->right);}
  73.         else if (data < t->data){return contains( data, t->left );}
  74.         return false;
  75.     }
  76.     void insert(const T& data, BinaryNode* t)
  77.     {
  78.         if (t == NULL) {t == new BinaryTree(data, NULL, NULL);}
  79.         else if (data > t ->data){ insert(data, t->right);}
  80.         else if (data < t ->data ){ insert(data, t->left); }
  81.         else;
  82.     }
  83.     void remove(const T& data, BinaryNode* t)
  84.     {
  85.         if ( t == NULL){ return; }
  86.         else if ( data > t->data){remove(data, t->right);}
  87.         else if (data < t->data) {remove(data, t->left);}
  88.         else if (t->left != NULL && t->right != NULL)
  89.         {
  90.             t->data = findMin(t->right)->data;
  91.             remove(t->data, t->right);
  92.         }
  93.         else // 也可以选用懒惰删除法,用计数记录,而不是真正的删除
  94.         {
  95.             BinaryNode* oldNode = t;
  96.             t = (t->left !=NULL )? t->left : t->right;
  97.             delete oldNode;
  98.         }
  99.     }
  100.     void makeEmpty(BinaryNode * &t) //删除原指针,否则只是删除t的副本,虽然t的空间被删除,但t会成为野指针
  101.     {
  102.         if (t != NULL)
  103.         {
  104.             makeEmpty(t->left);
  105.             makeEmpty(t->right);
  106.             delete t;
  107.         }
  108.         t = NULL;
  109.     }
  110.     BinaryNode* clone(BinaryNode* t)
  111.     {
  112.         if (t == NULL){return t;}
  113.         return new BinaryNode(t->data, clone(t->left), clone(t->right));
  114.     }
  115.     void printTree(ofstream& out /* = cout */, BinaryNode t)
  116.     {
  117.         if (t != NULL) //中序遍历
  118.         {
  119.             printTree(out, t->left);
  120.             out << t->data <<endl;
  121.             printTree(out, t->right);
  122.         }
  123.     }
  124. };


  125. int _tmain(int argc, _TCHAR* argv[])
  126. {
  127.     return 0;
  128. }

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