java平衡二叉树增删改查遍历代码实现

package drug.binarySortTree;

/**
 * @author Drug
 * @create 2020-05-08 16:10
 */
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9,2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i : arr) {
            binarySortTree.add(new Node(i));
        }
        binarySortTree.fixOrder();
        System.out.println("删除后");
        binarySortTree.delete(2);
        binarySortTree.delete(5);
        binarySortTree.delete(9);
        binarySortTree.delete(12);
        binarySortTree.delete(7);
        binarySortTree.delete(3);
        binarySortTree.delete(10);
        binarySortTree.delete(1);
        binarySortTree.fixOrder();
    }

}

class BinarySortTree{
    //根节点
    Node root;

    /**
     * 添加节点
     * @param node
     */
    public void add(Node node){
        //根节点为空
        if(root == null){
            root = node;
        }else{
            //根节点不为空
            root.add(node);
        }
    }

    /**
     * 中序遍历
     */
    public void fixOrder(){
        if(root == null){
            System.out.println("空树");
        }else{
            root.fixOrder();
        }
    }

    /**
     * 查找节点
     * @param value
     * @return
     */
    public Node search(int value){
        if(root == null){
            return null;
        }
        return root.search(value);
    }

    /**
     * 查询父节点
     * @param value
     * @return
     */
    public Node searchParent(int value){
        if(root == null){
            return null;
        }
        return root.searchParent(value);
    }

    /**
     * 删除并返回节点右子树的最小节点
     * @param node
     * @return
     */
    public Node deleteRightTreeMin(Node node){
        //目标节点
        Node target = node;
        //找到右子树最小的节点
        while(target.left != null){
            target = target.left;
        }
        //删除右子树最小节点
        delete(target.value);
        //返回节点
        return target;
    }

    /**
     * 删除给定节点
     * @param value
     */
    public void delete(int value){
        //根节点为空
        if(root == null){
            return;
        }
        //查询目标节点
        Node target = search(value);
        //目标节点为空
        if (target == null) {
            return;
        }
        //目标节点不为空
        //如果是根节点
        if (root.left == null && root.right == null) {
            root = null;
        }else {
            Node parent = searchParent(value);
            //判断target是不是叶子节点
            if (target.left == null && target.right == null) {
                //待删除的是父节点的左子节点
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                    return;
                }
                //待删除的是父节点的右子节点
                if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                    return;
                }
            }else if(target.left != null && target.right != null){
                //target有左右子节点
                Node newNode = deleteRightTreeMin(target.right);
                target.value = newNode.value;
            }else{
                //target只有一个子节点
                if(target.left != null){
                    //target有左子节点
                    //target是parent的左子节点
                   if(parent != null){
                       if(parent.left == target){
                           parent.left = target.left;
                       }else{
                           //target是parent的右子节点
                           parent.right = target.right;
                       }
                   }else{
                       root = target.left;
                   }
                }else{
                    //target有右子节点
                    //target是parent的左子节点
                   if(parent != null){
                       if(parent.left == target){
                           parent.left = target.right;
                       }else {
                           //target是parent的右子节点
                           parent.right = target.right;
                       }
                   }else{
                       root = target.right;
                   }
                }
            }
        }
    }
}


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

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

    /**
     * 添加节点
     *
     * @param node
     */
    public void add(Node node) {
        //添加的节点数据大
        if (this.value < node.value) {
            if (this.right == null) {
                //右节点为空
                this.right = node;
            } else {
                //右节点不为空
                this.right.add(node);
            }
        }else {
            //添加的节点数据小于等于左节点
            if(this.left == null){
                //左节点为空
                this.left = node;
            }else{
                this.left.add(node);
            }
        }
    }

    /**
     * 中序遍历
     */
    public void fixOrder(){
        if(this.left != null){
            this.left.fixOrder();
        }
        System.out.println(this);
        if(this.right != null){
            this.right.fixOrder();
        }
    }



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


    /**
     * 查找值的节点
     * @param value
     * @return
     */
    public Node search(int value){
        if(this.value == value){
            return this;
        }
        //查找值小于当前节点值
        if(value < this.value){
            if(this.left != null){
                return this.left.search(value);
            }else{
                //没有找到
                System.out.println("没有找到");
                return null;
            }
        }else{
            //查找值大于等于当前节点值
            if(this.right != null){
                return this.right.search(value);
            }else{
                //没有找到
                System.out.println("没有找到");
                return null;
            }
        }
    }

    /**
     * 查找给定值的父节点
     * @param value
     * @return
     */
    public Node searchParent(int value){
        //如果左子节点或者右子节点值等于给定值,返回当前节点
        if((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)){
            return this;
        }
        //给定值小于当前节点值
        if(value < this.value){
            //左子节点不为空
            if(this.left != null){
                return this.left.searchParent(value);
            }else{
                //左子节点为空
                return null;
            }
        }else{
            //给定值大于等于当前节点值
            if(this.right != null){
                return this.right.searchParent(value);
            }else{
                //右子节点为空
                return null;
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值