Bootstrap

小论二叉搜索树

目录

01.二叉搜索树的概念

02.二叉搜索树的操作过程

03.二叉搜索树的代码实现

(1)基本框架

(2)树的创建与销毁

(3)元素的查找

(4)元素的插入

(5)元素的删除

(6)中序遍历


01.二叉搜索树的概念

有一种查找方法,具有极高的查找效率,时间复杂度仅为 O(log⁡n)。在每一步查找过程中都会将搜索范围减半,这就是二分查找。

但是在实际应用中,我们并不经常使用二分查找。这是因为它只能在有序数组或有序数据结构上工作,如果数据是无序的,则需要先进行排序;而且它只适用于数组或连续存储的数据结构中。

在实际运用中,我们更常用的是二叉搜索树(Binary Search Tree, BST),其查找时间复杂度也是 O(log⁡n),并且拥有高效的插入和删除操作。

二叉搜索树又称二叉排序树,它的定义如下:

1.若它的左子树不为空,则左子树上所有节点的值都小于根节点的值

2.若它的右子树不为空,则右子树上所有节点的值都大于根节点的值

3.它的左右子树也分别为二叉搜索树

检验一个二叉树是否是二叉搜索树,不仅要检查每个节点的左右子节点的值是否满足条件,还要确保整个左子树的所有节点的值都小于根节点的值,整个右子树的所有节点的值都大于根节点的值。

02.二叉搜索树的操作过程

1.二叉搜索树的查找

        step1:从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。

        step2:如果最终走到节点为空还没找到,则这个值不存在。

2.二叉树搜索树的插入

二叉搜索树的插入都是在叶子节点进行插入,第一个插入的元素就是根节点,后面插入的元素根据性质判断插入位置

3.二叉搜索树的删除

二叉搜索树的删除除了会在叶子结点进行删除外,还可能在中间结点进行删除,在中间结点进行删除时,就需要考虑它的孩子结点的情况,因此要分情况讨论:

case1.删除结点无孩子结点

case2.删除结点只有左孩子结点

case3.删除结点只有右孩子结点

case4.删除结点有左、右孩子结点

其实case1和case2或3可以结合起来,实际删除过程如下:

case2:删除该结点后,该父结点指向该左孩子结点

case3:删除该结点后,该父结点指向该右孩子结点

case4:需要在其子树中寻找一个结点替代该结点(左子树的值最大结点/右子树的值最小结点),替换后仍满足搜索树要求

比如上述例子中,可以用36(左子树的值最大结点)或50(右子树的值最小结点)对41进行替换,然后将替换的结点位置进行删除。

03.二叉搜索树的代码实现

(1)基本框架

身为一个二叉树,其结点内部包含左孩子结点指针、右孩子结点指针、数据,在二叉搜索树中,我们需要实现它的查找、插入、删除以及中序遍历操作。

为什么特意要实现中序遍历呢,因为根据二叉搜索树的特性(任意一个结点,其左子树的所有结点都小于该结点,右子树所有结点都大于该结点)中序遍历会以递增的方式访问树中的所有结点(先访问左子树-->根节点-->右子树),此时就可以按顺序输出所有节点,实现了排序的效果。


  1. template<class T>

  2. struct BSTNode

  3. {

  4. BSTNode(const T& data = T())

  5. :

  6. _pLeft(nullptr), _pRight(nullptr), _data(data)

  7. {}

  8. BSTNode<T>* _pLeft;

  9. BSTNode<T>* _pRight;

  10. T _data;

  11. };

  12. template<class T>

  13. class BSTree

  14. {

  15. typedef BSTNode<T> Node;

  16. typedef Node* PNode;

  17. public:

  18. BSTree();//默认构造

  19. ~BSTree();//析构

  20. PNode Find(const T& data);//查找元素

  21. bool Insert(const T& data);//插入元素

  22. bool Erase(const T& data);//删除元素

  23. void InOrder();//中序遍历

  24. private:

  25. PNode _pRoot;

  26. };

