Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360518
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 1138
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 16:18
个人简介

最多140个字

文章分类

全部博文(60)

文章存档

2016年(1)

2015年(34)

2014年(25)

分类: C/C++

2014-04-28 19:48:40


  1. #ifndef __BINARYSEARCHTREE_H__
  2. #define __BINARYSEARCHTREE_H__

  3. //template<typename int>
  4. class BinarySearchTree
  5. {
  6.     private:
  7.         struct BinaryNode
  8.         {
  9.             int element;
  10.             BinaryNode *left;
  11.             BinaryNode *right;
  12.             BinaryNode(const int &theElement,BinaryNode *lt,BinaryNode *rt):element(theElement),left(lt),right(rt)
  13.             {
  14.             }
  15.         };
  16.         BinaryNode *root;

  17.         void insert(const int &x,BinaryNode * &t)const;
  18.         void remove(const int& x,BinaryNode *&t)const;
  19.         BinaryNode *findMin(BinaryNode *t)const;
  20.         BinaryNode *findMax(BinaryNode *t)const;
  21.         bool contains(const int &x,BinaryNode *t)const;
  22.         void makeEmpty(BinaryNode* & t);
  23.         void printTree(BinaryNode *t)const;
  24.     //    BinaryNode *clone(BinaryNode *t)const;
  25.         int deep(BinaryNode *t);
  26.     
  27.     
  28.     public:
  29.         BinarySearchTree();
  30.         BinarySearchTree(const BinarySearchTree &rhs);
  31.         ~BinarySearchTree();

  32.         const int &findMin()const;
  33.         const int &findMax()const;
  34.         bool contains(const int &x)const;
  35.         bool isEmpty()const;
  36.         void printTree()const;

  37.         void makeEmpty();
  38.         void insert(const int &x);
  39.         void remove(const int &x);
  40.     //    const BinarySearchTree& operator=(const BinarySearchTree& rhs);
  41.         int deep()
  42.         {
  43.             return deep(root);
  44.         }
  45. };

  46. #endif

点击(此处)折叠或打开

  1. #include"BinarySearchTree.h"
  2. #include<iostream>
  3. using std::cout;

  4. BinarySearchTree::BinarySearchTree():root(NULL)
  5. {
  6. }

  7. bool BinarySearchTree::contains(const int& x)const
  8. {
  9.     return contains(x,root);
  10. }

  11. bool BinarySearchTree::contains(const int &x,BinaryNode *t)const
  12. {
  13.     if(t==NULL)
  14.         return false;
  15.     else if(x < t->element)
  16.         return contains(x,t->left);
  17.     else if(x > t->element)
  18.         return contains(x,t->right);
  19.     else
  20.         return true;
  21. }

  22. void BinarySearchTree::insert(const int &x)
  23. {
  24.     insert(x,root);
  25. }

  26. void BinarySearchTree::insert(const int &x,BinaryNode *&t)const
  27. {
  28.     if(t==NULL)
  29.         t=new BinaryNode(x,NULL,NULL);
  30.     else if(x<t->element)
  31.         insert(x,t->left);
  32.     else if(x>t->element)
  33.         insert(x,t->right);
  34.     else
  35.         ;//Duplicate;do nothing。即如果x已经存在二叉树中,则插入操作终止。
  36. }

  37. void BinarySearchTree::remove(const int &x)
  38. {
  39.     remove(x,root);
  40. }

  41. void BinarySearchTree::remove(const int &x,BinaryNode * &t)const
  42. {
  43.     if(t==NULL)
  44.         return ;
  45.     if(x<t->element)
  46.         remove(x,t->left);
  47.     else if(x>t->element)
  48.         remove(x,t->right);
  49.     else if(t->left!=NULL && t->right!=NULL)
  50.     {
  51.         t->element=findMin(t->right)->element;
  52.         remove(t->element,t->right);
  53.     }
  54.     else
  55.     {
  56.         BinaryNode *oldNode=t;
  57.         t=(t->left!=NULL)? t->left : t->right;
  58.         delete oldNode;
  59.     }
  60. }

  61. BinarySearchTree::BinaryNode* BinarySearchTree::findMin(BinaryNode *t)const
  62. {
  63.     if(t==NULL)
  64.         return NULL;
  65.     if(t->left==NULL)
  66.         return t;
  67.     return findMin(t->left);
  68. }

  69. BinarySearchTree::BinaryNode* BinarySearchTree::findMax(BinaryNode *t)const
  70. {
  71.     if(t!=NULL)
  72.         while(t->right!=NULL)
  73.             t=t->right;
  74.     return t;
  75. }

  76. void BinarySearchTree::makeEmpty()
  77. {
  78.     makeEmpty(root);
  79. }
  80. void BinarySearchTree::makeEmpty(BinaryNode* &t)
  81. {
  82.     if(t!=NULL)
  83.     {
  84.         makeEmpty(t->left);
  85.         makeEmpty(t->right);
  86.         delete t;
  87.     }
  88.     t=NULL;
  89. }

  90. BinarySearchTree::~BinarySearchTree()
  91. {
  92.     makeEmpty();
  93. }

  94. void BinarySearchTree::printTree()const
  95. {
  96.     printTree(root);
  97. }
  98. void BinarySearchTree::printTree(BinaryNode *t)const
  99. {
  100.     if(t==NULL)
  101.         return ;
  102.     else
  103.         cout<<t->element<<',';
  104.     //    printf("%d",t->element);
  105.     printTree(t->left);
  106.     printTree(t->right);
  107. }


  108. //BinarySearchTree::operator =(const BinarySearchTree &rhs)
  109. //{

  110. //}
  111. int BinarySearchTree::deep(BinaryNode *t)//求二叉树的深度。
  112. {
  113.     int lm,rm;
  114.     if(t==NULL)
  115.         return -1;
  116.     lm=deep(t->left);
  117.     rm=deep(t->right);
  118.     return 1+(lm>rm? lm:rm);
  119. }


点击(此处)折叠或打开

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

  4. int main()
  5. {
  6.     BinarySearchTree bt;
  7.     bt.insert(6);
  8.     bt.insert(2);
  9.     bt.insert(1);
  10.     bt.insert(4);
  11.     bt.insert(3);
  12.     bt.insert(8);

  13.     cout<<bt.deep()<<endl;
  14.     bt.printTree();
  15.     cout<<endl;
  16.     bt.remove(2);
  17.     bt.printTree();
  18.     cout<<endl;
  19.     cout<<bt.deep()<<endl;
  20.     bt.remove(1);
  21.     bt.remove(3);
  22.     cout<<bt.deep()<<endl;
  23.     return 0;
  24. }


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