• Java数据结构之二分搜索树(BST)



    提示:以下是本篇文章正文内容,Java系列学习将会持续更新

    一、基本概念

    在这里插入图片描述
    二叉查找树 (Binary Search Tree)
     ①它属于二叉树的一种。
     ②左子树的所有节点值 < 根节点的值 < 右子树的所有节点值
     ③它的左右子树也是二分搜索树。
     ④一般不考虑值相等的情况,元素不重复

    特点
    对二分搜索树进行中序遍历,得到的集合就是一个升序的集合。

    二、二叉树查询性能分析

    二叉搜索树查找长度与结点在二叉搜索树的深度有关,即结点越深,则比较次数越多。
    在这里插入图片描述
    最优情况下,二叉搜索树为完全二叉树,其平均比较次数为:logN
    最差情况下,二叉搜索树退化为单支树,其平均比较次数为:N

    回到目录…

    三、插入

    ①BST树不能插入重复的元素。
    ②往BST树中插入元素,一定是插入到叶子节点的位置,不会影响原本树的结构。

    每次都是和父节点作比较,小于的话往左子树中插,大于的话往右子树中插,再和子树的父节点比较,以此往下。。。

    	/** 添加元素 **/
        public void add(int val) {
            root = add(root, val);
        }
        
        //向以root为根节点的树添加元素
        private TreeNode add(TreeNode root, int val){
            //边界条件
            if(root == null){
                size ++;
                return new TreeNode(val);
            }
            if(val < root.val){
                //在左树中插入val
                root.left = add(root.left, val);
            }else if(val > root.val){
                //在右树中插入val
                root.right = add(root.right, val);
            }
            return root;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    回到目录…

    四、删除

    • 当待删节点没有左子树时,就类似于删除最小值,直接将右子树拼接到原本的位置。

    • 当待删节点没有右子树时,就类似于删除最大值,直接将左子树拼接到原本的位置。

    • 当待删节点左右子树都存在时,找到当前节点的前驱或后继节点来代替它。所谓前驱 / 后继,并不是它的左孩子 / 右孩子,而是大小关系上最接近它且比它小 / 大的节点

    • 那具体如何找前驱 / 后继呢? 前驱 = 左子树中的最大值; 后继 = 右子树中的最小值。

    	/**
         * 删除任意值
         * Hibbard Deletion
         * 删除一个左右子树都存在的节点,就找到当前节点的前驱或后继来代替它。
         * 前驱、后继指的是大小关系上的,而不是left\right的指向
         * @return
         */
        public void remove(int val) {
            root = remove(root, val);
        }
        private TreeNode remove(TreeNode root, int val) {
            if(root == null){
                throw new NoSuchElementException("BST中没有值为" + val + "的节点");
            }else if(root.val < val){
                root.right = remove(root.right, val);
                return root;
            }else if(root.val > val){
                root.left = remove(root.left, val);
                return root;
            }else { //此时root就是待删节点
                if(root.left == null){
                	// 先存右子树
                    TreeNode right = root.right;
                    // 将待删节点的指向剪断至空
                    root.right = root = null;
                    // 右子树就是结果树,直接返回
    				return right;
                }
                if(root.right == null){
                    TreeNode left = root.left;
                    root.left = root = null;
                    return left;
                }
                //此时,左右子树都不为空
                //先找到右子树中的最小值节点
                TreeNode rightMin = minNode(root.right);
                // 结果树的右子树就是原节点删除最小值后的右子树
                rightMin.right = removeMin(root.right);
                // 结果树的左子树就是原节点的左子树
                rightMin.left = root.left;
                // 断开连接
                root.left = root = null;
                root.right = root = null;
                return rightMin;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    回到目录…

    五、代码实现

    /**
     * 普通的二分搜索树
     */
    public class BST {
    
        private class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
            TreeNode(int val) {
                this.val = val;
            }
        }
    
        private int size;
        private TreeNode root;
    
        /** 添加元素 **/
        public void add(int val) {
            root = add(root, val);
        }
        //向以root为根节点的树添加元素
        private TreeNode add(TreeNode root, int val){
            //边界条件
            if(root == null){
                size ++;
                return new TreeNode(val);
            }
            if(val < root.val){
                //在左树中插入val
                root.left = add(root.left, val);
            }else if(val > root.val){
                //在右树中插入val
                root.right = add(root.right, val);
            }
            return root;
        }
    
        /** 查找元素 **/
        public boolean contains(int val) {
            return contains(root, val);
        }
        //判断根节点为root的二叉树中有无val
        private boolean contains(TreeNode root, int val) {
            //边界条件
            if(root == null){
                return false;
            }
            if(root.val == val){
                return true;
            }else if(root.val > val){
                return contains(root.left, val);
            }else{
                return contains(root.right, val);
            }
        }
    
        /** 返回最小值: 递归 **/
        public int findMin() {
            //判空
            if(size == 0){
                throw new NoSuchElementException("BST is empty! Cannot find!");
            }
            return findMin(root);
        }
        //返回以root为根节点的二叉树的最小值
        private int findMin(TreeNode root) {
            if(root.left == null){
                return root.val;
            }
            return findMin(root.left);
        }
    
        /** 返回最大值: 迭代 **/
        public int findMax() {
            //判空
            if(size == 0){
                throw new NoSuchElementException("BST is empty! Cannot find!");
            }
            TreeNode node = root;
            while (node.right != null){
                node = node.right;
            }
            return node.val;
        }
    
        /** 删除最小值: 递归 **/
        public int removeMin() {
            //判空
            if(size == 0){
                throw new NoSuchElementException("BST is empty! Cannot remove!");
            }
            int min = findMin();
            root = removeMin(root);
            return min;
        }
        //删除以root为根节点的二叉树的最小值
        private TreeNode removeMin(TreeNode root) {
            //当左子树不存在时
            if(root.left == null){
                //当前的root就是待删节点,则先暂存右子树的地址
                TreeNode right = root.right;
                //断开连接,并置为空
                root.right = root = null;
                size --;
                return right;
            }
            //左子树存在时,则递归去删左子树的最小值
            root.left = removeMin(root.left);
            return root;
        }
    
        /** 删除最大值: 递归 **/
        public int removeMax() {
            //日常判空
            if(size == 0){
                throw new NoSuchElementException("BST is empty! Cannot remove!");
            }
            int max = findMax();
            root = removeMax(root);
            return max;
        }
        //删除以root为根节点的二叉树的最大值
        private TreeNode removeMax(TreeNode root) {
            //当不存在右子树时
            if(root.right == null){
                //root就是最大值,待删节点
                TreeNode left = root.left;
                root.left = root = null;
                size --;
                return left;
            }
            //存在右子树时
            root.right = removeMax(root.right);
            return root;
        }
    
        /**
         * 删除任意值
         * Hibbard Deletion
         * 删除一个左右子树都存在的节点,就找到当前节点的前驱或后继来代替它。
         * 前驱、后继指的是大小关系上的,而不是left\right的指向
         * @return
         */
        public void remove(int val) {
            root = remove(root, val);
        }
        private TreeNode remove(TreeNode root, int val) {
            if(root == null){
                throw new NoSuchElementException("BST中没有值为" + val + "的节点");
            }else if(root.val < val){
                root.right = remove(root.right, val);
                return root;
            }else if(root.val > val){
                root.left = remove(root.left, val);
                return root;
            }else { //此时root就是待删节点
                if(root.left == null){
                    return removeMin(root);
                }
                if(root.right == null){
                    return removeMax(root);
                }
                //此时,左右子树都不为空
                //先找到右子树中的最小值节点
                TreeNode rightMin = minNode(root.right);
                //
                rightMin.right = removeMin(root.right);
                rightMin.left = root.left;
                root.left = root = null;
                root.right = root = null;
                return rightMin;
            }
        }
        //查找以root为根节点的树中最小值的节点
        private TreeNode minNode(TreeNode root) {
            if(root == null){
                throw new NoSuchElementException("BST is empty! Not have minNode!");
            }
            TreeNode node = root;
            while(node.left != null){
                node = node.left;
            }
            return node;
        }
    
        /** 打印二叉树, 中序遍历的结果 **/
        @Override
        public String toString() {
            List<Integer> list = new ArrayList<>();
            inOrder(list, root);
            return list.toString();
        }
        // 中序遍历
        private void inOrder(List<Integer> list, TreeNode root) {
            if(root == null){
                return;
            }
            inOrder(list, root.left);
            list.add(root.val);
            inOrder(list, root.right);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203

    回到目录…


    总结:
    提示:这里对文章进行总结:
    以上就是今天的学习内容,本文是Java数据结构的学习,认识了BST树,BST树的插入和删除操作,以及BST树的不平衡性。之后的学习内容将持续更新!!!

  • 相关阅读:
    请求DNS查找的host命令示例
    rust多线程
    木聚糖-聚乙二醇-苯硼酸,PBA-PEG-Xylan,苯硼酸-PEG-木聚糖
    java毕业设计——基于java+JSP+MySQL的健身俱乐部会员管理系统设计与实现——健身俱乐部会员管理系统
    国标GB28181视频平台EasyGBS国标视频云平台级联到EasyCVR,上级平台无法播放通道视频的问题解决方案
    maven 打包 deploy 项目时 出现 401 Unauthorized
    Android SurfaceFlinger导读(04)理解BufferQueue
    深度学习环境搭建
    VMware-克隆虚拟机
    ADS原理图到Layout,Layout更新原理图
  • 原文地址:https://blog.csdn.net/qq15035899256/article/details/126166284