(2)树的创建与销毁

树的默认构造只需要在初始化列表中将成员变量根结点赋值为空即可。

析构过程与其他二叉树相同,需要递归地销毁每一个结点:对于每一个结点,首先递归销毁它的左子树,然后递归销毁销毁它的右子树,最后销毁该结点本身。这种顺序保证了我们在销毁父结点前已经销毁了它的所有子结点,防止内存泄漏。


  1. BSTree() : _pRoot(nullptr){}

  2. ~BSTree()

  3. {

  4. destory(_pRoot);

  5. _pRoot = nullptr;

  6. }

  7. void destory(const PNode& node)

  8. {

  9. if (node == nullptr)

  10. return;

  11. destory(node->_pLeft);

  12. destory(node->_pRight);

  13. delete node;

  14. }

(3)元素的查找

首先将要查找的值与当前节点进行比较,根据比较结果选择路径:

  • 比当前节点小,则移向当前节点的左子节点。
  • 比当前节点大,则移向当前节点的右子节点。
  • 等于当前节点,则查找成功,返回当前节点。
  • 遍历到结点为空还没找到,则该值不存在,返回空。

  1. PNode Find(const T& data)

  2. {

  3. Node* cur = _pRoot;

  4. while (cur != nullptr)

  5. {

  6. if (data < cur->_data)

  7. cur = cur->_pLeft;

  8. else if (data > cur->_data)

  9. cur = cur->_pRight;

  10. else

  11. return cur;

  12. }

  13. return nullptr;

  14. }

(4)元素的插入

将待插入的值与当前节点进行比较:

  • 比当前节点小,则移向当前节点的左子节点,。
  • 比当前节点的值大,则移向当前节点的右子节点。
  • 等于当前节点的值,则不能进行插入,返回false。
  • 遍历到节点为空,则当前位置就是要插入位置

我们可以定义两个指针,一个记录当前遍历节点位置,一个记录父节点位置,这样在找到插入位置时就可以直接进行插入。


  1. bool Insert(const T& data)

  2. {

  3. // 如果树为空,直接插入

  4. if (nullptr == _pRoot)

  5. {

  6. _pRoot = new Node(data);

  7. return true;

  8. }

  9. // 按照二叉搜索树的性质查找data在树中的插入位置

  10. PNode pCur = _pRoot;

  11. // 记录pCur的双亲,因为新元素最终插入在pCur双亲左右孩子的位置

  12. PNode pParent = nullptr;

  13. while (pCur)

  14. {

  15. pParent = pCur;

  16. if (data < pCur->_data)

  17. pCur = pCur->_pLeft;

  18. else if (data > pCur->_data)

  19. pCur = pCur->_pRight;

  20. else

  21. return false; // 元素已经在树中存在

  22. }

  23. // 插入元素

  24. pCur = new Node(data);

  25. if (data < pParent->_data)

  26. pParent->_pLeft = pCur;

  27. else

  28. pParent->_pRight = pCur;

  29. return true;

  30. }

以上方法我们需要显式的管理父节点指针,过程略显繁琐,有没有什么办法可以直接修改当前节点指针呢,我们观察以下这段代码:


  1. bool Insert(const T& data)

  2. {

  3. InsertR(_pRoot,data);

  4. return true;

  5. }

  6. void InsertR(PNode& node, const T& data)

  7. {

  8. if (nullptr == node) {

  9. node = new Node(data);

  10. return;

  11. }

  12. if (node->_data == data)

  13. return;

  14. if (data < node->_data)

  15. InsertR(node->_pLeft, data);

  16. else

  17. InsertR(node->_pRight, data);

  18. return;

  19. }

在这段代码中,我们依旧将data值与当前节点进行比较,比较之后,我们选择递归地将data插入到左或右子树当中,并且在递归过程中通过引用传参,可以直接修改节点指针,就不需要用到父节点指针了。

这种引用传参的递归方法,巧妙地化简了插入过程,并且在每个递归调用中自然地处理了边界情况,逻辑更为直观。

