• 二叉查找树(二)- C++实现


    上一章介绍了"二叉查找树的相关理论知识,并通过C语言实现了二叉查找树"。这一章给出二叉查找树的C++版本。这里不再对树的相关概念进行介绍,若遇到不明白的概念,可以在上一章查找。

    目录
    1. 二叉树查找树
    2. 二叉查找树的C++实现
    3. 二叉查找树的C++实现(完整源码)
    4. 二叉查找树的C++测试程序

    转载请注明出处:二叉查找树(二)之 C++的实现 - 如果天空不死 - 博客园


    更多内容: 数据结构与算法系列 目录 

    (01) 二叉查找树(一)之 图文解析 和 C语言的实现
    (02) 二叉查找树(二)之 C++的实现
    (03) 二叉查找树(三)之 Java的实现

    二叉查找树简介

    二叉查找树(Binary Search Tree),又被称为二叉搜索树。
    它是特殊的二叉树:对于二叉树,假设x为二叉树中的任意一个结点,x节点包含关键字key,节点x的key值记为key[x]。如果y是x的左子树中的一个结点,则key[y] <= key[x];如果y是x的右子树的一个结点,则key[y] >= key[x]。那么,这棵树就是二叉查找树。如下图所示:

    在二叉查找树中:
    (01) 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
    (02) 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
    (03) 任意节点的左、右子树也分别为二叉查找树。
    (04) 没有键值相等的节点(no duplicate nodes)。

    二叉查找树的C++实现

    1. 节点和二叉查找树的定义

    1.1 二叉查找树节点

    template <class T>
    class BSTNode{
        public:
            T key;            // 关键字(键值)
            BSTNode *left;    // 左孩子
            BSTNode *right;    // 右孩子
            BSTNode *parent;// 父结点
    
            BSTNode(T value, BSTNode *p, BSTNode *l, BSTNode *r):
                key(value),parent(),left(l),right(r) {}
    };

    BSTNode是二叉查找树的节点,它包含二叉查找树的几个基本信息:
    (01) key -- 它是关键字,是用来对二叉查找树的节点进行排序的。
    (02) left -- 它指向当前节点的左孩子。
    (03) right -- 它指向当前节点的右孩子。
    (04) parent -- 它指向当前节点的父结点。

    1.2 二叉树操作

    template <class T>
    class BSTree {
        private:
            BSTNode<T> *mRoot;    // 根结点
    
        public:
            BSTree();
            ~BSTree();
    
            // 前序遍历"二叉树"
            void preOrder();
            // 中序遍历"二叉树"
            void inOrder();
            // 后序遍历"二叉树"
            void postOrder();
    
            // (递归实现)查找"二叉树"中键值为key的节点
            BSTNode<T>* search(T key);
            // (非递归实现)查找"二叉树"中键值为key的节点
            BSTNode<T>* iterativeSearch(T key);
    
            // 查找最小结点:返回最小结点的键值。
            T minimum();
            // 查找最大结点:返回最大结点的键值。
            T maximum();
    
            // 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。
            BSTNode<T>* successor(BSTNode<T> *x);
            // 找结点(x)的前驱结点。即,查找"二叉树中数据值小于该结点"的"最大结点"。
            BSTNode<T>* predecessor(BSTNode<T> *x);
    
            // 将结点(key为节点键值)插入到二叉树中
            void insert(T key);
    
            // 删除结点(key为节点键值)
            void remove(T key);
    
            // 销毁二叉树
            void destroy();
    
            // 打印二叉树
            void print();
        private:
            // 前序遍历"二叉树"
            void preOrder(BSTNode<T>* tree) const;
            // 中序遍历"二叉树"
            void inOrder(BSTNode<T>* tree) const;
            // 后序遍历"二叉树"
            void postOrder(BSTNode<T>* tree) const;
    
            // (递归实现)查找"二叉树x"中键值为key的节点
            BSTNode<T>* search(BSTNode<T>* x, T key) const;
            // (非递归实现)查找"二叉树x"中键值为key的节点
            BSTNode<T>* iterativeSearch(BSTNode<T>* x, T key) const;
    
            // 查找最小结点:返回tree为根结点的二叉树的最小结点。
            BSTNode<T>* minimum(BSTNode<T>* tree);
            // 查找最大结点:返回tree为根结点的二叉树的最大结点。
            BSTNode<T>* maximum(BSTNode<T>* tree);
    
            // 将结点(z)插入到二叉树(tree)中
            void insert(BSTNode<T>* &tree, BSTNode<T>* z);
    
            // 删除二叉树(tree)中的结点(z),并返回被删除的结点
            BSTNode<T>* remove(BSTNode<T>* &tree, BSTNode<T> *z);
    
            // 销毁二叉树
            void destroy(BSTNode<T>* &tree);
    
            // 打印二叉树
            void print(BSTNode<T>* tree, T key, int direction);
    };

    BSTree是二叉树。它包含二叉查找树的根节点和二叉查找树的操作。二叉查找树的操作中有许多重载函数,例如insert()函数,其中一个是内部接口,另一个是提供给外部的接口。

    2 遍历

    这里讲解前序遍历、中序遍历、后序遍历3种方式。

    2.1 前序遍历
    若二叉树非空,则执行以下操作:
    (01) 访问根结点;
    (02) 先序遍历左子树;
    (03) 先序遍历右子树。

    前序遍历代码

    template <class T>
    void BSTree<T>::preOrder(BSTNode<T>* tree) const
    {
        if(tree != NULL)
        {
            cout<< tree->key << " " ;
            preOrder(tree->left);
            preOrder(tree->right);
        }
    }
    
    template <class T>
    void BSTree<T>::preOrder() 
    {
        preOrder(mRoot);
    }

    2.2 中序遍历

    若二叉树非空,则执行以下操作:
    (01) 中序遍历左子树;
    (02) 访问根结点;
    (03) 中序遍历右子树。

    中序遍历代码

    template <class T>
    void BSTree<T>::inOrder(BSTNode<T>* tree) const
    {
        if(tree != NULL)
        {
            inOrder(tree->left);
            cout<< tree->key << " " ;
            inOrder(tree->right);
        }
    }
    
    template <class T>
    void BSTree<T>::inOrder() 
    {
        inOrder(mRoot);
    }

    2.3 后序遍历

    若二叉树非空,则执行以下操作:
    (01) 后序遍历左子树;
    (02) 后序遍历右子树;
    (03) 访问根结点。

    后序遍历代码

    template <class T>
    void BSTree<T>::postOrder(BSTNode<T>* tree) const
    {
        if(tree != NULL)
        {
            postOrder(tree->left);
            postOrder(tree->right);
            cout<< tree->key << " " ;
        }
    }
    
    template <class T>
    void BSTree<T>::postOrder() 
    {
        postOrder(mRoot);
    }

    看看下面这颗树的各种遍历方式:

    对于上面的二叉树而言,
    (01) 前序遍历结果: 3 1 2 5 4 6
    (02) 中序遍历结果: 1 2 3 4 5 6 
    (03) 后序遍历结果: 2 1 4 6 5 3

    3. 查找

    递归版本的代码

    template <class T>
    BSTNode<T>* BSTree<T>::search(BSTNode<T>* x, T key) const
    {
        if (x==NULL || x->key==key)
            return x;
    
        if (key < x->key)
            return search(x->left, key);
        else
            return search(x->right, key);
    }
    
    template <class T>
    BSTNode<T>* BSTree<T>::search(T key) 
    {
        search(mRoot, key);
    }

    非递归版本的代码

    template <class T>
    BSTNode<T>* BSTree<T>::iterativeSearch(BSTNode<T>* x, T key) const
    {
        while ((x!=NULL) && (x->key!=key))
        {
            if (key < x->key)
                x = x->left;
            else
                x = x->right;
        }
    
        return x;
    }
    
    template <class T>
    BSTNode<T>* BSTree<T>::iterativeSearch(T key)
    {
        iterativeSearch(mRoot, key);
    }


    4. 最大值和最小值

    查找最大值的代码

    template <class T>
    BSTNode<T>* BSTree<T>::maximum(BSTNode<T>* tree)
    {
        if (tree == NULL)
            return NULL;
    
        while(tree->right != NULL)
            tree = tree->right;
        return tree;
    }
    
    template <class T>
    T BSTree<T>::maximum()
    {
        BSTNode<T> *p = maximum(mRoot);
        if (p != NULL)
            return p->key;
    
        return (T)NULL;
    }

    查找最小值的代码

    template <class T>
    BSTNode<T>* BSTree<T>::minimum(BSTNode<T>* tree)
    {
        if (tree == NULL)
            return NULL;
    
        while(tree->left != NULL)
            tree = tree->left;
        return tree;
    }
    
    template <class T>
    T BSTree<T>::minimum()
    {
        BSTNode<T> *p = minimum(mRoot);
        if (p != NULL)
            return p->key;
    
        return (T)NULL;
    }

    5. 前驱和后继

    节点的前驱:是该节点的左子树中的最大节点。
    节点的后继:是该节点的右子树中的最小节点。

    查找前驱节点的代码

    /* 
     * 找结点(x)的前驱结点。即,查找"二叉树中数据值小于该结点"的"最大结点"。
     */
    template <class T>
    BSTNode<T>* BSTree<T>::predecessor(BSTNode<T> *x)
    {
        // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。
        if (x->left != NULL)
            return maximum(x->left);
    
        // 如果x没有左孩子。则x有以下两种可能:
        // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
        // (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。
        BSTNode<T>* y = x->parent;
        while ((y!=NULL) && (x==y->left))
        {
            x = y;
            y = y->parent;
        }
    
        return y;
    }

    查找后继节点的代码

    /* 
     * 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。
     */
    template <class T>
    BSTNode<T>* BSTree<T>::successor(BSTNode<T> *x)
    {
        // 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。
        if (x->right != NULL)
            return minimum(x->right);
    
        // 如果x没有右孩子。则x有以下两种可能:
        // (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。
        // (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。
        BSTNode<T>* y = x->parent;
        while ((y!=NULL) && (x==y->right))
        {
            x = y;
            y = y->parent;
        }
    
        return y;
    }


    6. 插入

    插入节点的代码

    /* 
     * 将结点插入到二叉树中
     *
     * 参数说明:
     *     tree 二叉树的根结点
     *     z 插入的结点
     */
    template <class T>
    void BSTree<T>::insert(BSTNode<T>* &tree, BSTNode<T>* z)
    {
        BSTNode<T> *y = NULL;
        BSTNode<T> *x = tree;
    
        // 查找z的插入位置
        while (x != NULL)
        {
            y = x;
            if (z->key < x->key)
                x = x->left;
            else
                x = x->right;
        }
    
        z->parent = y;
        if (y==NULL)
            tree = z;
        else if (z->key < y->key)
            y->left = z;
        else
            y->right = z;
    }
    
    /* 
     * 将结点(key为节点键值)插入到二叉树中
     *
     * 参数说明:
     *     tree 二叉树的根结点
     *     key 插入结点的键值
     */
    template <class T>
    void BSTree<T>::insert(T key)
    {
        BSTNode<T> *z=NULL;
    
        // 如果新建结点失败,则返回。
        if ((z=new BSTNode<T>(key,NULL,NULL,NULL)) == NULL)
            return ;
    
        insert(mRoot, z);
    }

    注:本文实现的二叉查找树是允许插入相同键值的节点的。若想禁止二叉查找树中插入相同键值的节点,可以参考"二叉查找树(一)之 图文解析 和 C语言的实现"中的插入函数进行修改。

    7. 删除

    删除节点的代码

    /* 
     * 删除结点(z),并返回被删除的结点
     *
     * 参数说明:
     *     tree 二叉树的根结点
     *     z 删除的结点
     */
    template <class T>
    BSTNode<T>* BSTree<T>::remove(BSTNode<T>* &tree, BSTNode<T> *z)
    {
        BSTNode<T> *x=NULL;
        BSTNode<T> *y=NULL;
    
        if ((z->left == NULL) || (z->right == NULL) )
            y = z;
        else
            y = successor(z);
    
        if (y->left != NULL)
            x = y->left;
        else
            x = y->right;
    
        if (x != NULL)
            x->parent = y->parent;
    
        if (y->parent == NULL)
            tree = x;
        else if (y == y->parent->left)
            y->parent->left = x;
        else
            y->parent->right = x;
    
        if (y != z) 
            z->key = y->key;
    
        return y;
    }
    
    /* 
     * 删除结点(z),并返回被删除的结点
     *
     * 参数说明:
     *     tree 二叉树的根结点
     *     z 删除的结点
     */
    template <class T>
    void BSTree<T>::remove(T key)
    {
        BSTNode<T> *z, *node; 
    
        if ((z = search(mRoot, key)) != NULL)
            if ( (node = remove(mRoot, z)) != NULL)
                delete node;
    }

    8. 打印

    打印二叉查找树的代码

    /*
     * 打印"二叉查找树"
     *
     * key        -- 节点的键值 
     * direction  --  0,表示该节点是根节点;
     *               -1,表示该节点是它的父结点的左孩子;
     *                1,表示该节点是它的父结点的右孩子。
     */
    template <class T>
    void BSTree<T>::print(BSTNode<T>* tree, T key, int direction)
    {
        if(tree != NULL)
        {
            if(direction==0)    // tree是根节点
                cout << setw(2) << tree->key << " is root" << endl;
            else                // tree是分支节点
                cout << setw(2) << tree->key << " is " << setw(2) << key << "'s "  << setw(12) << (direction==1?"right child" : "left child") << endl;
    
            print(tree->left, tree->key, -1);
            print(tree->right,tree->key,  1);
        }
    }
    
    template <class T>
    void BSTree<T>::print()
    {
        if (mRoot != NULL)
            print(mRoot, mRoot->key, 0);
    }

    9. 销毁

    销毁二叉查找树的代码

    /*
     * 销毁二叉树
     */
    template <class T>
    void BSTree<T>::destroy(BSTNode<T>* &tree)
    {
        if (tree==NULL)
            return ;
    
        if (tree->left != NULL)
            return destroy(tree->left);
        if (tree->right != NULL)
            return destroy(tree->right);
    
        delete tree;
        tree=NULL;
    }
    
    template <class T>
    void BSTree<T>::destroy()
    {
        destroy(mRoot);
    }

    二叉查找树的C++实现(完整源码)

    二叉查找树的C++实现文件(BSTree.h)

    1. /**
    2. * C++ 语言: 二叉查找树
    3. *
    4. * @author skywang
    5. * @date 2013/11/07
    6. */
    7. #ifndef _BINARY_SEARCH_TREE_HPP_
    8. #define _BINARY_SEARCH_TREE_HPP_
    9. #include <iomanip>
    10. #include <iostream>
    11. using namespace std;
    12. template <class T>
    13. class BSTNode{
    14. public:
    15. T key; // 关键字(键值)
    16. BSTNode *left; // 左孩子
    17. BSTNode *right; // 右孩子
    18. BSTNode *parent;// 父结点
    19. BSTNode(T value, BSTNode *p, BSTNode *l, BSTNode *r):
    20. key(value),parent(),left(l),right(r) {}
    21. };
    22. template <class T>
    23. class BSTree {
    24. private:
    25. BSTNode<T> *mRoot; // 根结点
    26. public:
    27. BSTree();
    28. ~BSTree();
    29. // 前序遍历"二叉树"
    30. void preOrder();
    31. // 中序遍历"二叉树"
    32. void inOrder();
    33. // 后序遍历"二叉树"
    34. void postOrder();
    35. // (递归实现)查找"二叉树"中键值为key的节点
    36. BSTNode<T>* search(T key);
    37. // (非递归实现)查找"二叉树"中键值为key的节点
    38. BSTNode<T>* iterativeSearch(T key);
    39. // 查找最小结点:返回最小结点的键值。
    40. T minimum();
    41. // 查找最大结点:返回最大结点的键值。
    42. T maximum();
    43. // 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。
    44. BSTNode<T>* successor(BSTNode<T> *x);
    45. // 找结点(x)的前驱结点。即,查找"二叉树中数据值小于该结点"的"最大结点"。
    46. BSTNode<T>* predecessor(BSTNode<T> *x);
    47. // 将结点(key为节点键值)插入到二叉树中
    48. void insert(T key);
    49. // 删除结点(key为节点键值)
    50. void remove(T key);
    51. // 销毁二叉树
    52. void destroy();
    53. // 打印二叉树
    54. void print();
    55. private:
    56. // 前序遍历"二叉树"
    57. void preOrder(BSTNode<T>* tree) const;
    58. // 中序遍历"二叉树"
    59. void inOrder(BSTNode<T>* tree) const;
    60. // 后序遍历"二叉树"
    61. void postOrder(BSTNode<T>* tree) const;
    62. // (递归实现)查找"二叉树x"中键值为key的节点
    63. BSTNode<T>* search(BSTNode<T>* x, T key) const;
    64. // (非递归实现)查找"二叉树x"中键值为key的节点
    65. BSTNode<T>* iterativeSearch(BSTNode<T>* x, T key) const;
    66. // 查找最小结点:返回tree为根结点的二叉树的最小结点。
    67. BSTNode<T>* minimum(BSTNode<T>* tree);
    68. // 查找最大结点:返回tree为根结点的二叉树的最大结点。
    69. BSTNode<T>* maximum(BSTNode<T>* tree);
    70. // 将结点(z)插入到二叉树(tree)中
    71. void insert(BSTNode<T>* &tree, BSTNode<T>* z);
    72. // 删除二叉树(tree)中的结点(z),并返回被删除的结点
    73. BSTNode<T>* remove(BSTNode<T>* &tree, BSTNode<T> *z);
    74. // 销毁二叉树
    75. void destroy(BSTNode<T>* &tree);
    76. // 打印二叉树
    77. void print(BSTNode<T>* tree, T key, int direction);
    78. };
    79. /*
    80. * 构造函数
    81. */
    82. template <class T>
    83. BSTree<T>::BSTree():mRoot(NULL)
    84. {
    85. }
    86. /*
    87. * 析构函数
    88. */
    89. template <class T>
    90. BSTree<T>::~BSTree()
    91. {
    92. destroy();
    93. }
    94. /*
    95. * 前序遍历"二叉树"
    96. */
    97. template <class T>
    98. void BSTree<T>::preOrder(BSTNode<T>* tree) const
    99. {
    100. if(tree != NULL)
    101. {
    102. cout<< tree->key << " " ;
    103. preOrder(tree->left);
    104. preOrder(tree->right);
    105. }
    106. }
    107. template <class T>
    108. void BSTree<T>::preOrder()
    109. {
    110. preOrder(mRoot);
    111. }
    112. /*
    113. * 中序遍历"二叉树"
    114. */
    115. template <class T>
    116. void BSTree<T>::inOrder(BSTNode<T>* tree) const
    117. {
    118. if(tree != NULL)
    119. {
    120. inOrder(tree->left);
    121. cout<< tree->key << " " ;
    122. inOrder(tree->right);
    123. }
    124. }
    125. template <class T>
    126. void BSTree<T>::inOrder()
    127. {
    128. inOrder(mRoot);
    129. }
    130. /*
    131. * 后序遍历"二叉树"
    132. */
    133. template <class T>
    134. void BSTree<T>::postOrder(BSTNode<T>* tree) const
    135. {
    136. if(tree != NULL)
    137. {
    138. postOrder(tree->left);
    139. postOrder(tree->right);
    140. cout<< tree->key << " " ;
    141. }
    142. }
    143. template <class T>
    144. void BSTree<T>::postOrder()
    145. {
    146. postOrder(mRoot);
    147. }
    148. /*
    149. * (递归实现)查找"二叉树x"中键值为key的节点
    150. */
    151. template <class T>
    152. BSTNode<T>* BSTree<T>::search(BSTNode<T>* x, T key) const
    153. {
    154. if (x==NULL || x->key==key)
    155. return x;
    156. if (key < x->key)
    157. return search(x->left, key);
    158. else
    159. return search(x->right, key);
    160. }
    161. template <class T>
    162. BSTNode<T>* BSTree<T>::search(T key)
    163. {
    164. search(mRoot, key);
    165. }
    166. /*
    167. * (非递归实现)查找"二叉树x"中键值为key的节点
    168. */
    169. template <class T>
    170. BSTNode<T>* BSTree<T>::iterativeSearch(BSTNode<T>* x, T key) const
    171. {
    172. while ((x!=NULL) && (x->key!=key))
    173. {
    174. if (key < x->key)
    175. x = x->left;
    176. else
    177. x = x->right;
    178. }
    179. return x;
    180. }
    181. template <class T>
    182. BSTNode<T>* BSTree<T>::iterativeSearch(T key)
    183. {
    184. iterativeSearch(mRoot, key);
    185. }
    186. /*
    187. * 查找最小结点:返回tree为根结点的二叉树的最小结点。
    188. */
    189. template <class T>
    190. BSTNode<T>* BSTree<T>::minimum(BSTNode<T>* tree)
    191. {
    192. if (tree == NULL)
    193. return NULL;
    194. while(tree->left != NULL)
    195. tree = tree->left;
    196. return tree;
    197. }
    198. template <class T>
    199. T BSTree<T>::minimum()
    200. {
    201. BSTNode<T> *p = minimum(mRoot);
    202. if (p != NULL)
    203. return p->key;
    204. return (T)NULL;
    205. }
    206. /*
    207. * 查找最大结点:返回tree为根结点的二叉树的最大结点。
    208. */
    209. template <class T>
    210. BSTNode<T>* BSTree<T>::maximum(BSTNode<T>* tree)
    211. {
    212. if (tree == NULL)
    213. return NULL;
    214. while(tree->right != NULL)
    215. tree = tree->right;
    216. return tree;
    217. }
    218. template <class T>
    219. T BSTree<T>::maximum()
    220. {
    221. BSTNode<T> *p = maximum(mRoot);
    222. if (p != NULL)
    223. return p->key;
    224. return (T)NULL;
    225. }
    226. /*
    227. * 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。
    228. */
    229. template <class T>
    230. BSTNode<T>* BSTree<T>::successor(BSTNode<T> *x)
    231. {
    232. // 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。
    233. if (x->right != NULL)
    234. return minimum(x->right);
    235. // 如果x没有右孩子。则x有以下两种可能:
    236. // (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。
    237. // (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。
    238. BSTNode<T>* y = x->parent;
    239. while ((y!=NULL) && (x==y->right))
    240. {
    241. x = y;
    242. y = y->parent;
    243. }
    244. return y;
    245. }
    246. /*
    247. * 找结点(x)的前驱结点。即,查找"二叉树中数据值小于该结点"的"最大结点"。
    248. */
    249. template <class T>
    250. BSTNode<T>* BSTree<T>::predecessor(BSTNode<T> *x)
    251. {
    252. // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。
    253. if (x->left != NULL)
    254. return maximum(x->left);
    255. // 如果x没有左孩子。则x有以下两种可能:
    256. // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
    257. // (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。
    258. BSTNode<T>* y = x->parent;
    259. while ((y!=NULL) && (x==y->left))
    260. {
    261. x = y;
    262. y = y->parent;
    263. }
    264. return y;
    265. }
    266. /*
    267. * 将结点插入到二叉树中
    268. *
    269. * 参数说明:
    270. * tree 二叉树的根结点
    271. * z 插入的结点
    272. */
    273. template <class T>
    274. void BSTree<T>::insert(BSTNode<T>* &tree, BSTNode<T>* z)
    275. {
    276. BSTNode<T> *y = NULL;
    277. BSTNode<T> *x = tree;
    278. // 查找z的插入位置
    279. while (x != NULL)
    280. {
    281. y = x;
    282. if (z->key < x->key)
    283. x = x->left;
    284. else
    285. x = x->right;
    286. }
    287. z->parent = y;
    288. if (y==NULL)
    289. tree = z;
    290. else if (z->key < y->key)
    291. y->left = z;
    292. else
    293. y->right = z;
    294. }
    295. /*
    296. * 将结点(key为节点键值)插入到二叉树中
    297. *
    298. * 参数说明:
    299. * tree 二叉树的根结点
    300. * key 插入结点的键值
    301. */
    302. template <class T>
    303. void BSTree<T>::insert(T key)
    304. {
    305. BSTNode<T> *z=NULL;
    306. // 如果新建结点失败,则返回。
    307. if ((z=new BSTNode<T>(key,NULL,NULL,NULL)) == NULL)
    308. return ;
    309. insert(mRoot, z);
    310. }
    311. /*
    312. * 删除结点(z),并返回被删除的结点
    313. *
    314. * 参数说明:
    315. * tree 二叉树的根结点
    316. * z 删除的结点
    317. */
    318. template <class T>
    319. BSTNode<T>* BSTree<T>::remove(BSTNode<T>* &tree, BSTNode<T> *z)
    320. {
    321. BSTNode<T> *x=NULL;
    322. BSTNode<T> *y=NULL;
    323. if ((z->left == NULL) || (z->right == NULL) )
    324. y = z;
    325. else
    326. y = successor(z);
    327. if (y->left != NULL)
    328. x = y->left;
    329. else
    330. x = y->right;
    331. if (x != NULL)
    332. x->parent = y->parent;
    333. if (y->parent == NULL)
    334. tree = x;
    335. else if (y == y->parent->left)
    336. y->parent->left = x;
    337. else
    338. y->parent->right = x;
    339. if (y != z)
    340. z->key = y->key;
    341. return y;
    342. }
    343. /*
    344. * 删除结点(z),并返回被删除的结点
    345. *
    346. * 参数说明:
    347. * tree 二叉树的根结点
    348. * z 删除的结点
    349. */
    350. template <class T>
    351. void BSTree<T>::remove(T key)
    352. {
    353. BSTNode<T> *z, *node;
    354. if ((z = search(mRoot, key)) != NULL)
    355. if ( (node = remove(mRoot, z)) != NULL)
    356. delete node;
    357. }
    358. /*
    359. * 销毁二叉树
    360. */
    361. template <class T>
    362. void BSTree<T>::destroy(BSTNode<T>* &tree)
    363. {
    364. if (tree==NULL)
    365. return ;
    366. if (tree->left != NULL)
    367. return destroy(tree->left);
    368. if (tree->right != NULL)
    369. return destroy(tree->right);
    370. delete tree;
    371. tree=NULL;
    372. }
    373. template <class T>
    374. void BSTree<T>::destroy()
    375. {
    376. destroy(mRoot);
    377. }
    378. /*
    379. * 打印"二叉查找树"
    380. *
    381. * key -- 节点的键值
    382. * direction -- 0,表示该节点是根节点;
    383. * -1,表示该节点是它的父结点的左孩子;
    384. * 1,表示该节点是它的父结点的右孩子。
    385. */
    386. template <class T>
    387. void BSTree<T>::print(BSTNode<T>* tree, T key, int direction)
    388. {
    389. if(tree != NULL)
    390. {
    391. if(direction==0) // tree是根节点
    392. cout << setw(2) << tree->key << " is root" << endl;
    393. else // tree是分支节点
    394. cout << setw(2) << tree->key << " is " << setw(2) << key << "'s " << setw(12) << (direction==1?"right child" : "left child") << endl;
    395. print(tree->left, tree->key, -1);
    396. print(tree->right,tree->key, 1);
    397. }
    398. }
    399. template <class T>
    400. void BSTree<T>::print()
    401. {
    402. if (mRoot != NULL)
    403. print(mRoot, mRoot->key, 0);
    404. }
    405. #endif

    二叉查找树的C++测试程序(BSTreeTest.cpp)

    1. /**
    2. * C++ 语言: 二叉查找树
    3. *
    4. * @author skywang
    5. * @date 2013/11/07
    6. */
    7. #include <iostream>
    8. #include "BSTree.h"
    9. using namespace std;
    10. static int arr[]= {1,5,4,3,2,6};
    11. #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) )
    12. int main()
    13. {
    14. int i, ilen;
    15. BSTree<int>* tree=new BSTree<int>();
    16. cout << "== 依次添加: ";
    17. ilen = TBL_SIZE(arr);
    18. for(i=0; i<ilen; i++)
    19. {
    20. cout << arr[i] <<" ";
    21. tree->insert(arr[i]);
    22. }
    23. cout << "\n== 前序遍历: ";
    24. tree->preOrder();
    25. cout << "\n== 中序遍历: ";
    26. tree->inOrder();
    27. cout << "\n== 后序遍历: ";
    28. tree->postOrder();
    29. cout << endl;
    30. cout << "== 最小值: " << tree->minimum() << endl;
    31. cout << "== 最大值: " << tree->maximum() << endl;
    32. cout << "== 树的详细信息: " << endl;
    33. tree->print();
    34. cout << "\n== 删除根节点: " << arr[3];
    35. tree->remove(arr[3]);
    36. cout << "\n== 中序遍历: ";
    37. tree->inOrder();
    38. cout << endl;
    39. // 销毁二叉树
    40. tree->destroy();
    41. return 0;
    42. }

    关于二叉查找树的C++实现有两点需要补充说明的:
    第1点:采用了STL模板。因此,二叉查找树支持任意数据类型。
    第2点:将二叉查找树的"声明"和"实现"都位于BSTree.h中。这是因为,在二叉查找树的实现采用了模板;而C++编译器不支持对模板的分离式编译!

    二叉查找树的C++测试程序

    上面的BSTreeTest.c是二叉查找树树的测试程序,运行结果如下:

    == 依次添加: 1 5 4 3 2 6 
    == 前序遍历: 1 5 4 3 2 6 
    == 中序遍历: 1 2 3 4 5 6 
    == 后序遍历: 2 3 4 6 5 1 
    == 最小值: 1
    == 最大值: 6
    == 树的详细信息: 
     1 is root
     5 is  1's  right child
     4 is  5's   left child
     3 is  4's   left child
     2 is  3's   left child
     6 is  5's  right child
    
    == 删除根节点: 3
    == 中序遍历: 1 2 4 5 6 

    下面对测试程序的流程进行分析!

    (01) 新建"二叉查找树"root。


    (02) 向二叉查找树中依次插入1,5,4,3,2,6 。如下图所示:

    (03) 遍历和查找
    插入1,5,4,3,2,6之后,得到的二叉查找树如下:

    前序遍历结果: 1 5 4 3 2 6
    中序遍历结果: 1 2 3 4 5 6
    后序遍历结果: 2 3 4 6 5 1
    最小值是1,而最大值是6。

    (04) 删除节点4。如下图所示:

    (05) 重新遍历该二叉查找树。
    中序遍历结果: 1 2 4 5 6

  • 相关阅读:
    【无标题】
    在Linux上安装Oracle 数据库 11g (含静默方式安装)
    前端面试之事件循环
    第二章 Servlet生命周期 ② 代码
    SpringSecurity_权限管理
    Unity 实战项目 ☀️| 只用一个脚本做一个 刮刮乐 案例,一不小心刮出来一个女朋友!【学习娱乐一下】
    ArmSoM-W3之RK3588 MPP环境配置
    SpringBoot访问静态资源报404——记录一次调试过程与解决方案
    pycharm连接远程服务器进行调试
    编程基础-C++入门到入土知识手册
  • 原文地址:https://blog.csdn.net/u012294613/article/details/125536038