java数据结构-二叉排序树(删除只有一个子节点的节点,删除有两个节点的节点)

在二叉排序搜索树中,要删除有子节点的节点的话。
有两种情况
第一种-要删除的节点有一个子节点:
1、判断子节点是左节点还是右节点
2、判断出来是左节点的话
2.1、看看其父节点的左节点是不是我要删除的节点
2.2、是的话就将其父节点的左节点设置为其左子节点
2.3、如果不是左节点的话,就将其父节点的右节点设置为其左子节点
3、如果是右子节点的话,同理。但是要注意将左右设置清楚
第二种-要删除的节点有两个子节点:
1、找到其的后继节点
2、将其后继节点的值赋值给要删除的节点的值

代码实现

节点类

package com.demo4;

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

    public void add(Node node) {
        if(node == null){
            return;
        }
        if(node.value > this.value){
            if(this.right == null){
                this.right = node;
            }
            else{
                this.right.add(node);
            }
        }
        if(node.value < this.value){
            if(this.left == null){
                this.left = node;
            }
            else{
                this.left.add(node);
            }
        }
    }

    public void middleshow(Node node) {
        if(node == null){
            return;
        }
        middleshow(node.left);
        System.out.println(node.value);
        middleshow(node.right);
    }

    public Node search(int value) {
        if(this.value == value){
            return this;
        }
        else if(value < this.value){
            if(this.left == null) {
                return null;
            }
            return left.search(value);
        }
        else{
            if(this.right == null){
                return null;
            }
            return right.search(value);
        }
    }

    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.right != null){
                return this.right.searchParent(value);
            }
            else{
                return null;
            }
        }
    }
}

二叉排序树类

package com.demo4;

public class BinarySearchTree {
    Node root;
    public void add(Node node){
        if(root == null){
            root = node;
        }
        else{
            root.add(node);
        }
    }
    public void middleshow(){
        if(root == null){
            System.out.println("This tree is Empty.");
            return;
        }
        else{
            root.middleshow(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 delete(int value){
        if(root == null){
            return;
        }

        else{
            Node target = search(value);
            if(target == null){
                return;
            }
            Node parent = searchParent(value);
            if(target.left == null && target.right == null){
                if(parent.left.value == value){
                    parent.left = null;
                }
                else{
                    parent.right = null;
                }
            }//有两个子节点
            else if(target.left != null && target.right != null){
                int min = deleteMin(target.right);
                target.value = min;
            }
            else {
                if(target.left != null){
                    if(parent.left.value == value){
                        parent.left = target.left;
                    }
                    else{
                        parent.right = target.left;
                    }
                }
                else{
                    if(parent.right.value == value){
                        parent.right = target.right;
                    }
                    else{
                        parent.left = target.right;
                    }
                }
            }

        }


    }

    private int deleteMin(Node node) {
        Node target = node;
        while (target.left != null){
            target = node.left;
        }
        delete(target.value);
        return target.value;
    }
}

测试类

public class testBST {
    public static void main(String[] args) {
        int [] arr = new int[] {7,3,10,12,5,1,9};
        BinarySearchTree bst = new BinarySearchTree();
        for(int i : arr){
            bst.add(new Node(i));
        }
        System.out.println(bst.root.value);
        bst.delete(bst.root.value);
        System.out.println("**************************************************删除后");
        System.out.println(bst.root.value);

测试结果
我删除的是root节点来测试
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值