(5)元素的删除

根据上面的分析过程,分情况进行讨论

a:删除该结点后,该父结点指向该左孩子结点

b:删除该结点后,该父结点指向该右孩子结点

在a和b情况中,我们直接将父节点中指向当前节点的指针改为指向该节点的下一节点,然后对该节点进行销毁即可。

c:需要在其子树中寻找一个结点替代该结点(左子树的值最大结点/右子树的值最小结点),替换后仍满足搜索树要求

第三种情况处理起来就复杂许多,我们需要先找到替代节点(左子树最大或右子树最小,这里我们选择左子树最大这种情况),找到后还需要考虑替代节点是否含有左子树(不需要考虑右子树,因为最大值肯定没有右子树),如果有,我们需要将替代节点的左子树接到替代节点的父节点上,因此我们还需要定义一个父节点指针变量。

代码呈现如下:


  1. bool Erase(const T& data)

  2. {

  3. // 如果树为空,删除失败

  4. if (nullptr == _pRoot)

  5. return false;

  6. // 查找在data在树中的位置

  7. PNode pCur = _pRoot;

  8. PNode pParent = nullptr;

  9. while (pCur)

  10. {

  11. if (data == pCur->_data)

  12. break;

  13. else if (data < pCur->_data)

  14. {

  15. pParent = pCur;

  16. pCur = pCur->_pLeft;

  17. }

  18. else

  19. {

  20. pParent = pCur;

  21. pCur = pCur->_pRight;

  22. }

  23. }

  24. // data不在二叉搜索树中,无法删除

  25. if (nullptr == pCur)

  26. return false;

  27. // 分以下情况进行删除

  28. if (nullptr == pCur->_pRight)

  29. {

  30. // 当前节点只有左孩子或者左孩子为空---可直接删除

  31. if (pParent->_pLeft == pCur)

  32. pParent->_pLeft = pCur->_pLeft;

  33. else

  34. pParent->_pRight = pCur->_pLeft;

  35. delete pCur;

  36. }

  37. else if (nullptr == pCur->_pLeft)

  38. {

  39. // 当前节点只有右孩子---可直接删除

  40. if (pParent->_pLeft == pCur)

  41. pParent->_pLeft = pCur->_pRight;

  42. else

  43. pParent->_pRight = pCur->_pRight;

  44. delete pCur;

  45. }

  46. else

  47. {

  48. // 当前节点同时具有左右孩子

  49. Node* leftmax = pCur->_pLeft;

  50. Node* pre = pCur;

  51. while (leftmax->_pRight) {

  52. pre = leftmax;

  53. leftmax = leftmax->_pRight;

  54. }

  55. pCur->_data = leftmax->_data;

  56. if (leftmax->_pLeft)

  57. pre->_pRight = leftmax->_pLeft;

  58. else if (pre == pCur)

  59. pCur->_pLeft = nullptr;

  60. else

  61. pre->_pRight = nullptr;

  62. delete leftmax;

  63. leftmax = nullptr;

  64. }

  65. return true;

  66. }

这里我们还是可以考虑用引用传参递归的方法简化过程

但要注意的是,与插入操作不同,删除操作后,返回值是更新后的子树的根节点,这是因为在删除某些节点时,子树的根节点可能会发生变化,比如左子树或右子树的新根节点替代了被删除的节点。而插入操作则不需要,因为树的结构是自然地扩展的。


  1. PNode EraseR(PNode& node, const T& data)

  2. {

  3. if (nullptr == node)

  4. return node;

  5. if (data == node->_data)

  6. {

  7. if (!node->_pRight)

  8. return node->_pLeft;

  9. else if (!node->_pLeft)

  10. return node->_pRight;

  11. else {

  12. if (!node->_pRight) {

  13. PNode temp = node->_pLeft;

  14. delete node;

  15. return temp;

  16. }

  17. else if (!node->_pLeft) {

  18. PNode temp = node->_pRight;

  19. delete node;

  20. return temp;

  21. }

  22. else {

  23. PNode minNode = FindMin(node->_pRight);

  24. node->_data = minNode->_data;

  25. node->_pRight = EraseR(node->_pRight, minNode->_data);

  26. }

  27. }

  28. }

  29. if (data < node->_data)

  30. node->_pLeft = EraseR(node->_pLeft, data);

  31. else if (data > node->_data)

  32. node->_pRight = EraseR(node->_pRight, data);

  33. return node;

  34. }

  35. PNode FindMin(PNode node) const {

  36. while (node && node->_pLeft != nullptr)

  37. node = node->_pLeft;

  38. return node;

  39. }

