二分搜索树 10 删除最大 & 最小元素

寻找二分搜索树的最小元素

  • 二分搜索树的最小元素一定在最左路的尽头,整个最左路其实就是一个链表,问题其实就是:取到最左路链表的最后一个元素
  • 将问题转化为一个递归问题:寻找以 node 为头节点的最左路链表的最后一个元素;
  • 规模更小的同一个问题是:寻找以 node.left 为头节点的最左路链表的最后一个元素(将整条链表看成由 nodenode.left 组成);
  • 不能再缩小的基本问题是:node.left == null
// 寻找二分搜索树的最小元素
public E minimum(){
    if(size == 0)
        throw new IllegalArgumentException("BST is empty");

    Node minNode = minimum(root);
    return minNode.e;
}

// 返回以node为根的二分搜索树的最小值所在的节点
private Node minimum(Node node){
    if(node.left == null)
        return node;

    return minimum(node.left);
}

删除 BST 中的最小元素

  • 将问题转换为一个递归的问题:删除以 node 为根的 BST 的最小元素,并返回新 BST 的根;
  • 规模更小的同一个问题是:删除以 node.left 为根的 BST 的最小元素,并返回新 BST 的根;
  • 不能再缩小的基本问题是:node.left == null
    • 此时 node 就为该 BST 的最小元素,该被删掉;
    • 取出 node.right 并返回;
    • 断开 node 和右孩子的关系,即 node.right == null
    • 减少整个 BST 的节点个数,即 size --
// 从二分搜索树中删除最小值所在节点, 返回最小值
public E removeMin(){
    E ret = minimum();
    root = removeMin(root);
    return ret;
}

// 删除掉以node为根的二分搜索树中的最小节点
// 返回删除节点后新的二分搜索树的根
private Node removeMin(Node node){

    if(node.left == null){
        Node rightNode = node.right;
        node.right = null;
        size --;
        return rightNode;
    }

    node.left = removeMin(node.left);
    return node;
}

测试代码

public class Main {

    public static void main(String[] args) {
        BST bst = new BST<>();

        Random random = new Random();
        int n = 1000;

        // test removeMin
        for(int i = 0 ; i < n ; i ++)
            bst.add(random.nextInt(10000));

        ArrayList nums = new ArrayList<>();
        while(!bst.isEmpty())
            nums.add(bst.removeMin());

        System.out.println(nums);
        for(int i = 1 ; i < nums.size() ; i ++)
            if(nums.get(i - 1) > nums.get(i))
                throw new IllegalArgumentException("Error!");
        System.out.println("removeMin test completed.");
    }

}
输出

