• 手撕二叉搜索树——模拟实现


    前言:

            二叉搜索树的查找效率代表了其插入,删除的操作的性能,这次通过模拟实现二叉搜索树,增加对其底层实现的认识。

    值得提到的一点:

            二叉搜索树的插入,和查找功能很易实现,本篇作为学习笔记,重点了解其删除功能的实现。(代码在最后


    删除功能:

                    如何找到待删除的结点?可以通过创建一个cur指针,和一个parent指针(用来指向cur的父亲结点),cur通过二叉搜索树的性质(左结点的值 < 根节点的值 < 右结点的值)遍历二叉搜索树,找到待删除元素后分以下三大种情况:

            1.cur.left == null; 

            满足以上条件继续分为,cur == root、cur != root即cur是否为parent的左节点或右结点;

            2.cur.right == null;

            满足以上条件继续分为,cur == root、cur != root即cur是否为parent的左节点或右结点;

            3.cur.left != null && cur.right != null;(难点)

            此时需要用替代法进行删除,替代法删除有以下两种方法:(任选一种即可,本篇代码实现选用第一种)

    • 在cur的右边找到最小值,并且若能找到这个最小值,这个最小值点一定不存在左子树;
    • 在cur的左边找到最大值,并且若能找到这个最大值,这个最大值点一定不存在右子树;

            最后通过target(需要被替换的结点)和targetParent(target的父亲结点)两个指针来进行删除操作。

    感觉有点懵?来看下方图解:


    代码实现:

    1. public class BinarySearchTree {
    2. static class TreeNode{
    3. TreeNode left;
    4. TreeNode right;
    5. int val;
    6. public TreeNode(int val){
    7. this.val = val;
    8. }
    9. }
    10. TreeNode root = null;
    11. //查找结点
    12. public TreeNode sreach(int key){
    13. TreeNode cur = root;
    14. while(cur != null){
    15. if(cur.val > key){
    16. cur = cur.left;
    17. }else if(cur.val < key){
    18. cur = cur.right;
    19. }else{
    20. return cur;
    21. }
    22. }
    23. return null;
    24. }
    25. //插入结点
    26. public boolean insert(int key){
    27. if(root == null){
    28. root = new TreeNode(key);
    29. return true;
    30. }
    31. TreeNode cur = root;
    32. TreeNode parent = root;
    33. while(cur != null){
    34. parent = cur;
    35. if(cur.val > key){
    36. cur = cur.left;
    37. }else if(cur.val < key){
    38. cur = cur.right;
    39. }else{
    40. return false;
    41. }
    42. }
    43. if(parent.val > key){
    44. parent.left = new TreeNode(key);
    45. }else{
    46. parent.right = new TreeNode(key);
    47. }
    48. return true;
    49. }
    50. //删除结点
    51. public void remove(int key){
    52. TreeNode cur = root;
    53. TreeNode parent = root;
    54. while(cur != null){
    55. if(cur.val > key){
    56. parent = cur;
    57. cur = cur.left;
    58. }else if(cur.val < key){
    59. parent = cur;
    60. cur = cur.right;
    61. }
    62. else{//找到了,并删除
    63. removeNode(cur,parent);
    64. return;
    65. }
    66. }
    67. }
    68. private void removeNode(TreeNode cur, TreeNode parent){
    69. //分三大种情况: 1.cur.left == null
    70. if(cur.left == null){
    71. if(cur == root){
    72. root = cur.right;
    73. }else if(parent.left == cur){
    74. parent.left = cur.right;
    75. }else if(parent.right == cur){
    76. parent.right = cur.right;
    77. }
    78. //第二大种情况:2.cur.right == null
    79. }else if(cur.right == null){
    80. if(cur == root){
    81. root = cur.left;
    82. }else if(parent.left == cur){
    83. parent.left = cur.left;
    84. }else if(parent.right == cur){
    85. parent.right = cur.left;
    86. }
    87. }
    88. else{
    89. /**
    90. * 第三大种情况:cur两边都不为空
    91. * 可以使用替换法进行删除,有以下两种替换方式(任意一种即可)
    92. * 1.在cur的右边找最小值,并且若能找到这个最小值,这个最小值点一定不存在左子树
    93. * 2.在cur的左边找最大值,并且若能找到这个最小值,这个最大值点一定不存在右子树
    94. * 以下代码通过方法1实现
    95. * 通过target和targetParent来确定要替换的结点
    96. */
    97. TreeNode targetParent = cur;
    98. TreeNode target = cur.right;
    99. while(target.left != null){
    100. targetParent = target;
    101. target = target.left;
    102. }
    103. cur.val = target.val;
    104. //分两种情况
    105. if(targetParent.left == target){
    106. targetParent.left = target.right;
    107. }else{
    108. targetParent.right = target.right;
    109. }
    110. }
    111. }
    112. //中序遍历打印二叉树
    113. public void Print(TreeNode root){
    114. if(root == null){
    115. return;
    116. }
    117. Print(root.left);
    118. System.out.print(root.val);
    119. Print(root.right);
    120. }
    121. }

  • 相关阅读:
    基于NoneBot2的qq机器人配置记录
    零束科技打通智驾全域数据闭环,加速智驾场景规模化量产落地!
    Hadoop集群资源管理器-YARN
    ssm基于web的酒店预订及个性化服务系统 毕业设计源码241822
    JUC第十四讲:JUC锁: ReentrantReadWriteLock详解
    AsyncContext优雅实现HTTP长轮询接口
    PG 联表更新
    蓝桥杯青少组(Python组)考核知识范围
    【MySQL高级】Mysql复制及Mysql权限管理
    Linux驱动开发——PCI设备驱动
  • 原文地址:https://blog.csdn.net/CYK_byte/article/details/126272980