二叉排序树(BST)

class BinarySortTree

package DataStructures.tree;

/**
 * 二叉排序树(BST)
 * @author coffee
 * @Date 2021-04-04 18:43
 */
public class BinarySortTree {

    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 Node2(arr[i]));
        }

        binarySortTree.delNode(10);
        binarySortTree.infixOrder();

    }

    private Node2 root;

    //删除节点
    public void delNode(int value){
        if (root == null){
            return;
        }else {
            Node2 target = search(value);//找到要删除的节点
            if (target == null){//没有找到要删除的节点
                return;
            }
            if (root.left == null && root.right == null){//只有根节点
                root = null;
                return;
            }
            Node2 parent = searchParent(value);//找到要删除的节点的父节点

            if (target.left == null && target.right == null) {//删除叶子节点
                if (parent.left != null && target == parent.left) {//当前节点是父节点的左子节点
                    parent.left = null;
                } else if (parent.right != null && target == parent.right) {//当前节点是父节点的右子节点
                    parent.right = null;
                }
            }else if (target.left == null || target.right == null){//删除只有一颗子树的节点
                if (target.right == null){//子节点是左子节点
                    if (parent == null){//当前节点为根节点,并有左子节点
                        root = target.left;
                    }
                    if (target == parent.left){//当前节点是父节点的左子节点
                        parent.left = target.left;
                    }else {//当前节点是父节点的右子节点
                        parent.right = target.left;
                    }
                }else{//子节点是右子节点
                    if (parent == null){//当前节点为根节点,并有右子节点
                        root = target.right;
                    }
                    if (target == parent.left){//当前节点是父节点的左子节点
                        parent.left = target.right;
                    }else {//当前节点是父节点的右子节点
                        parent.right = target.right;
                    }
                }
            } else {//删除有两颗子树的节点
                target.value = delRightTreeMin(target.right);
            }
        }
    }
    //返回以node为根节点的二叉排序树的最小节点的值
    //删除以node为根节点的二叉排序树的最小节点
    public int delRightTreeMin(Node2 node){
        while (node.left != null){//查找
            node = node.left;
        }
        delNode(node.value);//删除
        return node.value;
    }
    //查找要删除的节点的父节点
    public Node2 searchParent(int value) {
        return root.searchParent(value);
    }

    //查找要删除的节点
    public Node2 search(int value){
        return root.search(value);
    }
    //添加节点
    public void add(Node2 node){
        if (root == null){
            root = node;
        }else {
            root.add(node);
        }
    }
    //中序遍历
    public void infixOrder(){
        if (root != null){
            root.infixOrder();
        }else {
            System.out.println("空,不可遍历");
        }
    }
}
//节点
class Node2{
    int value;
    Node2 left;
    Node2 right;

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

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

    /**
     * 查找要删除的节点的父节点
     */
    public Node2 searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.left.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.right.value && this.right != null){
                return this.right.searchParent(value);
            }else {
                return null;
            }
        }
    }

    /**
     * 查找要删除的节点
     */
    public Node2 search(int value){
        if (this.value == value){
            return this;
        }else 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);
        }
    }

    /**
     * 添加节点
     */
    public void add(Node2 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();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值