[1, 19, 20, 35, 36, 38, 48, 66, 71, 84, 87, 92, 98, 114, 124, 128, 132, 134, 143, 145, 156, 170, 177, 186, 204, 205, 207, 209, 222, 224, 231, 237, 238, 247, 250, 258, 266, 278, 290, 291, 308, 316, 321, 328, 331, 363, 371, 380, 383, 390, 426, 467, 473, 483, 486, 495, 500, 502, 510, 518, 522, 534, 548, 555, 558, 581, 588, 592, 602, 639, 652, 656, 660, 662, 667, 694, 724, 730, 735, 741, 754, 778, 798, 809, 815, 817, 819, 824, 840, 866, 878, 885, 886, 900, 901, 902, 916, 918, 924, 935, 941, 945, 954, 962, 964, 989, 999, 1001, 1009, 1012, 1027, 1029, 1033, 1039, 1054, 1059, 1070, 1075, 1082, 1087, 1093, 1104, 1139, 1141, 1155, 1168, 1180, 1181, 1188, 1213, 1223, 1225, 1227, 1233, 1240, 1248, 1272, 1289, 1314, 1326, 1332, 1334, 1349, 1382, 1386, 1387, 1388, 1405, 1416, 1418, 1420, 1434, 1435, 1441, 1446, 1447, 1471, 1499, 1506, 1524, 1525, 1527, 1543, 1549, 1570, 1578, 1582, 1587, 1603, 1608, 1638, 1643, 1652, 1665, 1678, 1691, 1707, 1713, 1725, 1775, 1793, 1814, 1834, 1839, 1901, 1912, 1926, 1934, 1956, 1964, 1981, 1990, 1992, 1996, 2011, 2012, 2018, 2033, 2035, 2042, 2048, 2067, 2101, 2117, 2122, 2130, 2136, 2142, 2150, 2151, 2153, 2164, 2165, 2195, 2199, 2202, 2204, 2228, 2244, 2245, 2258, 2260, 2267, 2280, 2288, 2298, 2324, 2351, 2363, 2368, 2374, 2380, 2396, 2403, 2414, 2431, 2439, 2446, 2459, 2470, 2485, 2511, 2535, 2551, 2619, 2630, 2647, 2663, 2674, 2702, 2706, 2720, 2728, 2740, 2741, 2760, 2805, 2814, 2815, 2819, 2832, 2844, 2847, 2865, 2874, 2887, 2905, 2915, 2933, 2939, 2942, 2944, 2951, 2961, 2962, 2963, 2979, 2980, 2993, 3006, 3036, 3053, 3058, 3067, 3077, 3096, 3128, 3150, 3161, 3172, 3174, 3175, 3185, 3186, 3187, 3248, 3265, 3278, 3296, 3301, 3306, 3311, 3315, 3321, 3326, 3342, 3349, 3361, 3365, 3369, 3383, 3401, 3413, 3423, 3424, 3426, 3429, 3450, 3466, 3501, 3512, 3514, 3548, 3554, 3564, 3582, 3583, 3584, 3585, 3588, 3605, 3607, 3635, 3639, 3664, 3666, 3674, 3685, 3686, 3688, 3702, 3709, 3718, 3727, 3740, 3749, 3755, 3759, 3774, 3781, 3784, 3795, 3800, 3805, 3809, 3819, 3829, 3857, 3865, 3886, 3903, 3957, 3964, 3977, 3995, 3999, 4010, 4022, 4064, 4069, 4075, 4083, 4093, 4098, 4112, 4117, 4138, 4160, 4163, 4165, 4166, 4171, 4172, 4192, 4196, 4212, 4225, 4238, 4242, 4244, 4275, 4324, 4326, 4334, 4343, 4369, 4381, 4389, 4398, 4404, 4409, 4424, 4427, 4457, 4473, 4483, 4498, 4514, 4542, 4543, 4579, 4589, 4607, 4631, 4645, 4672, 4675, 4680, 4688, 4710, 4717, 4719, 4721, 4733, 4748, 4750, 4758, 4766, 4779, 4788, 4790, 4800, 4801, 4816, 4845, 4858, 4876, 4881, 4895, 4898, 4903, 4905, 4908, 4910, 4913, 4922, 4947, 4949, 4955, 4973, 4983, 5000, 5009, 5016, 5023, 5025, 5030, 5044, 5048, 5049, 5057, 5073, 5078, 5079, 5080, 5084, 5093, 5102, 5142, 5149, 5154, 5158, 5173, 5187, 5194, 5198, 5205, 5206, 5214, 5243, 5258, 5259, 5270, 5274, 5342, 5351, 5353, 5354, 5361, 5366, 5373, 5378, 5388, 5404, 5417, 5439, 5441, 5455, 5458, 5466, 5485, 5492, 5517, 5520, 5524, 5537, 5543, 5550, 5557, 5560, 5568, 5579, 5580, 5585, 5591, 5600, 5601, 5613, 5614, 5626, 5640, 5651, 5652, 5657, 5664, 5694, 5704, 5712, 5718, 5719, 5744, 5748, 5769, 5780, 5784, 5785, 5796, 5799, 5806, 5811, 5814, 5824, 5864, 5871, 5885, 5900, 5910, 5945, 5957, 5985, 6008, 6009, 6012, 6014, 6017, 6039, 6061, 6067, 6095, 6109, 6140, 6150, 6158, 6160, 6165, 6168, 6175, 6196, 6211, 6240, 6243, 6249, 6257, 6259, 6283, 6288, 6292, 6312, 6354, 6357, 6366, 6373, 6382, 6391, 6399, 6408, 6414, 6416, 6417, 6426, 6428, 6432, 6436, 6438, 6439, 6497, 6505, 6509, 6528, 6547, 6549, 6551, 6563, 6593, 6596, 6597, 6599, 6610, 6613, 6620, 6627, 6629, 6657, 6668, 6687, 6691, 6702, 6707, 6727, 6742, 6747, 6754, 6768, 6784, 6787, 6791, 6793, 6796, 6798, 6803, 6822, 6833, 6835, 6837, 6852, 6861, 6884, 6910, 6924, 6925, 6926, 6941, 6970, 6983, 7002, 7003, 7012, 7014, 7026, 7035, 7043, 7058, 7074, 7076, 7078, 7090, 7106, 7109, 7116, 7117, 7126, 7128, 7145, 7151, 7157, 7159, 7164, 7167, 7171, 7185, 7200, 7208, 7245, 7253, 7255, 7258, 7259, 7265, 7279, 7282, 7286, 7297, 7302, 7308, 7312, 7315, 7323, 7324, 7328, 7338, 7342, 7345, 7350, 7356, 7371, 7382, 7383, 7391, 7426, 7433, 7452, 7454, 7467, 7469, 7474, 7529, 7531, 7560, 7563, 7570, 7576, 7581, 7589, 7596, 7601, 7629, 7638, 7639, 7651, 7654, 7657, 7658, 7659, 7660, 7669, 7735, 7758, 7759, 7766, 7778, 7780, 7782, 7792, 7794, 7802, 7811, 7835, 7862, 7864, 7871, 7874, 7883, 7886, 7889, 7903, 7906, 7907, 7918, 7933, 7939, 7956, 7959, 7971, 7977, 7979, 8000, 8002, 8014, 8020, 8022, 8028, 8029, 8032, 8034, 8038, 8053, 8066, 8082, 8085, 8086, 8093, 8094, 8117, 8122, 8126, 8127, 8137, 8152, 8172, 8179, 8218, 8219, 8223, 8238, 8241, 8242, 8251, 8257, 8271, 8272, 8293, 8297, 8319, 8325, 8328, 8357, 8385, 8395, 8428, 8431, 8465, 8483, 8491, 8503, 8505, 8507, 8513, 8516, 8521, 8527, 8532, 8534, 8559, 8572, 8592, 8603, 8627, 8632, 8633, 8644, 8650, 8668, 8672, 8684, 8712, 8716, 8725, 8740, 8755, 8771, 8773, 8792, 8802, 8803, 8810, 8816, 8818, 8823, 8831, 8851, 8863, 8871, 8885, 8917, 8919, 8930, 8934, 8935, 8940, 8947, 8956, 8958, 8972, 8974, 8979, 8987, 8991, 8994, 8995, 8998, 9006, 9009, 9033, 9037, 9062, 9064, 9067, 9095, 9097, 9102, 9127, 9128, 9140, 9153, 9189, 9193, 9206, 9230, 9235, 9236, 9239, 9241, 9244, 9262, 9273, 9305, 9316, 9330, 9354, 9362, 9368, 9386, 9388, 9393, 9396, 9397, 9417, 9422, 9438, 9440, 9444, 9445, 9455, 9456, 9465, 9485, 9512, 9522, 9523, 9542, 9548, 9562, 9564, 9591, 9594, 9617, 9623, 9634, 9644, 9650, 9651, 9653, 9655, 9701, 9705, 9706, 9710, 9715, 9723, 9727, 9732, 9733, 9734, 9742, 9745, 9758, 9776, 9788, 9801, 9812, 9817, 9836, 9838, 9841, 9851, 9869, 9870, 9873, 9882, 9951, 9955, 9960, 9964]
removeMin test completed.

删除BST中的最大元素

// 寻找二分搜索树的最大元素
public E maximum(){
    if(size == 0)
        throw new IllegalArgumentException("BST is empty");

    return maximum(root).e;
}

// 返回以node为根的二分搜索树的最大值所在的节点
private Node maximum(Node node){
    if( node.right == null )
        return node;

    return maximum(node.right);
}

// 从二分搜索树中删除最大值所在节点
public E removeMax(){
    E ret = maximum();
    root = removeMax(root);
    return ret;
}

// 删除掉以node为根的二分搜索树中的最大节点
// 返回删除节点后新的二分搜索树的根
private Node removeMax(Node node){
    if(node.right == null){
        Node leftNode = node.left;
        node.left = null;
        size --;
        return leftNode;
    }

    node.right = removeMax(node.right);
    return node;
}

你可能感兴趣的:(二分搜索树 10 删除最大 & 最小元素)