数据结构与算法练习---二叉排序树增删查练习代码

本文详细介绍了二叉排序树的增删查操作实现代码,包括添加元素、中序遍历、查找节点、查找父节点及删除节点等功能,通过具体示例展示了二叉排序树的基本操作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package binarysorttree;

/**
 * 二叉排序树增删查练习代码
 */
public class BinarySortTreeDemo {

    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍历结果为: ");
        binarySortTree.infixOrder();
        Node node = binarySortTree.search(3);
        System.out.println("查找结果为: " + node);
        Node parent = binarySortTree.searchParent(7);
        System.out.println("父节点为: " + parent);
        binarySortTree.deleteNode(2);
        binarySortTree.deleteNode(5);
        binarySortTree.deleteNode(9);
        binarySortTree.deleteNode(12);
        binarySortTree.deleteNode(7);
        binarySortTree.deleteNode(3);
        binarySortTree.deleteNode(10);
        binarySortTree.deleteNode(1);
        System.out.println("删除节点后的遍历结果: ");
        binarySortTree.infixOrder();
    }

}

class BinarySortTree{

    private Node root;

    //删除结点
    public void deleteNode(int value){
        //若根节点为空则直接结束方法
        if(root == null){
            return;
        }else{
            //若根节点不为空,则通过search方法获取当前节点
            Node targetNode = search(value);
            //如果要删除的结点不存在,则方法直接结束
            if(targetNode == null){
                return;
            }
            //如果要删除的结点存在,则继续获取他的父节点
            Node parent = searchParent(value);
            //若targetNode != null 且该树只有一个根节点则目标节点只能是根节点
            if(root.getLeft() == null && root.getRight() == null){
                //若整棵树只有一个根节点,且要删除该节点,则直接将该节点置为空即可
                root = null;
                return;
            }
            //分情况讨论, 1、要删除的结点是叶子节点  2、要删除的结点是只有一个子树的结点  3、要删除的结点是由两个子树的结点
            if(targetNode.getLeft() == null && targetNode.getRight() == null){
                //若目标结点的左右子树均为空,则可能有两种情况  1、该节点是叶子节点  2、该树只有一个根节点,且该节点是要删除的目标结点
                //上面已经排除了情况2   在情况一的状况下,叶子节点的父节点必定不可能为空
                //若要删除叶子节点,则将该节点的所在的父节点的某一子树置为空
                if(parent.getLeft() != null && parent.getLeft().getValue() == value){
                    //要删除的结点为父节点的左子结点
                    parent.setLeft(null);
                }
                if(parent.getRight() != null && parent.getRight().getValue() == value){
                    //要删除的结点为父节点的右子结点
                    parent.setRight(null);
                }
            }else if(targetNode.getLeft() != null && targetNode.getRight() != null){
                //若待删除结点的左右子树均不为空
                //则不需要考虑待删除的结点是否为根节点,也不用考虑parentNode是否为空
                //要删除拥有两个子树的结点则需要找到其左子树的最大值或右子树的最小值来替换现节点的值
                int min = getMinValue(targetNode.getRight());
                targetNode.setValue(min);
            }else{
                //排除上面两种情况后余下的情况均为目标节点有单一子树的情况
                //此种情况下需要考虑待删除结点是否为根节点  因为删除此种结点需要使用parent结点
                //若目标结点的左子树不为空
                if(targetNode.getLeft() != null){
                    //若要删除的结点为根节点,即parent == null  则直接将子节点替换根节点
                    if(parent == null){
                        root = targetNode.getLeft();
                    }else{
                        //若待删除结点为父节点的左子节点
                        if(parent.getLeft().getValue() == value){
                            parent.setLeft(targetNode.getLeft());
                        }else{
                            //若待删除结点为父节点的左子节点
                            parent.setRight(targetNode.getLeft());
                        }
                    }
                }else{
                    //若目标结点的右子树不为空
                    if(parent == null){
                        root = targetNode.getRight();
                    }else{
                        //若待删除结点为父节点的左子节点
                        if(parent.getLeft().getValue() == value){
                            parent.setLeft(targetNode.getRight());
                        }else{
                            //若待删除结点为父节点的右子节点
                            parent.setRight(targetNode.getRight());
                        }
                    }
                }
            }
        }
    }

    //查询某个结点左子树的最小值
    public int getMinValue(Node node){
        //找到最小值的结点,并将其引用给target
        Node target = node;
        while(target.getLeft() != null){
            target = target.getLeft();
        }
        //删除该拥有最小值的结点
        deleteNode(target.getValue());
        //将最小值返回
        return target.getValue();
    }

    //添加元素
    public void add(Node node){
        if(root == null){
            this.root = node;
        }else{
            this.root.add(node);
        }
    }

    //中序遍历
    public void infixOrder(){
        if(root != null){
            this.root.infixOrder();
        }else {
            System.out.println("根节点为空,无法遍历");
        }
    }

    //根据value值查找结点
    public Node search(int value){
        if(this.root == null){
            return null;
        }
        return this.root.search(value);
    }

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

class Node{

    private int value;
    private Node left;
    private Node right;

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

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

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

    //添加节点
    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 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 Node search(int value){
        if(this.value == value){
            return this;
        }else if(this.value > value){
            if(this.left == null){
                return null;
            }
            return this.left.search(value);
        }else{
            if(this.right == null){
                return null;
            }
            return this.right.search(value);
        }
    }

    //中序遍历    二叉排序树中序遍历后的结果为从小到大的有序数列
    public void infixOrder(){
        if(this.left != null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right != null){
            this.right.infixOrder();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值