二叉排序树删除

13.7.2 二叉排序树的删除

二叉排序树情况分为三种:

  1. 删除叶子节点
    1. 需要先找到要删除的节点 targetNode
    2. 找到targetNode的父节点 parent
    3. 确定 targetNodeparent的左子节点还是右子节点
    4. 根据前面的情况来对应删除
      1. 左子节点:parent.left = null
      2. 右子节点:parent.right = null
  2. 删除只有一棵子树的节点
    1. 需要先找到要删除的节点 targetNode
    2. 找到targetNode的父节点 parent
    3. 确定 targetNodeparent的左子节点还是右子节点
    4. targetNodeparent左子节点还是右子节点
      1. 如果targetNodeparent的左子节点parent.left = targetNode.left(targetNode 有左子节点) || targetNode.right(targetNode 有右子节点)
      2. 如果targetNodeparent的右子节点 parent.right = target.right (targetNode 有左子节点)|| targetNode.left(targetNode 有左子节点)
  3. 删除有两棵子树的节点
    1. 需要先找到要删除的节点 targetNode
    2. 找到targetNode的父节点 parent
    3. targetNode的右子树找到最小的节点
    4. 用一个临时变量,将最小结点的值保存 temp
    5. 删除这个最小结点
    6. targetNode.value = temp
package binarysorttree;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9};
        BinarySortTree binarySortTree = new BinarySortTree();
        // 循环添加节点到二叉排序树
        for (int i = 0; i < arr.length; i++){
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("二叉树的创建");
        binarySortTree.infixOrder();

        System.out.println("删除结点");
//        binarySortTree.delNode(1);
        binarySortTree.delNode(7);
//        binarySortTree.delNode(10);
        binarySortTree.infixOrder();
    }
}
// 创建二叉排序数
class BinarySortTree {
    private Node root;
    // 查找要删除的结点
    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 {
            // 先去查找到要删除的点
            Node targetNode = search(value);
            if (targetNode == null){
                return; // 没找到直接返回
            }
            // targetNode 没有父节点 等价于 跟结点 等价于 长度只有一的树
            // 长度只有一个跟结点
            if(root.left == null && root.right == null){
                root = null;
                return;
            }
            // 在去查找要删除点的父节点
            Node parent = searchParent(value);
            // 如果要删除的结点是叶子结点
            if (targetNode.left == null && targetNode.right == null){
                // 判断 targetNode 是 parent 的左子节点还是右子节点
                if (parent.left != null && parent.left == targetNode){
                    parent.left = null;
                }else if (parent.right != null && parent.right == targetNode){
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right == null){
                // 要删除结点只存在左子树
                if (parent.left != null && parent.left == targetNode){
                    parent.left = targetNode.left;
                }else if (parent.right != null && parent.right == targetNode){
                    parent.right = targetNode.right;
                }
            } else if (targetNode.right != null && targetNode.left == null){
                // 删除结点只存在右子树
                if (parent.left != null && parent.left.value == value){
                    parent.left = null;
                }else if (parent.right != null && parent.right.value == value){
                    parent.right = null;
                }
            }  else if (targetNode.left != null && targetNode.right != null){
                // 两种思路,可以从右子树找最小的,也可以从左子树找最大的
                // 下面是右子树最小的
                int mincalue = delRightTreeMin(targetNode.right);
                targetNode.value = mincalue;
            }
        }
    }

    /**
     *  1.返回的 以node为跟结点的二叉排序树的最小的节点的值
     *  2. 删除这个最小结点
     * @param node  传入的结点(当作二叉排序树的跟结点)
     * @return  返回是以node为跟结点的二叉排序树的最小的节点的值
     */
    public int delRightTreeMin(Node node){
        Node target = node;
        // 循环查找左节点,就会找到最小值
        while (target.left != null){
            target = target.left;
        }
        // 这时,target 就指向了最小的值
        // 删除最小结点的值
        delNode(target.value);
        return target.value;
    }

    // 添加节点
    public void add(Node node){
        if (root == null){
            root = node;
        }else{
            root.add(node);
        }
    }
    // 中序遍历
    public void infixOrder(){
        if (root != null){
            root.infixOrder();
        } else {
            System.out.println("当前二叉排序树为空,不能遍历");
            return;
        }
    }

}
class Node {
    int value;
    Node left;
    Node right;

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

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    // 查找要删除的结点

    /**
     *
     * @param value 希望删除的结点的值
     * @return  如果找到返回该结点,否则返回null
     */
    public Node search(int value){
        if (this.value == value){
            // 说明该点就是要找的结点
            return this;
        }else if (this.value < value && this.right != null){
            return this.right.search(value);
        }else if (this.value > value && this.left != null){
            return this.left.search(value);
        }else{
            return null;
        }
    }
    // 查找要删除结点的父节点