(6)中序遍历

中序遍历的顺序是:左子树 -> 根节点 -> 右子树,我们使用递归方法来实现:

1.递归左子树:首先递归访问左子树,确保左子树的所有节点值被访问和打印。

2.打印当前节点:然后打印当前节点的值。

3.递归右子树:最后递归访问右子树,确保右子树的所有节点值被访问和打印。


  1. void InOrder()

  2. {

  3. print(_pRoot);

  4. cout << endl;

  5. }

  6. void print(PNode node)

  7. {

  8. if (node == nullptr)

  9. return;

  10. print(node->_pLeft);

  11. cout << node->_data << " ";

  12. print(node->_pRight);

  13. }

最后附上完整代码:


  1. #include<iostream>

  2. using namespace std;

  3. template<class T>

  4. struct BSTNode

  5. {

  6. BSTNode(const T& data = T())

  7. :

  8. _pLeft(nullptr), _pRight(nullptr), _data(data)

  9. {}

  10. BSTNode<T>* _pLeft;

  11. BSTNode<T>* _pRight;

  12. T _data;

  13. };

  14. template<class T>

  15. class BSTree

  16. {

  17. typedef BSTNode<T> Node;

  18. typedef Node* PNode;

  19. public:

  20. BSTree() : _pRoot(nullptr)

  21. {}

  22. ~BSTree(){

  23. destory(_pRoot);

  24. _pRoot = nullptr;

  25. }

  26. PNode Find(const T& data)

  27. {

  28. Node* cur = _pRoot;

  29. while (cur != nullptr)

  30. {

  31. if (data < cur->_data)

  32. cur = cur->_pLeft;

  33. else if (data > cur->_data)

  34. cur = cur->_pRight;

  35. else

  36. return cur;

  37. }

  38. return nullptr;

  39. }

  40. bool Insert(const T& data)

  41. {

  42. // 如果树为空,直接插入

  43. if (nullptr == _pRoot)

  44. {

  45. _pRoot = new Node(data);

  46. return true;

  47. }

  48. // 按照二叉搜索树的性质查找data在树中的插入位置

  49. PNode pCur = _pRoot;

  50. // 记录pCur的双亲,因为新元素最终插入在pCur双亲左右孩子的位置

  51. PNode pParent = nullptr;

  52. while (pCur)

  53. {

  54. pParent = pCur;

  55. if (data < pCur->_data)

  56. pCur = pCur->_pLeft;

  57. else if (data > pCur->_data)

  58. pCur = pCur->_pRight;

  59. else

  60. return false; // 元素已经在树中存在

  61. }

  62. // 插入元素

  63. pCur = new Node(data);

  64. if (data < pParent->_data)

  65. pParent->_pLeft = pCur;

  66. else

  67. pParent->_pRight = pCur;

  68. return true;

  69. /*InsertR(_pRoot,data);

  70. return true;*/

  71. }

  72. bool Erase(const T& data)

  73. {

  74. // 如果树为空,删除失败

  75. if (nullptr == _pRoot)

  76. return false;

  77. // 查找在data在树中的位置

  78. PNode pCur = _pRoot;

  79. PNode pParent = nullptr;

  80. while (pCur)

  81. {

  82. if (data == pCur->_data)

  83. break;

  84. else if (data < pCur->_data)

  85. {

  86. pParent = pCur;

  87. pCur = pCur->_pLeft;

  88. }

  89. else

  90. {

  91. pParent = pCur;

  92. pCur = pCur->_pRight;

  93. }

  94. }

  95. // data不在二叉搜索树中,无法删除

  96. if (nullptr == pCur)

  97. return false;

  98. // 分以下情况进行删除

  99. if (nullptr == pCur->_pRight)

  100. {

  101. // 当前节点只有左孩子或者左孩子为空---可直接删除

  102. if (pParent->_pLeft == pCur)

  103. pParent->_pLeft = pCur->_pLeft;

  104. else

  105. pParent->_pRight = pCur->_pLeft;

  106. delete pCur;

  107. }

  108. else if (nullptr == pCur->_pLeft)

  109. {

  110. // 当前节点只有右孩子---可直接删除

  111. if (pParent->_pLeft == pCur)

  112. pParent->_pLeft = pCur->_pRight;

  113. else

  114. pParent->_pRight = pCur->_pRight;

  115. delete pCur;

  116. }

  117. else

  118. {

  119. Node* leftmax = pCur->_pLeft;

  120. Node* pre = pCur;

  121. while (leftmax->_pRight) {

  122. pre = leftmax;

  123. leftmax = leftmax->_pRight;

  124. }

  125. pCur->_data = leftmax->_data;

  126. if (leftmax->_pLeft)

  127. pre->_pRight = leftmax->_pLeft;

  128. else if (pre == pCur)

  129. pCur->_pLeft = nullptr;

  130. else

  131. pre->_pRight = nullptr;

  132. delete leftmax;

  133. leftmax = nullptr;

  134. }

  135. return true;

  136. //EraseR(_pRoot, data);

  137. //return true;

  138. }

  139. void InOrder()

  140. {

  141. print(_pRoot);

  142. cout << endl;

  143. }

  144. private:

  145. PNode _pRoot;

  146. void InsertR(PNode& node, const T& data)

  147. {

  148. if (nullptr == node) {

  149. node = new Node(data);

  150. return;

  151. }

  152. if (node->_data == data)

  153. return;

  154. if (data < node->_data)

  155. InsertR(node->_pLeft, data);

  156. else

  157. InsertR(node->_pRight, data);

  158. return;

  159. }

  160. PNode EraseR(PNode& node, const T& data)

  161. {

  162. if (nullptr == node)

  163. return node;

  164. if (data == node->_data)

  165. {

  166. if (!node->_pRight)

  167. return node->_pLeft;

  168. else if (!node->_pLeft)

  169. return node->_pRight;

  170. else {

  171. if (!node->_pRight) {

  172. PNode temp = node->_pLeft;

  173. delete node;

  174. return temp;

  175. }

  176. else if (!node->_pLeft) {

  177. PNode temp = node->_pRight;

  178. delete node;

  179. return temp;

  180. }

  181. else {

  182. PNode minNode = FindMin(node->_pRight);

  183. node->_data = minNode->_data;

  184. node->_pRight = EraseR(node->_pRight, minNode->_data);

  185. }

  186. }

  187. }

  188. if (data < node->_data)

  189. node->_pLeft = EraseR(node->_pLeft, data);

  190. else if (data > node->_data)

  191. node->_pRight = EraseR(node->_pRight, data);

  192. return node;

  193. }

  194. PNode FindMin(PNode node) const {

  195. while (node && node->_pLeft != nullptr)

  196. node = node->_pLeft;

  197. return node;

  198. }

  199. void destory(const PNode& node)

  200. {

  201. if (node == nullptr)

  202. return;

  203. destory(node->_pLeft);

  204. destory(node->_pRight);

  205. delete node;

  206. }

  207. void print(PNode node)

  208. {

  209. if (node == nullptr)

  210. return;

  211. print(node->_pLeft);

  212. cout << node->_data << " ";

  213. print(node->_pRight);

  214. }

  215. };

以上就是二叉搜索树的图解与完整实现过程,欢迎在评论区留言,觉得这篇博客对你有帮助的,可以点赞收藏关注支持一波~😉

;