二叉排序树

规则:

  1. 左子节点值 < 父节点值
  2. 右子节点值>=父节点值
  3. 如果中序遍历二叉排序树,则会升序排列

节点的添加和遍历

public class BinarySortTree {
    private Node root;

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

    public void infView(){
        if (this.root == null){
            System.out.println("empty");
        }else{
            this.root.infView(this.root);
        }
    }

    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9};
        BinarySortTree tree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            tree.add(new Node(arr[i]));
        }
        tree.infView();
        tree.add(null);
    }
}

class Node {
    Node left;
    Node right;
    int val;

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

    // 添加节点
    // 思路: 如果被添加node小于当前节点,则添加到当前节点的左边,如果当前节点左边不为空,则递归探索该节点的左边节点
    // 如果被添加的node大于等于当前节点,则添加到当前节点的右边,如果当前节点右边不为空,则递归探索该节点的右边节点
    public void add(Node node){
        if (node == null){
            return;
        }
        // this.val 相当于根节点的值
        if (node.val < this.val){
            if (this.left!=null){
                this.left.add(node);
            }else {
                this.left = node;
            }
        }else {
            if (this.right!=null){
                this.right.add(node);
            }else {
                this.right = node;
            }
        }
    }

    // 前序遍历
    public void infView(Node root){
        if (root == null) {
            return;
        }
        infView(root.left);
        System.out.println(root.val);
        infView(root.right);
    }
}

节点的删除

思路:
首先分成三种情况:

  1. 删除叶子节点
  2. 删除只有一颗子树的节点
  3. 删除有两颗子树的节点

对于1:
1). 找到要删除的节点target
2). 找到目标节点的父节点
3).判断target是父节点的左节点还是右节点
4).如果是左节点: parent.left = null; 如果是右节点: parent.right = null

对于2:
1). 2). 3)同上
4). 判断target的子节点是左节点还是右节点
5). 如果target有左节点
5.1). target为parent的左子节点, parent.left = target.left
5.2). target为parent的右子节点, parent.right = target.left
6). 如果target有右节点
6.1). target为parent的左子节点, parent.left = target.right
6.2). target为parent的右子节点, parent.right = target.right

对于3:
1). 2)同上
3). 从target的右子树中找到最小节点,记为temp(或者左子树中的最大节点)
4). 删除最小节点, target.val = temp.val

Node类中

    public void delete(int val){
        // 找到目标节点及其父节点
        Node target = this.search(val);
        Node parent = this.searchParent(val);
        if (target == null){
            System.out.println("不存在要删除的节点");
        }else {
            // 判断删除节点的类型
            // 有父节点
            if (parent != null) {
                if (target.left == null && target.right == null) {
                    // 情况1. 要删除的节点为叶子节点
                    if (target.isRight(parent)) {
                        // 要删除的节点为父节点的右节点
                        parent.right = null;
                    } else{
                        parent.left = null;
                    }
                }else if ((target.left!=null&&target.right==null) || (target.left==null&&target.right!=null)){
                    // 情况2. 要删除的节点只含有一个子树
                    if (target.left!=null){
                        // 判断target是含有左子树
                        if (target.isRight(parent)){
                            parent.right = target.left;
                        }else {
                            parent.left = target.left;
                        }
                    }else {
                        // target含有右子树
                        if (target.isRight(parent)){
                            parent.right = target.right;
                        }else {
                            parent.left = target.right;
                        }
                    }
                }else {
                    // 第三种情况, 含有两颗子树
                    // find target右子树中的最小值,并删除
                    int temp = target.right.minVal();
                    Node tempParent = this.searchParent(temp);
                    if (tempParent.right.val == temp){
                        tempParent.right = null;
                        target.val = temp;
                    }else {
                        tempParent.left = null;
                        target.val = temp;
                    }
                }
            }else {
                // 该节点为root;
                // 1.该节点没有子节点
                if (target.left == null && target.right == null){
                    target = null;
                }else if (target.left!=null){
                    // 含有左子树
                    int temp = target.left.maxVal();
                    Node tempParent = this.searchParent(temp);
                    if (tempParent.left.val == temp){
                        tempParent.left = null;
                        target.val = temp;
                    }else {
                        tempParent.right = null;
                        target.val = temp;
                    }
                }else {
                    //含有右子树
                    int temp = target.right.minVal();
                    Node tempParent = this.searchParent(temp);
                    if (tempParent.right.val == temp){
                        tempParent.right = null;
                        target.val = temp;
                    }else {
                        tempParent.left = null;
                        target.val = temp;
                    }
                }
            }
        }
    }

    // 辅助函数: 找到要删除的节点
    public Node search(int val){
        // 递归的出口
        if (this.val == val){
            return this;
        }
        if (this.val > val){
            // 应该继续向左递归搜索
            if (this.left!=null){
                return this.left.search(val);
            }else {
                // 如果左节点不存在,则val不在树中
                return null;
            }
        }else {
            // 应该先右递归搜索
            if (this.right!=null){
                return this.right.search(val);
            }else {
                // 如果右节点不存在,则val不在树中
                return null;
            }
        }
    }

    // 辅助方法2: 找到target的父节点
    public Node searchParent(int val){
        // 递归出口
        if ((this.left!=null&&this.left.val==val)||(this.right!=null&&this.right.val == val)){
            return this;
        }
        if (this.val > val){
            // 应该向左搜索
            if (this.left!=null){
                return this.left.searchParent(val);
            }else {
                return null;
            }
        }else {
            if (this.right!=null){
                return this.right.searchParent(val);
            }else {
                return null;
            }
        }
    }

    // 辅助方法3: 判断target是parent的左子树
    public boolean isLeft(Node parent){
        if (parent.left == this){
            return true;
        }else {return false;}
    }

    // 辅助方法4: 判断target是parent的右子树
    public boolean isRight(Node parent){
        if (parent.right == this){
            return true;
        }else {return false;}
    }

    // 辅助方法5: 找到target右子树中的最小值
    public int minVal(){
        // 递归出口: 左子节点为空
        if (this.left==null){
            return this.val;
        }
        return this.left.minVal();
    }

    // 辅助方法6: 找到target左子树中的最大值
    public int maxVal(){
        // 递归出口: 右子节点为空
        if (this.right==null){
            return this.val;
        }
        return this.right.minVal();
    }

选择二叉树中

    // 删除节点
    public void delete(int val) {
        if (this.root == null) {
            System.out.println("empty");
        } else {
            this.root.delete(val);
        }
    }

上述写的太复杂,有待优化

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值