二叉查找树

  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. template<typename object>
  5. class less
  6. {
  7. public:
  8.     bool operator()(object lhs,object rhs)
  9.     {
  10.         return lhs < rhs;
  11.     }
  12. };
  13. template<typename object,typename comparator = less<object> >
  14. class BinarySearchTree
  15. {
  16.     struct BinaryNode
  17.     {
  18.         object element;
  19.         BinaryNode* left;
  20.         BinaryNode* right;
  21.     };
  22. public:
  23.     BinarySearchTree()
  24.     {
  25.         
  26.     }
  27.     ~BinarySearchTree()
  28.     {
  29.         //todo:
  30.     }
  31.     BinaryNode* findMin() const;
  32.     BinaryNode* findMax() const;
  33.     bool contains(const object& x) const;
  34.     void printTree(ostream& out = cout);
  35. private:
  36.     
  37.     BinaryNode* root;
  38.     comparator isLessThan;
  39.     bool contains(const object& x,BinaryNode* t) const
  40.     BinaryNode* findMin(BinaryNode* t) const
  41.     {
  42.         if (t == NULL)
  43.         {
  44.             return NULL;
  45.         }
  46.         if (t->left == NULL)
  47.         {
  48.             return t;
  49.         }
  50.         
  51.         return findMin(t->left);
  52.     }
  53.     BinaryNode* findMax(BinaryNode* t) const
  54.     {
  55.         if (t == NULL)
  56.         {
  57.             return NULL;
  58.         }
  59.         while(t->right != NULL)
  60.         {
  61.             t = t->right;
  62.         }
  63.         return t;
  64.     }
  65.     
  66.     void insert(const object& x,BinaryNode* t)
  67.     {
  68.         if (t == NULL)
  69.         {
  70.             t = new BinaryNode;
  71.             t->element = x;
  72.             t->left = NULL;
  73.             t->right = NULL;
  74.         }
  75.         else if (isLessThan(x,t->element))
  76.         {
  77.             insert(x,t->left);
  78.         }
  79.         else if (isLessThan(t->element, x))
  80.         {
  81.             insert(x,t->right);
  82.         }
  83.     }
  84.     void remove(const object& x,BinaryNode* t)
  85.     {
  86.         if (t == NULL)
  87.             return;
  88.         if (isLessThan(x,t->element))
  89.         {
  90.             remove(x,t->left);
  91.         }
  92.         else if (isLessThan(t->element,x))
  93.         {
  94.             remove(x,t->element);
  95.         }
  96.         else if (t->left != NULL && t->right != NULL)
  97.         {
  98.             t->element = findMin(t->right)->element;
  99.             remove(t->element, t->right);
  100.         }
  101.         else
  102.         {
  103.             BinaryNode* p = t;
  104.             t = (t->left !=NULL) ? t->left : t->right;
  105.             delete p;
  106.         }
  107.     }
  108.     void printTree(BinaryNode* t,ostream& out)
  109.     {
  110.         if (t != NULL)
  111.         {
  112.             printTree(t->left, out);
  113.             out << t->element << endl;
  114.             printTree(t->right, out);
  115.         }
  116.     }
  117.     void height(BinaryNode* t)
  118.     {
  119.         if (t == NULL)
  120.             return 0;
  121.         
  122.         if (t->left != NULL || t->right != NULL)
  123.             return 1 + max(height(t->left),height(t->right));
  124.     }
  125. };
  126. template<typename object,typename comparator>
  127. bool BinarySearchTree<object,comparator>::contains(const object& x,BinaryNode* t) const
  128. {
  129.     if (t == NULL)
  130.     {
  131.         return false;
  132.     }
  133.     
  134.     if (isLessThan(x,t->element))
  135.     {
  136.         return contains(x,t->left);
  137.     }
  138.     else if(isLessThan(t->element,x))
  139.     {
  140.         return contains(x,t->right);
  141.     }
  142.     else
  143.     {
  144.         return true;
  145.     }
  146.     return false;
  147. }
  148. template<typename object, typename comparator>
  149. bool BinarySearchTree<object, comparator>::contains(const object& x) const
  150. {
  151.     return contains(x,t);
  152. }
  153. template<typename object, typename comparator>
  154. void BinarySearchTree<object, comparator>::printTree(ostream& out)
  155. {
  156.     if (t == NULL)
  157.     {
  158.         out << "empty tree" << endl;
  159.         return;
  160.     }
  161.     printTree(t,out);
  162. }

未完待续。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值