二叉排序树BST代码java版

package bst;

import java.math.BigInteger;

/**
 * @author yzy
 * @version 1.0
 */
//应该尽量避免相同的情况
public class BST {
    //main
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9,0};
        BinarySortTree bst = new BinarySortTree();
        for(int value:arr){
            bst.add(new Node(value));
        }
        //测试
        bst.delete(7);
        bst.minOrder();

    }
}
//树
class BinarySortTree{
    //node类中写添加等,是底层代码,在bst树中需要对其进行封装,使得在主函数能直接调用bst的方法
    Node root;
    //添加
    public void add(Node node){
        if(node == null){
            return;
        }
        if(root == null){
            root = node;
        }else {
            root.add(node);
        }
    }
    //遍历
    public void minOrder(){
        if(root != null){
            root.midOrder();
        }
    }
    //查找待删节点
    public Node search(int value){
        if(root == null){
            return null;
        }else {
            return root.search(value);
        }
    }
    //查找父节点
    public Node searchParentNode(int value){
        if(root == null){
            return null;
        }else {
            return root.searchParentNode(value);
        }
    }
    //删除
    public void delete(int value){
        Node target = root.search(value);
        Node parent = root.searchParentNode(value);
        Node temp;
        if(target == null || root == null){
            System.out.println("不可删除");
            return;
        }else {
            if(target.left == null && target.right == null){//情况一叶子结点
                //有了target不一定有parent
                if(parent == null){
                    root = null;
                }
                if(parent.value > target.value){
                    parent.left = null;
                }else {
                    parent.right = null ;
                }
            }else if(target.left == null && target.right != null){//情况二
                //有了target不一定有parent
                if(parent == null){
                    root = target.right;
                }
                if(parent.value > target.value){
                    parent.left = target.right;
                }else {
                    parent.right = target.right ;
                }

            }else if(target.left != null && target.right == null){//情况三
                //有了target不一定有parent
                if(parent == null){
                    root = target.left;
                }
                if(parent.value > target.value){
                    parent.left = target.left;
                }else {
                    parent.right = target.left ;
                }

            }else {//情况四,两个子树的结点
                temp = target.right;//表示右子树的最小值
                while(true){
                    if(temp.left != null){
                        temp = temp.left;
                        continue;
                    }else {
                        break;
                    }
                }
                Node tempParent = root.searchParentNode(temp.value);
                target.value = temp.value;
                //删除叶子结点
                if(temp.value < tempParent.value){
                    tempParent.left = null;
                }else {
                    tempParent.right = null;
                }
            }

        }
    }
}
//节点类
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.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 midOrder(){
        if(this.left != null){
            this.left.midOrder();
        }
        System.out.println(this);
        if(this.right != null){
            this.right.midOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    //查找
    public Node search(int value){
        if(this.value == value){
            return this;
        }
        if(this.left !=  null){
            if(value < this.value){
                return this.left.search(value);
            }
        }
        if(this.right !=  null){
            if(value >= this.value){
                return this.right.search(value);
            }
        }
        return null;
    }
    //查找待删节点的父节点
    public Node searchParentNode(int value){
        if (this.search(value) == null){
            return null;
        }
        //根节点的父节点返回null
        if(value == this.value){
            return null;
        }
        if((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)  ){
            return this;
        }else if(value < this.value){
            return this.left.searchParentNode(value);
        }else {
            return this.right.searchParentNode(value);
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值