return self.root test_cases = [ ([1], (2, 1), [1, 2]), ([1, 2, 3, 4, 5, 6], (7, 3), (8, 4), [1, 2, 3, 4, 5, 6, 7, 8]), ([ 43, 7, 46, 23, 31, 5, 50, 27, 2, 8, 26, 13, 43, 18, 37, 8, 26, 18, 14, 42, 40, 23, 49, 42, 8, 10, 6, 26, 9, 37, 9, 9, 28, 44, 3, 17, 10, 38, 2, 47 ], (15, 42), (2, 40), (6, 40), (9, 23), (49, 23), (23, 49), (7, 49), (4, 42), (4, 42), (14, 8), (28, 8), (2, 10), (35, 10), (28, 6), (48, 6), (47, 26), (23, 26), (1, 9), (15, 9), (20, 37), (14, 37), (37, 9), (4, 9), (5, 9), (7, 9), (41, 28), (26, 28), (11, 44), (29, 44), (38, 3), (29, 3), (9, 17), (33, 17), (13, 10), (44, 10), (23, 38), (16, 38), (48, 2), (0, 2), (34, 47), [ 43, 7, 46, 23, 31, 5, 50, 27, 2, 8, 26, 13, 43, 18, 37, 8, 26, 18, 14, 42, 40, 23, 49, 42, 8, 10, 6, 26, 9, 37, 9, 9, 28, 44, 3, 17, 10, 38, 2, 47, 15, 2, 6, 9, 49, 23, 7, 4, 4, 14, 28, 2, 35, 28, 48, 47, 23, 1, 15, 20, 14, 37, 4, 5, 7, 41, 26, 11, 29, 38, 29, 9, 33, 13, 44, 23, 16, 48, 0, 34 ]), ] for test_input in test_cases: test_object = CBTInserter( ConstructTree.build_tree_leetcode(test_input[0]).root) for test_add_val, test_parent_val in test_input[1:-1]: assert test_object.insert(test_add_val) == test_parent_val get_tree = BinaryTree(test_object.get_root()).leetcode_traversal() assert CompareTree.compare_leetcode_traversal(get_tree, test_input[-1])
None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, 2]), ] for test_tree_1, test_tree_2, expected_tree in test_cases: get_tree = BinaryTree(merge_trees((ConstructTree.build_tree_leetcode(test_tree_1).root if test_tree_1 else None), (ConstructTree.build_tree_leetcode(test_tree_2).root if test_tree_2 else None))) get_tree_traversal = get_tree.leetcode_traversal() assert CompareTree.compare_leetcode_traversal(get_tree_traversal, expected_tree)
2441, 2445, 2449, 2453, 2457, 2461, 2465, 2469, 2473, 2477, 2481, 2485, 2489, 2493, 2497, 2501, 2505, 2509, 2513, 2517, None, 2523, 2527, 2531, 2535, 2539, 2543, 2547, 2551, 2555, 2559, 2563, 2567, 2571, 2575, 2579, None, 2585, 2589, 2593, 2597, 2601, 2605, 2609, 2613, 2617, 2621, 2625, 2629, 2633, 2637, 2641, None, 2647, 2651, 2655, 2659, 2663, 2667, 2671, 2675, 2679, 2683, 2687, 2691, 2695, 2699, 2703, 2707, 2711, 2715, 2719, 2723, 2727, 2731, 2735, 2739, 2743, 2747, 2751, 2755, 2759, 2763, 2767, None, 2773, 2777, 2781, 2785, 2789, 2793, 2797, 2801, 2805, 2809, 2813, 2817, 2821, 2825, 2829, None, 2835, 2839, 2843, 2847, 2851, 2855, 2859, 2863, 2867, 2871, 2875, 2879, 2883, 2887, 2891 ], 2791, 2851, 87451), ] for test_tree_list, test_low, test_high, expected_output in test_cases: assert BST(ConstructTree.build_tree_leetcode( test_tree_list).root).range_accumulate(test_low, test_high) == expected_output # Test Binary Tree Trim Boundary test_cases = [ ([1, 0, 2], 1, 2, [1, None, 2]), ([3, 0, 4, None, 2, None, None, 1], 1, 3, [3, 2, None, 1]), ([1], 1, 2, [1]), ([1, None, 2], 1, 3, [1, None, 2]), ([1, None, 2], 2, 4, [2]), ] for tree_input, low_input, high_input, expected_output in test_cases: test_tree = BST(ConstructTree.build_tree_leetcode(tree_input).root) test_tree.trim_boundary(low_input, high_input) assert CompareTree.compare_leetcode_traversal( test_tree.leetcode_traversal(), expected_output)
None, 7, 6, None, None, 1, 3, None, 4, 9, 8, None, None, 0, None, 3, 4, None, 8, None, 1, None, None, 2, 2, None, None, 4, None, None, None, 3, None, None, 2, None, None, None, 4, None, 5, None, None, None, None, 2, None, 5, None, None, None, None, None, None, 2, 7, 5, None, 6, None, None, None, None, 2, None, 0, None, 3, None, 1, None, 9, 4, None, 3, None, None, None, None, None, None, None, 5, 5, 7, None, None, 1, None, 4, 6, None, None, None, 2, None, 5, 9, 0, 6, 2, None, None, None, None, None, None, None, None, None, None, None, None, 5, None, 7, None, 2, 9, None, None, 1, None, None, None, 1, 6, None, 6, None, None, 0, 8, None, 4, None, None, None, None, 4, None, None, 0, None, 6, 0, None, None, None, 4, None, None, None, None, None, 0, None, None, None, None, None, None, None, None, None, None, None, None, 0, 5, 4, 2, 6, 4, 5, 3, 4, None, None, 5, None, None, None, None, 4, None, None, 3, 6, 2, 0, None, 6, 6, None, None, None, None, 0, 6, None, None, None, 3, 9, 4, None, None, None, None, None, 0, None, None, 6, 7, 0, None, 9, 2, None, 3, 3, None, None, 8, None, 3, None, None, None, 8, 5, 3, None, 2, 4, None, 9, 6, 9, None, None, None, None, 6, None, 6, None, 5, 3, None, None, None, None, 4, None, None, None, 9, 0, 9, 7, 1, 1, None, 1, None, 1, 6, None, 5, None, 6, None, None, 1, None, None, None, None, None, None, 5, None, None, None, None, None, 3, None, 6, 1, None, 0, 2, None, None, 0, None, None, 0, None, None, None, None, None, 3, None, None, 8, None, None, 5, 3, 3, None, None, None, None, None, None, None, 3, None, None, 0, 8, 7, None, None, 8, 1, None, None, None, None, None, None, 7, None, None, None, None, None, None, None, None, None, None, None, 5, 2, None, 2, 6, None, None, None, None, None, None, None, 1, 5, 0, None, None, 2, None, 7, None, None, 6, None, None, None, None, None, None, None, None, None, None, None, None, None, 8, None, None, None, None, 3, None, None, 4, None, None, 2, None, None, None, None, 0, 3, None, None, None, None, None, 7, None, 8, None, None, None, None, 8, 5, None, 3, 4, None, None, None, 8, None, None, None, None, None, None, None, None, None, 3, 7, None, None, None, 4, 0, 3, None, None, 6, None, None, None, None, None, None, None, None, None, None, None, None, 8, None, None, None, None, None, 2, None, None, None, None, None, None, None, None, None, 0, None, None, None, 2, None, None, None, 8, 2, None, None, None, None, None, None, None, 8, None, None, None, None, None, None, None, None, None, None, 2, None, None, None, 2, 5, None, None, None, None, None, None, None, None, None, None, None, 2, None, None, None, None, None, 8, None, None, None, None, None, None, None, None, None, None, 0, 5, None, 1, 1]), ] for test_tree_list, test_value, test_depth, expected_output in test_cases: get_tree = BinaryTree(add_one_row((ConstructTree.build_tree_leetcode(test_tree_list).root if test_tree_list else None), test_value, test_depth)).leetcode_traversal() assert CompareTree.compare_leetcode_traversal(get_tree, expected_output)
272, 258, None, 266, 182, -43, 66, 48, None, None, -122, None, 205, 68, None, 275, None, -165, None, None, None, None, -6, 160, None, None, None, 80, None, 29, 278, None, None, None, None, None, 185, None, None, None, None, None, None, None, -70, -89, None, None, None, None, None, 60, None, None, None, None, None, 99, 101, 16, None, -44, None, -108, -67, None, None, -15, None, None, None, None, None, 53, None, None, None, 75, -190, 86, 4, 73, 19, None, 63, 50, 245, 220, None, -184, None, None, 113, None, None, None, 206, 42, None, None, None, -26, -87, None, None, -86, None, -105, 222, 199, -145, 268, None, None, None, None, None, None, None, 162, 184, None, 155, None, None, None, None, None, None, None, 114, None, None, None, None, None, None, None, None, 92, None, None, 9, 271, None, None, None, 218, None, None, None, None, -25, -47, 190, None, None, None, -148, None, None, None, None, None, None, None, None, 27, None, None, None, 179, -166, None, None, -75, None, None, 242, None, None, None, None, None, None, -144, 230, 111, None, -27, None, 225, 149, None, 262, None, None, 117, 200, 234, None, None, -124, None, None, None, None, None, 213, None, None, None, 281, None, 188, -72, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 58, 297, None, None, None, None, None, None, None, None, None, None, None, None, 285, None, None, 207, None, -39, 292, -171, -52, None, None, None, None, None, None, -71, -114, -176 ]), ] for test_pre_order, test_in_order, expected_tree_list in test_cases: assert CompareTree.compare_leetcode_traversal( ConstructTree.build_tree_pre_in(test_pre_order, test_in_order).leetcode_traversal(), expected_tree_list)
None, 363, None, 440, None, None, 325, 247, None, None, None, None, 289, None, None, 432, None, None, None, None, None, None, None, 484, None, None, 321, 269, None, None, None, None, None, 373, 485, 403, 344, 243, 254, 202, None, 473, 347, 455, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 308, None, None, None, None, None, None, None, None, None, 142, None, None, 369, None, None, None, None, 436, None, 376, 420, None, None, None, None, None, None, 115, 263, None, None, None, 399, None, None, 371, 234, 351, None, 474, None, 441, None, None, None, 483, 418, None, None, 358, 427, None, None, None, None, 349, None, 365, None, 389, None, None, None, None, None, 384, 438, None, 410, None, None, 378, 350, None, 479, None, None, None, 464, None, 332, 405, None, None, None, None, None, None, None, None, 447, 136, 164, 280, None, 401, None, 456, None, None, 422, None, None, None, None, None, None, None, None, 486, None, None, None, None, 493, None, None, None, None, None, None, 458, None, None, None, None, None, None, None, None, 490, None, None, None, 494, None, None, None, None, None, None, 150, 157, 206, 396, None, 337, None, None, None, 495, None, 481, None, None, None, None, None, None, None, None, None, None, 159, 210, 307, 377, 259, None, None, 421, None, None, None, None, None, None, 268, None, 431, 223, None, 342, None, 491, None, None, None, None, None, None, None, 460, 275, 224, None, None, None, None, None, None, None, None, 322, 239, 463 ], [463]), ] for test_tree_list, expected_list in test_cases: got_tree = BinaryTree( sub_tree_with_all_deepest_nodes( ConstructTree.build_tree_leetcode(test_tree_list).root)) assert CompareTree.compare_leetcode_traversal( got_tree.leetcode_traversal(), expected_list)