查看原文
其他

二叉查找树的解读和实现

The following article is from ytao Author ytao

(给算法爱好者加星标,修炼编程内功

作者:ytao (本文来自作者投稿)

https://ytao.top/2019/11/03/5_bst/

二叉查找树是将一组无序的数据构建成一颗有序数据的树,其设计思想与二分法类似。很好的提高了海量数据查找效率,使得由从头遍历到尾的方式转为二分查找的方式,时间复杂度从O(n)降低为O(log(n))。

概念

  1. 结点:树上的每个元素。

  2. 根结点:没有父结点的结点。

  3. 父结点:结点的上一级结点。

  4. 子结点:结点的下一级结点。

  5. 叶子结点:没有子结点的结点。

  6. 兄弟结点:拥有同一父结点的相邻结点。

  7. 结点的度:一个结点中拥有子结点的个数。

  8. 树的度:树上最大结点的度。

  9. 结点的层次:以根结点为1,每深入一个子结点层次加1。

  10. 树的高度:树中最大的结点的层次。

特性

  1. 左子树所有的结点值均小于,等于根结点值或为空。

  2. 右子树所有的结点值均大于,等于根结点值或为空。

  3. 左、右子树也分别为二叉排序树。

  4. 没有键值相等的结点。

构建

构建二叉查找树,主要把握几条原则,小于当前结点的在左边,大于的在右边,相等的不予处理。但是情况下结合实际业务需求,也可在相等时放在左结点或右结点,但是必须统一规则,不能左右都存在相等的。创建结点对象:

  1. package com.ytao.bst;


  2. /**

  3. * Created by YANGTAO on 2019/11/3 0003.

  4. */

  5. public class Node {


  6. private Integer value;


  7. private Node leftChildren;


  8. private Node rightChildren;


  9. public Integer getValue() {

  10. return value;

  11. }


  12. public void setValue(Integer value) {

  13. this.value = value;

  14. }


  15. public Node getLeftChildren() {

  16. return leftChildren;

  17. }


  18. public void setLeftChildren(Node leftChildren) {

  19. this.leftChildren = leftChildren;

  20. }


  21. public Node getRightChildren() {

  22. return rightChildren;

  23. }


  24. public void setRightChildren(Node rightChildren) {

  25. this.rightChildren = rightChildren;

  26. }


  27. public Node(Integer value) {

  28. this.value = value;

  29. }

  30. }

创建树的实现:

  1. package com.ytao.bst;


  2. /**

  3. * Created by YANGTAO on 2019/11/3 0003.

  4. */

  5. public class BuildBST {


  6. private Node rootNode = null;


  7. public Node build(int[] vals){

  8. // 遍历所有数据,每次都需从根结点开始寻找左或右子节点为空的位置添加

  9. for (int val : vals) {

  10. this.assemble(rootNode, val);

  11. }


  12. return rootNode;

  13. }


  14. private void assemble(Node node, int val){

  15. // 创建根结点

  16. if (node == null){

  17. rootNode = new Node(val);

  18. }else{

  19. // 根据左小右大特性判断

  20. if (val < node.getValue()){

  21. Node leftNode = node.getLeftChildren();

  22. // 如果左子结点为空,就添加为当前结点的左结点,否则继续递归下去

  23. if (leftNode == null){

  24. node.setLeftChildren(new Node(val));

  25. }else{

  26. this.assemble(node.getLeftChildren(), val);

  27. }

  28. }else{

  29. Node rightNode = node.getRightChildren();

  30. // 如果右子结点为空,就添加为当前结点的右结点,否则继续递归下去

  31. if (rightNode == null){

  32. node.setRightChildren(new Node(val));

  33. }else{

  34. this.assemble(rightNode, val);

  35. }

  36. }


  37. }


  38. }


  39. }

使用 [7,5,9,2,11,6]测试是否满足我们创建树的要求:

  1. public static void main(String[] args) {

  2. int[] vals = {7,5,9,2,11,6};

  3. Node node = new BuildBST().build(vals);

  4. System.out.println(new Gson().toJson(node));

  5. }

测试结果满足我们要求

  1. {

  2. "value": 7,

  3. "leftChildren": {

  4. "value": 5,

  5. "leftChildren": {

  6. "value": 2

  7. },

  8. "rightChildren": {

  9. "value": 6

  10. }

  11. },

  12. "rightChildren": {

  13. "value": 9,

  14. "rightChildren": {

  15. "value": 11

  16. }

  17. }

  18. }

查找

假设从一百万个数字中获取值为88的数据,如果我们使用遍历的方式,最糟的情况就是排在第一百万个位置的时候,需要我们遍历一百万次才能获取到数据,这就是我们最不想遇到的情况。这时将一百万个数据构建成二叉查找树,我们就可通过树快速找到我们想要的数据。由于设定一百万个数据比较多,这里我们举例当前拥有数据 [7,5,9,2,11,6],我们要找出其中的 6使用循环遍历所有数据的方法,我们需要6次遍历 7->5->9->2->11->6。使用二叉查找树查找时,首先构建好的二叉查找树的结构如图:

 

从根结点开始查找;

获取根结点7,不等于6,且6<7,所以继续找左子结点;

获取到结点5,不等于6,且6>5,所以继续找右子节点;

最终获取到结点6,满足我们需要的条件。所遍历的数据为 7->5->6。代码实现查找:

  1. package com.ytao.bst;


  2. /**

  3. * Created by YANGTAO on 2019/11/3 0003.

  4. */

  5. public class SearchBST {


  6. public Node search(Node node, int val){

  7. // 如果结点为空,说明是没有了符合的结点

  8. if (node == null)

  9. return null;


  10. int nodeVal = node.getValue();


  11. // 如果结点上的键值相等,就是我们需要找的结点

  12. if (val == nodeVal){

  13. return node;

  14. }else if (val < nodeVal){ // 如果小于结点的值,那么一定在结点的左子树中

  15. return this.search(node.getLeftChildren(), val);

  16. }else{

  17. return this.search(node.getRightChildren(), val);

  18. }


  19. }



  20. }

插入

二叉查找树的插入规则,必须是要插入后的结点是作为叶子结点。现在向上面的树中插入10,根据上面所分析到的规则,为确保二叉查找树的完整性,最终的插入流程为7->9->11->10:

代码实现:

  1. package com.ytao.bst;


  2. /**

  3. * Created by YANGTAO on 2019/11/3 0003.

  4. */

  5. public class InsertBST {


  6. public void inesrt(Node node, int newVal){

  7. // 当结点为空是,说明是作为根结点

  8. if (node == null){

  9. node = new Node(newVal);

  10. }


  11. int nodeVal = node.getValue();


  12. // 如果小于结点的值,插入到左子树中,大于就插入右子树中

  13. if (newVal < nodeVal){

  14. Node leftNode = node.getLeftChildren();

  15. // 为空时,说明为叶子结点,可插入

  16. if (leftNode == null){

  17. node.setLeftChildren(new Node(newVal));

  18. }else {

  19. this.inesrt(leftNode, newVal);

  20. }

  21. }else if (newVal > nodeVal){

  22. Node rightNode = node.getRightChildren();

  23. if (rightNode == null){

  24. node.setRightChildren(new Node(newVal));

  25. }else {

  26. this.inesrt(rightNode, newVal);

  27. }

  28. }else {

  29. // todo 相等时,可根据具体业务处理,放弃,或在左右树中选择一个

  30. }


  31. }


  32. }

删除

删除结点分为多种情况,其中主要分析的:

叶子结点

删除叶子结点,将所要删除的叶子结点直接删除便可,比如删除结点6。

单子结点的结点

被删除结点,如果只有一个子结点,那么被删除结点删除后,该结点的子结点补上其位置,比如删除结点9。

存在左右子结点的结点

为了更加清楚表达删除存在左右结点的结点,先向树中多添加3个结点8,10,15。然后删除结点9。这里的解决方法就是,删除9后,可以用前驱结点或后继结点补上。前驱结点为左子树中最大的结点,后继结点为右子树中最小的结点。现在以后继结点补上的方案为:

后继结点补上删除后的结点:

完成删除,后继结点补充上后:

代码实现:

  1. package com.ytao.bst;


  2. /**

  3. * Created by YANGTAO on 2019/11/3 0003.

  4. */

  5. public class DeleteBST {



  6. public Node delete(Node node, int delVal) {

  7. // 为空时,代表叶子结点

  8. if (node == null){

  9. return node;

  10. }


  11. int nodeVal = node.getValue();

  12. Node leftNode = node.getLeftChildren();

  13. Node rightNode = node.getRightChildren();


  14. // 删除的结点,与遍历到的当前结点做比较,小于,大于或等于

  15. if (delVal < nodeVal){

  16. Node tempLeftNode = delete(leftNode, delVal);

  17. node.setLeftChildren(tempLeftNode);

  18. } else if(delVal > nodeVal){

  19. Node tempRightNode = delete(rightNode, delVal);

  20. node.setRightChildren(tempRightNode);

  21. } else {

  22. // 删除的结点与当前遍历到的结点相等时

  23. // 并且左结点为空时,返回右结点去补上删除的位置,反则返回左结点补上

  24. // 说明删除结点为单子结点的情况

  25. if (leftNode == null){

  26. return rightNode;

  27. } else if (rightNode == null){

  28. return leftNode;

  29. }


  30. // 通过查询最小右结点,获取后继结点

  31. Node minNode = minNode(rightNode);

  32. int minNodeValue = minNode.getValue();

  33. node.setValue(minNodeValue);

  34. // 删除后继结点

  35. Node tempRightNode = delete(rightNode, minNodeValue);

  36. node.setRightChildren(tempRightNode);

  37. }

  38. return node;

  39. }


  40. private Node minNode(Node node) {

  41. // 一直寻找最小值,知道左子节点为空为止

  42. Node leftNode = node.getLeftChildren();

  43. if (leftNode != null)

  44. return minNode(leftNode);

  45. return node;

  46. }


  47. }

至此上面三中情况都予满足。

总结

上面对二叉查找树的操作都已介绍,但是正真使用中,是要结合实际业务进行相关调整来满足自己的需求,不然,一切的优化手段都是假把式。二叉查找树虽然好用,但是它也是有一定要求,在数据量不大的情况下,使用遍历的方式,更加符合我们的要求,所以它使用场景一般是在海量数据的查询,用来提查询效率。


推荐阅读

(点击标题可跳转阅读)

4 张 GIF 图帮助你理解二叉查找树

漫画:什么是二叉堆?



觉得本文有帮助?请分享给更多人

关注「算法爱好者」加星标,修炼编程内功

好文章,我在看❤️

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存