二叉排序树的建立,删除,添加,遍历

这篇博客介绍了如何使用Java实现二叉排序树(BST)的基本操作,包括插入、删除和中序遍历。代码展示了如何在BST中查找节点的父节点以及找到最小值节点来处理删除操作。通过示例展示了不同情况下删除节点的逻辑,包括节点无子节点、只有一个子节点和有两个子节点的情况。
package com.jiahui.java;

/**7,3,10,12,5,1,9
 * @author shkstart
 * @create 2022-11-25 19:34
 */
public class Sort {
    public static void main(String args[]){
        SortBinaryTree tree=new SortBinaryTree(new Node(7));
        tree.add(new Node(3));
        Node node1=new Node(10);
        tree.add(node1);
        tree.add(new Node(12));
        tree.add(new Node(5));
        tree.add(new Node(1));
        tree.add(new Node(9));
        Node node=new Node(2);
        tree.add(node);
        tree.delete(node);
        tree.delete(node1);
        tree.inforOrder();
    }
}

class SortBinaryTree{
    Node root;

    public SortBinaryTree(Node root) {
        this.root = root;
    }

    public void inforOrder(){
        if (root==null){
            System.out.println("二叉排序树为空,无法遍历");
        }else {
            root.infoOrder();
        }
    }
    public void delete(Node node){
        if (root!=null){
            if (root==node){
                //左右都空
                if (root.getLeft()==null && root.getRight()==null){
                    root=null;
                    return;
                }
                //左右都不空
                if (root.getRight()!=null && root.getRight()!=null){
                    Node rightTree = root.getRight().getRight();
                    Node minNode = rightTree.minValue();
                    Node parentOfMin = root.searchParent(minNode);
                    if (parentOfMin.getLeft()!=null &&parentOfMin.getLeft()==minNode){
                        parentOfMin.setLeft(null);
                    }else {
                        parentOfMin.setRight(null);
                    }
                    root.setValue(minNode.getValue());
                    return;
                }
                //左空
                if (root.getLeft()!=null){
                    root=root.getLeft();
                    return;
                }
                //右空
                if (root.getRight()!=null){
                    root=root.getRight();
                    return;
                }

            }else {
                root.delete(node);
            }
        }else{
            System.out.println("二叉树为空,无法删除");
        }
    }
    public void add(Node node){
        if (root==null){
            root=node;
        }else {
            root.add(node);
        }
    }
}

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

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

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

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

    /**
     * 删除
     * @param node 要删除的节点
     */
    public void delete(Node node){
        int i=node.getValue();


        //左子树等于要找的node
        if (this.getLeft()!=null && this.getLeft().getValue()==i){
            //都为空
            if (this.getLeft().getLeft()==null && this.getLeft().getRight()==null){
              this.setLeft(null);
              return;
            }
            //都不为空
            if (this.getLeft().getLeft()!=null&&this.getLeft().getRight()!=null){
                Node rightTree = this.getLeft().getRight();
                Node minNode = rightTree.minValue();
//                Node parent = rightTree.searchParent(minNode);
//                if (parent.getLeft()==minNode){
//                    parent.delete();
//                }else {
//
//                }
                Node parentOfMin = this.searchParent(minNode);
                if (parentOfMin.getLeft()!=null &&parentOfMin.getLeft()==minNode){
                    parentOfMin.setLeft(null);
                }else {
                    parentOfMin.setRight(null);
                }
                this.getLeft().setValue(minNode.getValue());
                return;
            }
            //左不为空
            if (this.getLeft().getLeft()!=null ){
                this.setLeft(this.getLeft().getLeft());
                return;
            }
            //右不为空
            if (this.getLeft().getRight()!=null){
                this.setLeft(this.getLeft().getRight());
                return;
            }
        }

        //右子树等于
        if (this.getRight()!=null && this.getRight().getValue()==i){
            //都为空
            if (this.getRight().getLeft()==null && this.getRight().getRight()==null){
                this.setRight(null);
                return;
            }
            //都不为空
            if (this.getRight().getLeft()!=null && this.getRight().getRight()!=null){
                Node rightTree = this.getRight().getRight();
                Node minNode = rightTree.minValue();

                Node parentOfMin = this.searchParent(minNode);
                if (parentOfMin.getLeft()!=null &&parentOfMin.getLeft()==minNode){
                    parentOfMin.setLeft(null);
                }else {
                    parentOfMin.setRight(null);
                }
                this.getRight().setValue(minNode.getValue());
                return;
            }
            //左不为空
            if (this.getRight().getLeft()!=null ){
                this.setRight(this.getRight().getLeft());
                return;
            }
            //右不为空
            if (this.getRight().getRight()!=null){
                this.setRight(this.getRight().getRight());
                return;
            }
        }
        if (this.getLeft()!=null){
            this.getLeft().delete(node);
        }
        if (this.getRight()!=null){
            this.getRight().delete(node);
        }


    }
    public Node searchParent(Node temp){
        if (this.getLeft()==temp||this.getRight()==temp){
            return this;
        }
        if (this.getLeft()!=null && temp.getValue()<this.getValue()){
            return this.getLeft().searchParent(temp);
        }
        if (this.getRight()!=null && temp.getValue()>this.getValue()){
            return this.getRight().searchParent(temp);
        }
        return null;

    }
    public Node minValue(){
        Node node=this;
        while (node.getLeft()!=null){
            node=node.getLeft();
        }
        return node;

    }

    /**
     * 中序遍历
     */
    public void infoOrder(){
        if (this.getLeft()!=null){
            this.getLeft().infoOrder();
        }
        System.out.println(this);
        if (this.getRight()!=null){
            this.getRight().infoOrder();
        }
    }
    //添加
    public void add(Node node){
        if (node.getValue()>this.getValue()){
            if (this.getRight()==null){
                this.setRight(node);
            }else {
                this.getRight().add(node);
            }
        }
        if (node.getValue()<=this.getValue()){
            if (this.getLeft()==null){
                this.setLeft(node);
            }else {
                this.getLeft().add(node);
            }
        }
    }

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

    public int getValue() {
        return value;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

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

共勉!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值