创建、遍历二叉排序树以及删除节点

public class cBinarySortTree {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9,13,11};
        binarySortTree binarySortTree = new binarySortTree();
        for(int i:arr){
            binarySortTree.add(new Node(i));
        }
        binarySortTree.infixOrder();
        binarySortTree.delNode(7);
        System.out.println("删除后");
        binarySortTree.infixOrder();

    }
}
class binarySortTree{
    private Node root;


    //删除节点
    public void delNode(int value){
        if(root == null){
            System.out.println("当前树为空");
            return;
        }
        else if(root.left == null && root.right==null && root.value == value){//只剩根节点时
            root = null;
            return;
        }
        else {
            //首先获取到想要删除的节点
            Node target = search(value);
            if(target == null){
                System.out.println("找不到想要删除的节点");
                return;
            }
            //然后获取到要删除节点的父节点
            Node parent = searchParent(value);
            if(target.left!=null && target.right!=null){//说明要删除的节点的左右都有节点
                //找到要删除节点的右边最小的节点
                Node min = target.right.searchMin();
                delNode(min.value);
                target.value = min.value;
            } else if(target.left == null && target.right==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((target.left !=null && target.right==null)){
                //说明要删除的节点  有一个左子节点
                //判断要删除的节点是父节点的左边还是右边
                if(parent!=null) {
                    if (parent.left != null && parent.left.value == value) {
                        parent.left = target.left;
                    } else if (parent.right != null && parent.right.value == value) {
                        parent.right = target.left;
                    }
                }else {
                    root = target.left;
                }
            }else if((target.left==null && target.right!=null)){
                //说明要删除的节点  有一个右子节点
                //判断要删除的节点是父节点的左边还是右边
                if(parent != null) {
                    if (parent.left != null && parent.left.value == value) {
                        parent.left = target.right;
                    } else if (parent.right != null && parent.right.value == value) {
                        parent.right = target.right;
                    }
                }else {
                    root = target.right;
                }
            }
        }
    }



    //查找要删除的节点
    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 add(Node node){
        if(root == null){
            root = node;
        }else {
            root.add(node);
        }
    }

    public void infixOrder(){
        if(root == null){
            System.out.println("树空");
            return;
        }else {
            root.infixOrder();
        }
    }
}
class Node {
    int value;
    Node left;
    Node right;

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


    public Node searchMin(){
        if(this.left == null && this.right == null){
            return this;
        }else if(this.left !=null){
            return this.left.searchMin();
        }else{
           return this;
        }
    }

    //查找要删除的节点
    public Node search(int value){
        if(value == this.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 Node searchParent(int value){
        if((this.left!=null && this.left.value == value)||(this.right!=null && this.right.value == value)){
            return this;
        }else {
            if(this.left !=null && value<this.value){
                return this.left.searchParent(value);//向左子树递归
            }else if(this.right!=null && value>this.value){
                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.println(this.value);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值