二叉排序树删除节点详解

说明

  1. 二叉排序树有着比数组查找快,比链表增删快的优势,比较常见
  2. 二叉排序树要删除节点,首先要找到该节点和该节点对应的父节点,因为只根据当前节点是不能删除节点本身的,因此需要找到父节点
  3. 二叉排序树删除节点,根据节点所在的位置不同,需要分为三种情况
  4. 即要删除的节点是叶子节点,要删除的节点只有一颗子树的情况和要删除的节点有两颗子树的情况
  5. 考虑第一种情况,即要删除的节点是叶子节点
  6. 直接找到要删除节点的父节点,然后置空即可
  7. 考虑第二种情况,即要删除的节点有一颗子树
  8. 先找到该节点和该节点的父节点,根据该节点是父节点的左子节点还是右子节点和该节点有左子节点还是有有子节点,应该分为四种情况讨论,每种情况改变父节点的引用即可实现删除
  9. 考虑第三种情况,即要删除的节点有两颗子树
  10. 这种情况删除思路应该为找到当前节点右子树的的最大值节点或者找到当前节点左子树的最小值节点,先将这个节点删除,再用这个节点替换当前节点
  11. 实质操作先记录删除子树的最大最小值节点的值,然后删除这个节点,最后用记录的这个值替换掉要删除的节点,实现删除有左右子树节点的思路
  12. 源码及思路见下

源码及分析

package algorithm.tree.bst;

/**
 * @author AIMX_INFO
 * @version 1.0
 */
public class BinarySortTree {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BST bst = new BST();
        //循环添加节点
        for (int i = 0; i < arr.length; i++) {
            bst.add(new Node(arr[i]));
        }
        //中序遍历查看
        System.out.println("中序遍历");
        bst.infixOrder();
        System.out.println("删除叶子节点");
        bst.delNode(7);
        bst.infixOrder();
    }
}

//二叉排序树
class BST {
    private Node root;

    //中序遍历
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("树是空的");
        }
    }

    //添加节点
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    //查找某一节点
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找某一节点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //删除节点
    public void delNode(int value) {
        //先判断是否为空树
        if (root == null) {
            return;
        } else {
            //如果树不为空,再判断树是否只有一个空节点
            if (root.left == null && root.right == null && root.value == value) {
                root = null;
                return;
            }
            //否则先查找要删除的节点
            Node target = search(value);
            //判断要删除节点是否存在
            if (target == null) {
                return;
            }
            //如果存在则再找到要删除节点的父节点
            Node parent = searchParent(value);
            //然后根据要删除的节点分情况删除
            //如果要删除的节点是叶子节点
            if (target.left == null && target.right == null) {
                //判断target是父节点的左子节点还是右子节点
                //如果是左子节点
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                }
                //如果是左子节点
                if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
                //如果要删除的节点有两个子节点
            } else if (target.left != null && target.right != null) {
                int minVal = delRightNodeMin(target.right);
                target.value = minVal;

                //否则要删除的节点只有一个子节点
            } else {
                //判断要删除的节点有左子节点还是右子节点
                //target的左子节点不为空
                if (target.left != null){
                    //判断target是父节点的左子树还是右子树
                    //左子树
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = target.left;
                        } else {
                            //右子树
                            parent.right = target.left;
                        }
                    }else {
                        root = target.left;
                    }
                    //target的右子节点不为空
                }else {
                    //同理
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = target.right;
                        } else {
                            parent.right = target.right;
                        }
                    }else {
                        root = target.right;
                    }
                }
            }
        }
    }

    /**
     * 查找当前二叉排序树的最小节点并删除
     * @param node  当前二叉排序树
     * @return 返回最小节点的值
     */
    public int delRightNodeMin(Node node){
        //辅助变量用于遍历二叉排序树
        Node target = node;
        //循环查找最小节点
        while (target.left != null){
            target = target.left;
        }
        //循环结束时已经找到
        //删除当前节点
        delNode(target.value);
        //返回当前节点的值
        return target.value;
    }

}

//节点
class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    //查找某一节点

    /**
     * @param value 要查找的节点的值
     * @return 返回查找的结果
     */
    public Node search(int value) {
        //如果要查找的节点就是当前节点,直接返回
        if (value == this.value) {
            return this;
        }
        //判断要查找的节点的value与当前节点的value的大小关系
        //向左递归查找
        if (value < this.value) {
            //判断左子树是否为空
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            //向右递归查找
            //判断右子树是否为空
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }
    //查找要删除节点的父节点

    /**
     * @param value 要删除的节点的值
     * @return 返回查找的结果
     */
    public Node searchParent(int value) {
        //判断当前节点是不是要查找节点的父节点
        if ((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        } else {
            //如果不是则向左向右递归查找
            //向左递归
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
                //向右递归
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                //否则没有找到
                return null;
            }
        }
    }

    //递归添加节点的方法
    public void add(Node node) {
        //数据校验
        if (node == null) {
            return;
        }
        //根据要添加的节点的值和当前节点值的大小判断节点要添加的位置
        if (node.value < this.value) {
            //如果当前左子节点为空,则直接添加
            if (this.left == null) {
                this.left = node;
            } else {
                //否则递归添加
                this.left.add(node);
            }
        } else {
            //同理
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}