二叉排序树删除节点

/**
 * @Description 二叉排序树的节点
 * @auther Eleven
 * @create 2020-04-09 20:43
 **/
public 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 (this.value>node.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 midPrint(Node root) {
        if (root == null){
            return;
        }
        midPrint(root.left);
        System.out.println(root.value);
        midPrint(root.right);
    }

    //查找节点
    public Node searchNode(Node node) {
        if (this.value == node.value){
            return this;
        }else if (this.value>node.value){
            //当前值大于node的值
            if (this.left == null) return null;
            return   left.searchNode(node);
        }else {
            if (this.right == null) return null;
            return  right.searchNode(node);
        }
    }

    public void deleteNode(Node node) {

    }

    /**
     * 查找父节点
     * @param node
     * @return
     */
    public Node searchParent(Node node) {
        //如果当前节点的左节点不为空,且左节点的值等于查找的node的值 || 如果当前节点的右节点不为空,且右节点的值等于查找的node的值 当前节点即为父节点
        if ((this.left.value==node.value && this.left !=null)||(this.right.value==node.value&& this.right !=null)){
            return this;
        }else{

            if (this.left!=null && this.value>node.value){  //当当前node的value小于当前的往左找
               return this.left.searchParent(node);

            }else if (this.right!=null  && this.value<node.value){ //当当前node的value大于当前的往右找
                return this.right.searchParent(node);
            }
            return null;
        }
    }
}
/**
 * @Description 二叉排序树
 * @auther Eleven
 * @create 2020-04-09 20:43
 **/
public class BinarySortTree {
    Node root;

    public void  add(Node node){
        if (root == null ){
            root=node;
        }else{
            root.add(node);
        }

    }

    public void midPrint() {
        if (root !=null){
            root.midPrint(root);
        }
    }

    public Node searchNode(Node node){
        if (root==null){
            return null ;
        }else {
            return root.searchNode(node);
        }
    }

    /**
     * 删除节点
     * @param node
     */
    public void deleteNode(Node node){
        if (root == null){
            return;
        }else {
            //找到这个节点
            Node target = searchNode(node);
            if (target==null){
                return;
            }
            //找到这个节点的父节点
            Node parent = searchParent(node);
            //删除叶子节点
            if (target.left==null && target.right==null){
                //删除左节点
               if (target==parent.left){
                   parent.left=null;
               }else {
                   //删除右节点
                   parent.right=null;
               }
            }else if (target.left!=null && target.right!=null){
                 //找到右子树中最小的节点
                Node minNode = findMinNode(target.right);
                //替换目标节点的值
                target.value=minNode.value;

            }else {//删除只有一个子节点的节点
                if (target.left!=null){//左子节点不为空
                    //删除左节点
                    if (target==parent.left){
                        parent.left=target.left;
                    }else {
                        //删除右节点
                        parent.right=target.left;
                    }
                }else {//右子节点不为空
                    //删除左节点
                    if (target==parent.left){
                        parent.left=target.right;
                    }else {
                        //删除右节点
                        parent.right=target.right;
                    }
                }

            }
        }
    }

    /**
     * 找一颗数中的最小节点
     * @param node
     * @return
     */
    private Node findMinNode(Node node) {
        Node target = node;
        while (target.left!=null){
            target = target.left;
        }
        //删除最小节点
        deleteNode(target);
        return target;
    }


    /**
     * 搜索父节点
     * @param node
     * @return
     */
    public Node searchParent(Node node){
        if (root == null){
            return null;
        }else{
            return root.searchParent(node);
        }
    }
}
    public static void main(String[] args) {
        BinarySortTree binarySortTree = new BinarySortTree();
        int[] arr = new int[]{7,3,10,12,5,1,9};
        for (int i:arr){
            binarySortTree.add(new Node(i));
        }
        binarySortTree.midPrint();
       // System.out.println(binarySortTree.searchNode(new Node(1)).value);
        System.out.println("--------------------");
        binarySortTree.deleteNode(new Node(3));
        binarySortTree.midPrint();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值