二叉树的查找

二叉查找树的一些实现,一些心得都嵌入其中的注释

[cpp]  view plain copy
  1. #include "iostream"  
  2. using namespace std;  
  3.   
  4. //因为查找树左小右大  
  5. template <class T>  
  6. class BinarySearchTree  
  7. {  
  8. public:  
  9.     BinarySearchTree();    //构造  
  10.     BinarySearchTree(const BinarySearchTree &rhs);   //复制构造  
  11.     ~BinarySearchTree();  
  12.   
  13.     const BinarySearchTree &operator=(const BinarySearchTree &rhs);  
  14.   
  15.     const T &findMin()const;   //找打最小element那个结点及其值  
  16.     const T &findMax()const;   //找打最大element那个结点及其值  
  17.     bool contains(const T &x);  //其中有无此element的结点,有返回true,否则返回false;  
  18.     bool isempty() const;      //判断是否为空  
  19.     void printTree() const;    //打印树,遍历显示  
  20.   
  21.     void makeEmpty();          //置空  
  22.     void insert(const T &x);    //插入结点  
  23.     void remove(const T &x);   //删除结点  
  24.   
  25. private:  
  26.     struct BinaryNode  
  27.     {  
  28.         T element;  
  29.         BinaryNode *leftChild;  
  30.         BinaryNode *rightChild;  
  31.           
  32.         BinaryNode(const T &elem, BinaryNode *lt, BinaryNode *rt)          
  33.             :element(elem), leftChild(lt), rightChild(rt){}              //结点复制构造时使用  
  34.     };  
  35.   
  36.     BinaryNode *root;  
  37.   
  38.     void insert(const T &x, BinaryNode * &root) const;  //指向引用的指针  
  39.     void remove(const T &x, BinaryNode * &root) const;   //当要进行内存分配的时候,利用new 或者malloc 时用 "* &t"  
  40.   
  41.     BinaryNode *findMin(BinaryNode *root) const;  
  42.     BinaryNode *findMax(BinaryNode *root) const;  
  43.   
  44.     bool contains(const T &x, BinaryNode *root) const;  
  45.     void makeEmpty(BinaryNode * &root);  
  46.     void printTree(BinaryNode * root) const;  
  47.     BinaryNode *clone(BinaryNode *root) const;    //复制返回的根节点  
  48. };  
  49.   
  50. template <class T>  
  51. BinarySearchTree<T>::BinarySearchTree()  
  52. {  
  53.     root = NULL;  
  54. }  
  55.   
  56. template <class T>  
  57. BinarySearchTree<T>::BinarySearchTree(const BinarySearchTree &rhs)  
  58. {  
  59.     operator=(rhs);  
  60. }  
  61.   
  62. template <class T>  
  63. BinarySearchTree<T>::~BinarySearchTree()  
  64. {  
  65.     //delete root;  
  66. }  
  67.   
  68. template <class T>  
  69. const BinarySearchTree<T> &BinarySearchTree<T>::operator=(const BinarySearchTree &rhs)  
  70. {  
  71.     if(this = &this)  
  72.         return *this;  
  73.     delete root;  
  74.     root = new BinaryNode;  
  75.     root = rhs.root;  
  76.       
  77.     return *this;  
  78. }  
  79.   
  80. template <class T>  
  81. bool BinarySearchTree<T>::isempty() const  
  82. {  
  83.     if(!root)  
  84.         return true;  
  85.     else  
  86.         return false;  
  87. }  
  88.   
  89. template <class T>  
  90. const T &BinarySearchTree<T>::findMin()const  
  91. {  
  92.     T _min;  
  93.     BinaryNode *minNode = new BinaryNode;  
  94.     minNode = findMin(root);  
  95.     _min = minNode.element;  
  96.       
  97.     return _min;  
  98. }  
  99.   
  100. template <class T>  
  101. const T &BinarySearchTree<T>::findMax()const  
  102. {  
  103.     T _max;  
  104.     BinaryNode *maxNode = new BinaryNode;  
  105.     maxNode = findMax(maxNode);  
  106.     _max = maxNode.element;  
  107.       
  108.     return _max;  
  109. }  
  110.   
  111.   
  112. //声明这个是类型typename  
  113. template <class T>  
  114. typename BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMin(BinaryNode *root) const  
  115. {  
  116.     if(root)  
  117.     {  
  118.         if(root->leftChild)  
  119.         {  
  120.             return findMin(root->leftChild);  
  121.         }  
  122.         else   
  123.             return root;  
  124.     }  
  125.     else  
  126.         return root;  
  127. }  
  128.   
  129. template <class T>  
  130. typename BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMax(BinaryNode *root) const  
  131. {  
  132.     if(root)  
  133.     {  
  134.         if(root->rightChild)  
  135.         {  
  136.             return findMax(root->rightChild);   //返回这个小的结点  
  137.         }  
  138.         else   
  139.             return root;  
  140.     }  
  141.     else  
  142.         return root;  
  143. }  
  144.   
  145. template <class T>  
  146. bool BinarySearchTree<T>::contains(const T &x)  
  147. {  
  148.     return contains(x, root);  
  149.       
  150. }  
  151.   
  152. template <class T>  
  153. bool BinarySearchTree<T>::contains(const T &x, BinaryNode *root) const  
  154. {  
  155.     if(!root)  
  156.         return false;  
  157.     else if(x < root->element)  
  158.         return contains(x, root->leftChild);  
  159.     else if(x > root->element)  
  160.         return contains(x, root->rightChild);  
  161.     else  
  162.         return true;  
  163. }  
  164.   
  165. template <class T>  
  166. void BinarySearchTree<T>::insert(const T &x)  
  167. {  
  168.     insert(x, root);  
  169. }  
  170.   
  171. template <class T>  
  172. void BinarySearchTree<T>::insert(const T &x, BinaryNode * &root) const  //指向引用的指针  
  173. {  
  174.     if(!root)  
  175.     {  
  176.         root = new BinaryNode(x, NULL, NULL);   //那个在结点中构造函数的作用,哈哈  
  177.     }  
  178.     if(root && x !=root->element)  
  179.     {  
  180.         if(x < root->element)  
  181.             insert(x, root->leftChild);  
  182.         else  
  183.             insert(x, root->rightChild);  
  184.     }  
  185.     else  
  186.     return;  
  187. }  
  188.   
  189. template <class T>  
  190. void BinarySearchTree<T>::remove(const T &x)  
  191. {  
  192.     remove(x, root);  
  193.   
  194. }  
  195.   
  196. //删除,要的那个结点,若左存在,从他的作结点中找出中序遍历的最后一个结点,替换这个;若左不存在,则用右替换  
  197. template <class T>  
  198. void BinarySearchTree<T>::remove(const T &x, BinaryNode * &root) const   //当要进行内存分配的时候,利用new 或者malloc 时用 "* &t"  
  199. {  
  200.     if(!root)  
  201.         return;  
  202.     else  
  203.     {  
  204.         if(x > root->element)  
  205.         {  
  206.             remove(x, root->rightChild);  
  207.         }  
  208.         else if(x < root->element)  
  209.             remove(x, root->leftChild);  
  210.         else if(NULL != root->leftChild && NULL != root->rightChild)  
  211.         {  
  212.             root->element = findMin(root->rightChild)->element;  
  213.             remove(root->element, root->rightChild);  
  214.         }  
  215.         else  
  216.         {  
  217.             BinaryNode *oldNode = root;  
  218.             root = (root->leftChild != NULL) ? root->leftChild : root->rightChild;  
  219.             delete oldNode;  
  220.         }     
  221.     }  
  222. }  
  223.   
  224. template <class T>  
  225. void BinarySearchTree<T>::makeEmpty()  
  226. {  
  227.     makeEmpty(root);  
  228. }  
  229.   
  230. template <class T>  
  231. void BinarySearchTree<T>::makeEmpty(BinaryNode * &root)  
  232. {  
  233.     if(!root)  
  234.         return;  
  235.     else  
  236.     {  
  237.         if(root->leftChild)  
  238.             makeEmpty(root->leftChild);  
  239.         if(root->rightChild)  
  240.             makeEmpty(root->rightChild);  
  241.         delete root;  
  242.     }  
  243. }  
  244.   
  245. int main()  
  246. {  
  247.     BinarySearchTree<int> binaryTree;  
  248.     binaryTree.insert(10);  
  249.     binaryTree.insert(5);  
  250.     binaryTree.insert(6);  
  251.     binaryTree.insert(12);  
  252.     binaryTree.insert(15);  
  253.     binaryTree.remove(6);  
  254.     cout << binaryTree.contains(12) << endl;  
  255.     cout << binaryTree.isempty() << endl;  
  256.   
  257.     binaryTree.makeEmpty();    //注意在这里已经delete root一次, 那么在析构中就不应该delete 一次了  
  258.   
  259.     return 0;  
  260. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值