    /**
     *
     * @param value 要查找的结点的值
     * @return  返回的是要删除点的父节点,如果没有,则返回null
     */
    public Node searchParent(int value){
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)){
            return this;
        }else if(this.value > value && this.left != null){
            return this.left.searchParent(value);
        }else if (this.value < 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.print(this.value+"\t");
        if (this.right != null){
            this.right.infixOrder();
        }
    }
}

注意事项:

上面的代码,如果出现根节点只有一棵子树,然后删除根节点的情况的时候,会出现空指针异常,因为我们的判断是根据要查找的结点的父节点不为空的情况下进行的,因此还要考虑为空的情况,那么,以下版本是更正的。

package binarysorttree;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9};
        BinarySortTree binarySortTree = new BinarySortTree();
        // 循环添加节点到二叉排序树
        for (int i = 0; i < arr.length; i++){
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("二叉树的创建");
        binarySortTree.infixOrder();

        System.out.println("删除结点");
        binarySortTree.delNode(2);
        binarySortTree.delNode(5);
        binarySortTree.delNode(9);
        binarySortTree.delNode(12);
        binarySortTree.delNode(7);
        binarySortTree.delNode(3);
        binarySortTree.delNode(10);
        binarySortTree.delNode(1);
        binarySortTree.infixOrder();
    }
}
// 创建二叉排序数
class BinarySortTree {
    private Node root;
    // 查找要删除的结点
    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 {
            // 先去查找到要删除的点
            Node targetNode = search(value);
            if (targetNode == null){
                return; // 没找到直接返回
            }
            // targetNode 没有父节点 等价于 跟结点 等价于 长度只有一的树
            // 长度只有一个跟结点
            if(root.left == null && root.right == null){
                root = null;
                return;
            }
            // 在去查找要删除点的父节点
            Node parent = searchParent(value);
            // 如果要删除的结点是叶子结点
            if (targetNode.left == null && targetNode.right == null){
                // 判断 targetNode 是 parent 的左子节点还是右子节点
                if (parent.left != null && parent.left == targetNode){
                    parent.left = null;
                }else if (parent.right != null && parent.right == targetNode){
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right == null){
                if (parent != null){
                    // 要删除结点只存在左子树
                    if (parent.left != null && parent.left == targetNode){
                        parent.left = targetNode.left;
                    }else if (parent.right != null && parent.right == targetNode){
                        parent.right = targetNode.right;
                    }
                } else {
                    root = targetNode.left;
                }

            } else if (targetNode.right != null && targetNode.left == null){
                if (parent != null){
                    // 删除结点只存在右子树
                    if (parent.left != null && parent.left.value == value){
                        parent.left = null;
                    }else if (parent.right != null && parent.right.value == value){
                        parent.right = null;
                    } else if(parent == null){ // 当要删除的是根节点,那么它就没有父节点
                        // 直接让它的下面的一颗子树称为根节点就可以
                        root = targetNode.right;
                    }
                }else{
                    root = targetNode.right;
                }

            }  else if (targetNode.left != null && targetNode.right != null){
                // 两种思路,可以从右子树找最小的,也可以从左子树找最大的
                // 下面是右子树最小的
                int mincalue = delRightTreeMin(targetNode.right);
                targetNode.value = mincalue;
            }
        }
    }

    /**
     *  1.返回的 以node为跟结点的二叉排序树的最小的节点的值
     *  2. 删除这个最小结点
     * @param node  传入的结点(当作二叉排序树的跟结点)
     * @return  返回是以node为跟结点的二叉排序树的最小的节点的值
     */
    public int delRightTreeMin(Node node){
        Node target = node;
        // 循环查找左节点,就会找到最小值
        while (target.left != null){
            target = target.left;
        }
        // 这时,target 就指向了最小的值
        // 删除最小结点的值
        delNode(target.value);
        return target.value;
    }

    // 添加节点
    public void add(Node node){
        if (root == null){
            root = node;
        }else{
            root.add(node);
        }
    }
    // 中序遍历
    public void infixOrder(){
        if (root != null){
            root.infixOrder();
        } else {
            System.out.println("当前二叉排序树为空,不能遍历");
            return;
        }
    }

}
class Node {
    int value;
    Node left;
    Node right;

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

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    // 查找要删除的结点

    /**
     *
     * @param value 希望删除的结点的值
     * @return  如果找到返回该结点,否则返回null
     */
    public Node search(int value){
        if (this.value == value){
            // 说明该点就是要找的结点
            return this;
        }else if (this.value < value && this.right != null){
            return this.right.search(value);
        }else if (this.value > value && this.left != null){
            return this.left.search(value);
        }else{
            return null;
        }
    }
    // 查找要删除结点的父节点

    /**
     *
     * @param value 要查找的结点的值
     * @return  返回的是要删除点的父节点,如果没有,则返回null
     */
    public Node searchParent(int value){
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)){
            return this;
        }else if(this.value > value && this.left != null){
            return this.left.searchParent(value);
        }else if (this.value < 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.print(this.value+"\t");
        if (this.right != null){
            this.right.infixOrder();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值