• c++:二叉搜索树的简析与详细实现(万字)


    目录

    一:什么是二叉搜索树

     二:优点

     三:实现二叉搜索树

    (1)定义头文件和test函数

    (2)创建二叉树的基本结构

    (3)创建二叉树

     (4)Insert非递归版本

    insert 代码如下:

    (5)中序遍历(打印)

     (6)查找Find(非递归版本)

    (7)删除Erase(非递归版本)

    erase的总代码: 

    (7)拷贝和析构

     (8)查找(递归版本)

    (9)插入(递归版本)

     (10)删除Erase(递归版本)

    总代码:


    一:什么是二叉搜索树

    该树的任意一个子树都要满足

    左子树的值<根<右子树的值

    如图: 

     二:优点

    查找非常迅速,最多查找高度次

    列如我查找7,

    (1)8>7,进入左边

    (2)7>3,进入右边

    (3)7>6, 进入右边

    (4)7=7,找到

    缺点:查找时间复杂度:O(N)

    如下图的极端情况:

     三:实现二叉搜索树

    (1)定义头文件和test函数

    (2)创建二叉树的基本结构

    1. template<class k>//模板
    2. //struct BinarySearchTree全名
    3. struct BSTree//缩写名,孩子兄弟表示法
    4. {
    5. BSTree* _left;
    6. BSTree* _right;
    7. k _key;
    8. };

     不清楚孩子兄弟表示法看这里:树的孩子兄弟表示法_幻荼的博客-CSDN博客

    (3)创建二叉树

    1. template<class k>
    2. class BSTree
    3. {
    4. typedef BSTreeNode Node;//简写一下名字,不简写也行
    5. public:
    6. private:
    7. Node* _root=nullptr;//创建了一个名为_root的空树
    8. };

     (4)Insert非递归版本

    我们可以设定两个数记录位置,一个记录插入数的位置(cur),一个记录插入数的上一个位置(parent),当插入的数走到空,就new一个结点然后链接起来

    假设我有这么一个树,需要插入数字’7’

     根据二叉搜索树的特性,7会走到6的右边空位置处

     然后7就new一个结点出来,6再和7相连接

    注意这里链接还需要判断是链接在左边还是在右边,这里 7>6所以链接在右边

    假如这里不是6而是8,那么7就应该链接在左边

    insert 代码如下:

    1. bool Insert(const k& key)
    2. {
    3. if (_root == nullptr)//如果是一个空树,就直接new一个结点出来就好了
    4. {
    5. _root = new Node(key);
    6. return true;
    7. }
    8. Node* parent = nullptr;//设置一个记录该数字位置的上一个位置
    9. Node* cur = _root;//记录该数字的位置
    10. while (cur)
    11. {
    12. if (cur->_key < key)//该数字的值大于结点值,就朝该结点的右走
    13. {
    14. parent = cur;
    15. cur = cur->_right;
    16. }
    17. else if (cur->_key > key)//该数字的值小于结点值,就朝该结点的左走
    18. {
    19. parent = cur;
    20. cur = cur->_left;
    21. }
    22. else//不能插入同样得值
    23. {
    24. return false;
    25. }
    26. }
    27. cur = new Node(key);//此时走到空,new一个结点出来
    28. if (parent->_key < key)//如果该数字大于该数字的上一个结点,就需要链接到右边
    29. {
    30. parent->_right = cur;
    31. }
    32. else//如果该数字小于该数字的上一个结点,就需要链接到左边
    33. {
    34. parent->_left = cur;
    35. }
    36. return true;
    37. }

    (5)中序遍历(打印)

    先在private:里面写一个递归

    1. void _InOrder(Node* root)
    2. {
    3. if (root == nullptr)
    4. {
    5. return;
    6. }
    7. _InOrder(root->_left);
    8. cout<< root->_key << " ";
    9. _InOrder(root->_right);
    10. }

     然后public嵌套一下

    1. void InOrder()
    2. {
    3. _InOrder(_root);
    4. cout << endl;
    5. }

    这样嵌套一层,我们就可以直接用,不用传参就可以打印。

    1. void TestTree()
    2. {
    3. BSTree<int> t;//创建一个树
    4. int a[] = { 8,3,1,10,6,4,7,14,13 };
    5. for (auto e : a)//将数字插入树
    6. {
    7. t.Insert(e);
    8. }
    9. t.InOrder();//打印
    10. t.Insert(16);//插入
    11. t.Insert(9);//插入
    12. t.InOrder();//打印
    13. }

    不明白_InOrder递归怎么实现的?

    好吧

    差不多是这样的(没画完,理解就行):

     (6)查找Find(非递归版本)

    不就是把刚才的代码截取一半吗?

    1. bool Find(const k& key)
    2. {
    3. Node* parent=nullptr;
    4. Node* cur = _root;//传根节点
    5. while (cur)
    6. {
    7. if (cur->_key < key)//大就右走
    8. {
    9. parent = cur;
    10. cur = cur->_right;
    11. }
    12. else if (cur->_key > key)//小就左走
    13. {
    14. parent = cur;
    15. cur = cur->_left;
    16. }
    17. else//找到return true
    18. {
    19. return true;
    20. }
    21. }
    22. return false;//找不到return false
    23. }


    (7)删除Erase(非递归版本)

    我们删除这里分三种情况

    (1)该结点没有孩子,比如4,7,13,1

    (2)该节点只有一个孩子(不论左孩子还是右孩子),比如10,14

    (3)该结点有两个孩子(左孩子+右孩子),比如3,6.

    其实对于情况一和情况二,我们可以直接删除该结点,让该结点的父节点指向空或者自己的孩子结点。

    比如我要删除7和14

    7:因为7后面没有子节点,所以只需要干掉7的空间,然后让7的父亲6,指向7的左还是或者右孩子(反正都是空,随意)就可以了

     14:因为14有一个孩子13,所以删除14的时候,我们需要把14的父亲10链接到13上面

     这两种情况其实都非常好处理,难处理的是有两个孩子的情况

    比如根节点8和3

    这个时候就需要运用到一个叫做替换法的方法

    替换法:该结点与该节点左子树的最大值结点或者右子树的最小值结点的值进行交换

    我们拿3举例

    3:对于3这个结点而言,他左子树的最大值是1,右子树的最小值是4,假如我们和4进行交换

     假如我们和1进行交换

     实际上和直接删除相比,只是多了一个交换而已

    理解了上门的理论之后,我们就需要更深入的来完善各种细节

    (1)在只有一个或者没有子节点删除的时候我们需要谨慎的判断到底父节点指向子节点的左还是子节点的右

    假如我们删除4和14.(这里是一个孩子或者没有孩子的情况)

    这个时候6的左指向4的右,10的右指向14的左。

     所以我们这里就需要分两种情况来讨论,子节点的左为空,子节点的右为空

    1. Node* parent = nullptr;//这里就是find,重点在下面的else
    2. Node* cur = _root;
    3. while (cur)//找到该节点
    4. {
    5. if (cur->_key < key)
    6. {
    7. parent = cur;
    8. cur = cur->_right;
    9. }
    10. else if (cur->_key > key)
    11. {
    12. parent = cur;
    13. cur = cur->_left;
    14. }
    15. else{ //重点在这里
    16. if (cur->_left == nullptr)//如果左为空
    17. {
    18. if (cur == parent->_left)//我是左结点,父节点的左指向我的右
    19. {
    20. parent->_left = cur->_right;
    21. }
    22. else//我是右结点,父节点的右指向我的右
    23. {
    24. parent->_right = cur->_right;
    25. }
    26. delete cur;
    27. }
    28. else if (cur->_right == nullptr)//如果右为空
    29. {
    30. if (cur == parent->_left)//我是左孩子
    31. {
    32. parent->_left = cur->_left;//父亲的左指向我的左
    33. }
    34. else//我是右孩子
    35. {
    36. parent->_right = cur->_left;//父亲的右指向我的左
    37. }
    38. delete cur;
    39. }
    40. }

     这里还有一个极端情况

    假如该树只有左孩子,或者该树只有右孩子,然后删除根节点

     在这种情况下,我们的parent就会为空,所以我们只需要将根节点变为第二个结点就好

    1. if (parent == nullptr)
    2. {
    3. _root = cur->_right;//左数就left,右树就right
    4. }

     好了,上面是1个或者没有孩子的情况

    下面是有两个孩子的情况

    我们来删除3和8,默认找右树最小值(当然你也可以找左数最大值)

    其实最关键的还是看父节点的左和右指向子节点的哪

    3:3和4交换,父节点的左指向子节点的右

     8:10和8交换,父节点的右,指向子节点的右

    1. Node* minParent = cur;//注意父节点需要给根节点,防止为空
    2. Node* minRight = cur->_right;
    3. while (minRight->_left)
    4. {
    5. minParent = minRight;
    6. minRight = minRight->_left;
    7. }
    8. swap(minRight->_key, cur->_key);
    9. if (minParent->_left == minRight)
    10. {
    11. minParent->_left = minRight->_right;
    12. }
    13. else
    14. {
    15. minParent->_right = minRight->_right;
    16. }
    17. delete minRight;

    erase的总代码: 

    1. bool Erase(const k& key)
    2. {
    3. Node* parent = nullptr;
    4. Node* cur = _root;
    5. while (cur)
    6. {
    7. if (cur->_key < key)//值大走右边
    8. {
    9. parent = cur;
    10. cur = cur->_right;
    11. }
    12. else if (cur->_key > key)//值小走左边
    13. {
    14. parent = cur;
    15. cur = cur->_left;
    16. }
    17. else//找到该结点
    18. {
    19. //一个孩子--左为空 or 右为空
    20. //两个孩子--替换法
    21. if (cur->_left == nullptr)//左为空,有右孩子
    22. {
    23. if (parent == nullptr)//如果该树只有右孩子,又直接删除根节点会导致parent为空,所以直接将根结点变为右边的下一个孩子,然后删除根节点
    24. {
    25. _root = cur->_right;
    26. }
    27. else//这里就是正常情况
    28. {
    29. if (cur == parent->_left)//如果我是你的左孩子,那么父节点的左就要指向我的右
    30. {
    31. parent->_left = cur->_right;
    32. }
    33. else//如果我是你的右孩子,那么父节点的右就要指向我的右
    34. {
    35. parent->_right = cur->_right;
    36. }
    37. }
    38. delete cur;//删除结点
    39. }
    40. else if (cur->_right == nullptr)//右为空,有左孩子
    41. {
    42. if (parent == nullptr)//还是判断极端情况
    43. {
    44. parent = cur->_left;
    45. }
    46. else//正常情况
    47. {
    48. if (cur == parent->_left)//我是你的左孩子,父节点的左指向我的左
    49. {
    50. parent->_left = cur->_left;
    51. }
    52. else//我是你的右孩子,父节点的右指向我的左
    53. {
    54. parent->_right = cur->_left;
    55. }
    56. }
    57. delete cur;
    58. }
    59. else//两个孩子都不为空,替换法
    60. {
    61. //找右子树的最小结点替代
    62. Node* minParent = cur;
    63. Node* minRight = cur->_right;
    64. while (minRight->_left)//找右子树的最小值结点
    65. {
    66. minParent = minRight;
    67. minRight = minRight->_left;
    68. }
    69. swap(minRight->_key, cur->_key);//交换两个结点的值
    70. if (minParent->_left == minRight)//如果我是左孩子,父节点的左指向我的右
    71. {
    72. minParent->_left = minRight->_right;
    73. }
    74. else//如果我是你的右孩子,父节点的右指向我的右。
    75. {
    76. minParent->_right = minRight->_right;
    77. }
    78. delete minRight;
    79. }
    80. return true;
    81. }
    82. }
    83. return false;//找不到结点return false
    84. }

    注意:这里捋不清的小伙伴,自己多画画图,一定要判断清楚父节点的指针究竟指向哪里,这是删除里面最重要的

    (7)拷贝和析构

    不是重点看看就行了直接上代码

    1. public
    2. ~BSTree()
    3. {
    4. DestoryTree(_root);
    5. _root = nullptr;
    6. }
    7. //强制编译器自己生成构造
    8. BSTree() = default;
    9. BSTree(const BSTree& t)
    10. {
    11. _root = CopyTree(t._root);
    12. }
    13. BSTree& operator=(BSTree t)
    14. {
    15. swap(_root, t._root);
    16. return *this;
    17. }
    18. private
    19. void DestoryTree(Node* root)//递归逐步销毁
    20. {
    21. if (root == nullptr)
    22. {
    23. return;
    24. }
    25. DestoryTree(root->_left);
    26. DestoryTree(root->_right);
    27. delete root;
    28. }
    29. Node* CopyTree(Node* root)//递归逐步创建结点
    30. {
    31. if (root == nullptr)
    32. {
    33. return nullptr;
    34. }
    35. Node* copyNode = new Node(root->_key);
    36. copyNode->_left = CopyTree(root->_left);
    37. copyNode->_right = CopyTree(root->_right);
    38. return copyNode;
    39. }

    以上就是非递归版本,大家理解之后就可以看递归版本了 

     (8)查找(递归版本)

    1. public
    2. bool FindR(const k& key)//嵌套一层而已
    3. {
    4. return _FindR(_root, key);
    5. }
    6. private:
    7. bool _FindR(Node* root, const k& key)//这里是递归的实现
    8. {
    9. if (root == nullptr)//找不到
    10. {
    11. return false;
    12. }
    13. if (root->_key < key)//值大,往右查找
    14. {
    15. return _FindR(root->_right, key);
    16. }
    17. else if (root->_key > key)//值小,往左查找
    18. {
    19. return _FindR(root->_left, key);
    20. }
    21. else//找到
    22. {
    23. return true;
    24. }
    25. }

    (9)插入(递归版本)

    我这里先上代码,然后再来解释

    1. public
    2. bool InsertR(const k& key)//嵌套一层
    3. {
    4. return _InsertR(_root, key);
    5. }
    6. private
    7. bool _InsertR(Node*& root, const k& key)
    8. {
    9. if (root == nullptr)//找到了,插入
    10. {
    11. root = new Node(key);
    12. return true;
    13. }
    14. //下面是查找插入的位置
    15. if (root->_key < key)
    16. {
    17. return _InsertR(root->_right, key);
    18. }
    19. else if (root->_key > key)
    20. {
    21. return _InsertR(root->_left, key);
    22. }
    23. else
    24. {
    25. return false;
    26. }
    27. }

    小伙伴们其实找插入的位置是看的明白的,但是不明白new出来的结点是怎么和原来的树链接起来的。

    我们以前是使用了一个parent来记录插入结点的上一个位置(当然这里也可以),但是这里我们没有parent,究竟是怎么链接起来的呢?

    这里我们有一个小知识:引用C++引用_幻荼的博客-CSDN博客

    (不清楚引用的同学一定要先看我的这篇文章,很简短,不然后面看不懂)

    bool _InsertR(Node*& root, const k& key)//仔细看这里的root有个&符号

    我们举例,插入一个9:结果应该是这样的

     关键就是在数字10这里,我们深入看看

    (1)根节点是10,10>9,进入下面else if内

     (2)此时返回10的左(null)给InsertR,insertR把null传给插入

     (3)关键来了因为这里是&,所以root是root->_left的别名

    这样就相当于10->left=new Node(key)

     (10)删除Erase(递归版本)

    我们理解了上面的别名之后,我们看看Erase的递归是如何巧用别名的

    还是先上代码

    1. public
    2. bool EraseR(const k& key)//嵌套一层
    3. {
    4. return _EraseR(_root, key);
    5. }
    6. private
    7. bool _EraseR(Node*& root, const k& key)
    8. {
    9. if (root == nullptr)//没找到
    10. {
    11. return false;
    12. }
    13. if (root->_key < key)//大往右走
    14. {
    15. return _EraseR(root->_right, key);
    16. }
    17. else if (root->_key > key)//小往左走
    18. {
    19. return _EraseR(root->_left, key);
    20. }
    21. else//找到了
    22. {
    23. Node* del = root;//创建一个指针记录root方便删除
    24. if (root->_left == nullptr)
    25. {
    26. root = root->_right;
    27. }
    28. else if (root->_right == nullptr)
    29. {
    30. root = root->_left;
    31. }
    32. else
    33. {
    34. Node* minRight = root->_right;
    35. while (minRight->_left)
    36. {
    37. minRight = minRight->_left;
    38. }
    39. swap(root->_key, minRight->_key);
    40. return _EraseR(root->_right, key);
    41. }
    42. delete del;
    43. return true;
    44. }
    45. }

    我们举例,我们删除14(有一个孩子或者没有孩子的情况):

     (1)14右为空进入这个代码

     (2)而14其实是10->rigth的别名

    这个代码翻译一下就是

    1. else if14->right==nullptr)
    2. {
    3. 10->right=14->left;
    4. }

     是不是就链接起来了

     再看我们删除3(有两个孩子):

     (1)找到右树最小值,然后交换两个值

    (2)因为别名不能改变,我现在的别名是8->left,所以我们return,root->right;

    就等于是重新在一个树里面删除

     就是在这个根为6的树里面删除3这个结点,那么就和我们原来的一模一样了

     以上就是所有的讲解,最后附上总代码

    -------------------------------------

    总代码:

    BinarySearchTree.h

    1. #pragma once
    2. #include
    3. using namespace std;
    4. template<class k>
    5. struct BSTreeNode
    6. {
    7. BSTreeNode* _left;
    8. BSTreeNode* _right;
    9. k _key;
    10. BSTreeNode(const k& key)
    11. :_left(nullptr)
    12. ,_right(nullptr)
    13. ,_key(key)
    14. {}
    15. };
    16. template<class k>
    17. class BSTree
    18. {
    19. typedef BSTreeNode Node;
    20. public:
    21. ~BSTree()
    22. {
    23. DestoryTree(_root);
    24. _root = nullptr;
    25. }
    26. //t1=t2
    27. BSTree& operator=(BSTree t)
    28. {
    29. swap(_root, t._root);
    30. return *this;
    31. }
    32. //强制编译器自己生成构造
    33. BSTree() = default;
    34. BSTree(const BSTree& t)
    35. {
    36. _root = CopyTree(t._root);
    37. }
    38. bool Insert(const k& key)
    39. {
    40. if (_root == nullptr)
    41. {
    42. _root = new Node(key);
    43. return true;
    44. }
    45. Node* cur = _root;
    46. Node* parent = nullptr;
    47. while (cur)
    48. {
    49. if (cur->_key < key)
    50. {
    51. parent = cur;
    52. cur = cur->_right;
    53. }
    54. else if (cur->_key > key)
    55. {
    56. parent = cur;
    57. cur = cur->_left;
    58. }
    59. else
    60. {
    61. return false;
    62. }
    63. }
    64. cur = new Node(key);
    65. if (parent->_key < key)
    66. {
    67. parent->_right = cur;
    68. }
    69. else
    70. {
    71. parent->_left = cur;
    72. }
    73. return true;
    74. }
    75. bool Find(const k& key)
    76. {
    77. Node* cur = _root;
    78. while (cur)
    79. {
    80. if (cur->_key < key)
    81. {
    82. cur = cur->_right;
    83. }
    84. else if (cur->_key > key)
    85. {
    86. cur = cur->_left;
    87. }
    88. else
    89. {
    90. return true;
    91. }
    92. }
    93. return false;
    94. }
    95. bool Erase(const k& key)
    96. {
    97. Node* parent = nullptr;
    98. Node* cur = _root;
    99. while (cur)
    100. {
    101. if (cur->_key < key)
    102. {
    103. parent = cur;
    104. cur = cur->_right;
    105. }
    106. else if (cur->_key > key)
    107. {
    108. parent = cur;
    109. cur = cur->_left;
    110. }
    111. else//两个孩子,左为空或者右为空
    112. {
    113. if (cur->_left == nullptr)
    114. {
    115. if (cur == _root)
    116. {
    117. _root = cur->_right;
    118. }
    119. else
    120. {
    121. if (cur == parent->_left)
    122. {
    123. parent->_left = cur->_right;
    124. }
    125. else
    126. {
    127. parent->_right = cur->_right;
    128. }
    129. }
    130. delete cur;
    131. }
    132. else if (cur->_right == nullptr)
    133. {
    134. if (cur == _root)
    135. {
    136. _root = cur->_left;
    137. }
    138. else
    139. {
    140. if (cur == parent->_left)
    141. {
    142. parent->_left = cur->_left;
    143. }
    144. else
    145. {
    146. parent->_right = cur->_left;
    147. }
    148. }
    149. delete cur;
    150. }
    151. else//两个孩子都不为空
    152. {
    153. //右子树的最小结点替代
    154. Node* minRight = cur->_right;
    155. Node* minParent = cur;
    156. while (minRight->_left)
    157. {
    158. minParent = minRight;
    159. minRight = minRight->_left;
    160. }
    161. swap(minRight->_key, cur->_key);
    162. //cur->_key = minRight->_key;
    163. if (minParent->_left == minRight)
    164. {
    165. minParent->_left = minRight->_right;
    166. }
    167. else
    168. {
    169. minParent->_right = minRight->_right;
    170. }
    171. delete minRight;
    172. }
    173. return true;
    174. }
    175. }
    176. return false;
    177. }
    178. void InOrder()
    179. {
    180. _InOrder(_root);
    181. cout << endl;
    182. }
    183. bool FindR(const k& key)
    184. {
    185. return _FindR(_root, key);
    186. }
    187. bool InsertR(const k& key)
    188. {
    189. return _InsertR(_root, key);
    190. }
    191. bool EraseR(const k& key)
    192. {
    193. return _EraseR(_root, key);
    194. }
    195. private:
    196. bool _EraseR(Node* & root,const k& key)
    197. {
    198. if (root == nullptr)
    199. {
    200. return false;
    201. }
    202. if (root->_key < key)
    203. {
    204. return _EraseR(root->_right, key);
    205. }
    206. else if (root->_key > key)
    207. {
    208. return _EraseR(root->_left, key);
    209. }
    210. else
    211. {
    212. Node* del = root;
    213. //删除
    214. if (root->_left == nullptr)
    215. {
    216. root = root->_right;
    217. }
    218. else if (root->_right == nullptr)
    219. {
    220. root = root->_left;
    221. }
    222. else
    223. {
    224. Node* minRight = root->_right;
    225. while (minRight->_left)
    226. {
    227. minRight = minRight->_left;
    228. }
    229. swap(root->_key, minRight->_key);
    230. return _EraseR(root->_right,key);
    231. }
    232. }
    233. }
    234. bool _FindR(Node* root, const k& key)
    235. {
    236. if (root == nullptr)
    237. {
    238. return false;
    239. }
    240. if (root->_key < key)
    241. {
    242. return _FindR(root->_right, key);
    243. }
    244. else if (root->_key > key)
    245. {
    246. return _FindR(root->_left, key);
    247. }
    248. else
    249. {
    250. return true;
    251. }
    252. }
    253. bool _InsertR(Node*& root,const k& key)
    254. {
    255. if (root == nullptr)
    256. {
    257. root = new Node(key);
    258. return true;
    259. }
    260. if (root->_key < key)
    261. {
    262. return _InsertR(root->_right, key);
    263. }
    264. else if (root->_key > key)
    265. {
    266. return _InsertR(root->_left, key);
    267. }
    268. else
    269. {
    270. return false;
    271. }
    272. }
    273. Node* _root = nullptr;
    274. void _InOrder(Node* root)
    275. {
    276. if (root == nullptr)
    277. {
    278. return;
    279. }
    280. _InOrder(root->_left);
    281. cout << root->_key << " ";
    282. _InOrder(root->_right);
    283. }
    284. void DestoryTree(Node* root)
    285. {
    286. if (root == nullptr)
    287. {
    288. return;
    289. }
    290. DestoryTree(root->_left);
    291. DestoryTree(root->_right);
    292. delete root;
    293. }
    294. Node* CopyTree(Node* root)
    295. {
    296. if (root == nullptr)
    297. {
    298. return nullptr;
    299. }
    300. Node* copyNode = new Node(root->_key);
    301. copyNode->_left = CopyTree(root->_left);
    302. copyNode->_right = CopyTree(root->_right);
    303. return copyNode;
    304. }
    305. };
    306. void TestBSTree()
    307. {
    308. BSTree<int> t;
    309. int a[] = { 8,3,1,10,6,4,7,14,13 };
    310. for (auto e : a)
    311. {
    312. t.Insert(e);
    313. }
    314. t.InOrder();
    315. t.Insert(16);
    316. t.Insert(9);
    317. t.InOrder();
    318. }
    319. void TestBSTree2()
    320. {
    321. BSTree<int> t;
    322. int a[] = { 8,7,9,12,5,19,20,30 };
    323. for (auto e : a)
    324. {
    325. t.Insert(e);
    326. }
    327. t.InOrder();
    328. }
    329. void TestBSTree3()
    330. {
    331. BSTree<int> t;
    332. int a[] = { 8,3,1,10,6,4,7,14,13 };
    333. for (auto e : a)
    334. {
    335. t.Insert(e);
    336. }
    337. t.InOrder();
    338. cout << endl;
    339. t.Erase(3);
    340. t.Erase(8);
    341. for (auto e : a)
    342. {
    343. t.Erase(e);
    344. }
    345. t.InOrder();
    346. }
    347. void TestBSTree4()
    348. {
    349. BSTree<int> t;
    350. int a[] = { 8,3,1,10,6,4,7,14,13 };
    351. for (auto e : a)
    352. {
    353. t.Insert(e);
    354. }
    355. t.InOrder();
    356. BSTree<int> copy = t;
    357. copy.InOrder();
    358. }
    359. void TestBSTree5()
    360. {
    361. BSTree<int> t;
    362. int a[] = { 8,3,1,10,6,4,7,14,13 };
    363. for (auto e : a)
    364. {
    365. t.Insert(e);
    366. }
    367. t.InOrder();
    368. t.InsertR(9);
    369. BSTree<int> copy = t;
    370. copy.InOrder();
    371. }
    372. void TestBSTree6()
    373. {
    374. BSTree<int> t;
    375. int a[] = { 8,3,1,10,6,4,7,14,13 };
    376. for (auto e : a)
    377. {
    378. t.Insert(e);
    379. }
    380. t.InOrder();
    381. t.EraseR(3);
    382. t.EraseR(8);
    383. t.InOrder();
    384. t.EraseR(14);
    385. t.EraseR(7);
    386. t.InOrder();
    387. }

    test.cpp

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"BinarySearchTree.h"
    3. int main()
    4. {
    5. TestBSTree6();
    6. return 0;
    7. }

  • 相关阅读:
    c++访问修饰符与继承关系
    Dubbo-Adaptive实现原理
    11.10记录纪要
    查询或解析solidity智能合约事件event或logs日志
    程序员必看内容连续集之 SpringBoot05 整合Druid&Redis
    原生小程序一键获取手机号
    MySQL的enum类型的踩坑记录
    Python编程基础:深入理解值类型与引用类型及其在数据处理中的关键作用
    MyBatisPlus-条件构造器/分页/多数据源/MyBatisX插件(生成代码)
    js 监控 浏览器关闭前 调用
  • 原文地址:https://blog.csdn.net/qq_62718027/article/details/126009024