21.排序二叉树BST

二叉排序树

二叉排序树(Binary Sort Tree BST)又称二叉查找树(Binary Search Tree BST),二叉树中的一种特殊形式,查询和添加的效率比较高。

  • 二叉排序树要求任何一个非叶子节点的左节点的值应该小于等于当前节点,右节点的值应该大于等于当前节点。
  • 如果有相同的值,可以挂在左边也可以挂在右边

在这里插入图片描述

节点添加

  • 添加的元素比当前节点小,如果当前节点左节点为空,直接挂上,否则往左递归
  • 添加元素比当前元素大或者等于,如果当前节点右节点为空,直接挂上,否则往右递归
 /**
     * 添加
     * @param node
     */
    public void add(BSTNode node) {
        // 首先判断待添加节点是不是比当前节点小 如果小 挂在当前节点左边
        if (node.data < this.data) {
            // 如果左子节点为空直接挂上
            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 del(int data) {
        // 根节点特殊处理
        if (root == null) {
            return;
        }
        if (root.data == data && root.left == null && root.right == null) {
            root = null;
            return;
        }
        // 1.先找到当前节点和当前节点的父节点
        BSTNode targetNode = this.search(data);
        // 没找到直接返回
        if (targetNode == null) {
            return;
        }
        BSTNode parent = this.searchParent(data);
        // 2. 开始判断
        // 2.1 当前节点是不是叶子节点
        if (targetNode.isLeaf()) {
            // 叶子节点 判断当前节点的是父节点的左节点还是右节点用于判断挂在那边
            if (parent != null) {
                if (parent.left != null && parent.left.data == data) {
                    parent.left = null;
                }
                if (parent.right != null && parent.right.data == data) {
                    parent.right = null;
                }
            } else {
                //
            }


        } else {
            // 非叶子节点的情况处理 非叶子节点情况处理又分成了只有一个子树还是有2个子树的情况
            if (targetNode.left != null && targetNode.right != null) {
                // 两颗子树的情况
                // 找到待删除节点的右子树的最小值 或者左子树的最大值 用来替换掉待删除节点的值形成新的顺序二叉树
                int minData = this.findMin(targetNode.right);
                // 删除该节点 该节点一定是一个叶子节点
                this.del(minData);
                // 将待删除节点替换成临时变量即可
                targetNode.data = minData;
            } else {
                // 一颗子树的情况
                if (targetNode.left != null) {
                    // 待删除节点有左子树
                    if (parent != null) {
                        if (parent.left.data == targetNode.data) {
                            // 待删除节点是父节点的左子树
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        // parent为空说明删除的是带子树的跟节点
                        root = targetNode.left;
                    }
                } else {
                    // 待删除节点有右子树
                    if (parent != null) {
                        // 待删除节点是parent的左子树
                        if (parent.left.data == targetNode.data) {
                            parent.left = targetNode.right;
                        } else {
                            // 待删除节点是父节点的右子树
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    /**
     * 返回以node为根节点的最小值
     *
     * @param node
     * @return
     */
    public int findMin(BSTNode node) {
        BSTNode temp = node;
        while (node.left != null) {
            temp = temp.left;
        }
        return temp.data;
    }

顺序二叉树完整代码

public class BinarySortTreeDemo {

    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BSTTree bstTree = new BSTTree();
        for (int i : arr) {
            bstTree.add(new BSTNode(i));
        }
        bstTree.inOrderTraversal();
//        System.out.println(bstTree.search(2));
        System.out.println("=================================");
        bstTree.del(12);


        bstTree.del(5);
        bstTree.del(10);
        bstTree.del(2);
        bstTree.del(3);
        bstTree.del(9);
        bstTree.del(1);
        bstTree.del(7);

        bstTree.inOrderTraversal();
    }
}

/**
 * 二叉排序树
 */
class BSTTree {

    public BSTNode root;

    public void del(int data) {
        // 根节点特殊处理
        if (root == null) {
            return;
        }
        if (root.data == data && root.left == null && root.right == null) {
            root = null;
            return;
        }
        // 1.先找到当前节点和当前节点的父节点
        BSTNode targetNode = this.search(data);
        // 没找到直接返回
        if (targetNode == null) {
            return;
        }
        BSTNode parent = this.searchParent(data);
        // 2. 开始判断
        // 2.1 当前节点是不是叶子节点
        if (targetNode.isLeaf()) {
            // 叶子节点 判断当前节点的是父节点的左节点还是右节点用于判断挂在那边
            if (parent != null) {
                if (parent.left != null && parent.left.data == data) {
                    parent.left = null;
                }
                if (parent.right != null && parent.right.data == data) {
                    parent.right = null;
                }
            } else {
                //
            }


        } else {
            // 非叶子节点的情况处理 非叶子节点情况处理又分成了只有一个子树还是有2个子树的情况
            if (targetNode.left != null && targetNode.right != null) {
                // 两颗子树的情况
                // 找到待删除节点的右子树的最小值 或者左子树的最大值 用来替换掉待删除节点的值形成新的顺序二叉树
//                int tempData = this.findMin(targetNode.right);
                int tempData = this.findMax(targetNode.left);
                // 删除该节点 该节点一定是一个叶子节点
                this.del(tempData);
                // 将待删除节点替换成临时变量即可
                targetNode.data = tempData;
            } else {
                // 一颗子树的情况
                if (targetNode.left != null) {
                    // 待删除节点有左子树
                    if (parent != null) {
                        if (parent.left.data == targetNode.data) {
                            // 待删除节点是父节点的左子树
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        // parent为空说明删除的是带子树的跟节点
                        root = targetNode.left;
                    }
                } else {
                    // 待删除节点有右子树
                    if (parent != null) {
                        // 待删除节点是parent的左子树
                        if (parent.left.data == targetNode.data) {
                            parent.left = targetNode.right;
                        } else {
                            // 待删除节点是父节点的右子树
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    /**
     * 返回以node为根节点的最小值
     *
     * @param node
     * @return
     */
    public int findMin(BSTNode node) {
        BSTNode temp = node;
        while (node.left != null) {
            temp = temp.left;
        }
        return temp.data;
    }

    /**
     * 返回以node为根节点的最大值
     *
     * @param node
     * @return
     */
    public int findMax(BSTNode node) {
        BSTNode temp = node;
        while (temp.right != null) {
            temp = temp.right;
        }
        return temp.data;
    }

    /**
     * 添加
     *
     * @param node
     */
    public void add(BSTNode node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public BSTNode searchParent(int data) {
        if (root == null) {
            return null;
        }
        return root.searchParent(data);
    }

    public BSTNode search(int data) {
        if (root == null) {
            return null;
        }
        return root.search(data);
    }

    public void inOrderTraversal() {
        if (root == null) {
            throw new RuntimeException("root is null");
        }
        root.inOrderTraversal();
    }
}


/**
 * 二叉排序树节点
 */
class BSTNode {

    public int data;

    public BSTNode left;

    public BSTNode right;

    public BSTNode(int data) {
        this.data = data;
    }

    /**
     * 判断当前节点是不是一个叶子节点
     *
     * @return
     */
    public boolean isLeaf() {
        return this.left == null && this.right == null;
    }

    /**
     * 找到某一个节点的父节点
     *
     * @param data
     * @return
     */
    public BSTNode searchParent(int data) {
        if ((this.left != null && this.left.data == data) ||
                (this.right != null && this.right.data == data)) {
            return this;
        }

        //当前节点比目标节点大 向左递归
        if (this.data > data && this.left != null) {
            return this.left.searchParent(data);
        }

        // 当前节点比目标节点小 向右递归
        if (this.data <= data && this.right != null) {
            return this.right.searchParent(data);
        }
        return null;
    }

    /**
     * 当到当前节点
     *
     * @param data
     * @return
     */
    public BSTNode search(int data) {
        if (this.data == data) {
            return this;
        }
        if (this.left != null && this.data > data) {
            return this.left.search(data);
        }
        if (this.right != null && this.data < data) {
            return this.right.search(data);
        }
        return null;
    }

    /**
     * 中序遍历 遍历出来其实是按从小到大顺序
     */
    public void inOrderTraversal() {
        if (this.left != null) {
            this.left.inOrderTraversal();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.inOrderTraversal();
        }
    }

    /**
     * 添加
     *
     * @param node
     */
    public void add(BSTNode node) {
        // 首先判断待添加节点是不是比当前节点小 如果小 挂在当前节点左边
        if (node.data < this.data) {
            // 如果左子节点为空直接挂上
            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);
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值