def test_is_valid_bst(self): trues = [[], [5], [5, 1, 7], [5, 1, 7, -1, 2, 6], [5, 1, 7, 1, 2, 6], [5, 1, 7, -1, 2, None, 8]] falses = [[1, 2, 3], [5, 1, 7, -1, 1, 6], [5, 1, 7, 0, 6], [5, 3, 8, 2, 6, 7, 9]] for arr in trues: tree = make_binary_tree(arr) self.assertTrue(is_valid_bst(tree)) for arr in falses: tree = make_binary_tree(arr) self.assertFalse(is_valid_bst(tree))
def test_is_bst(self): bsts = [[8, 4, 10, 2, 6, None, 20], [6, 6, 10, 2, None, 9, 20], [6, 4, 7, 2, 5, None, 20]] not_bsts = [[8, 4, 10, 2, 6, 20], [8, 4, 10, 2, 12, None, 20], [8, 4, 8, 2, 6, None, 20], [1, 2, 3, 4, 5]] for arr in bsts: with self.subTest(arr=arr): tree = make_binary_tree(arr) self.assertTrue(is_bst(tree)) for arr in not_bsts: with self.subTest(arr=arr): tree = make_binary_tree(arr) self.assertFalse(is_bst(tree)) tree = make_bst(arr) self.assertTrue(is_bst(tree))
def test_find_second_largest(self): tests = [[[], None], [[5], None], [[5, 2], 2], [[5, 1, 7], 5], [[5, 1, 7, -1, 2, 6], 6], [[5, 1, 7, 1, 2, 6], 6], [[5, 1, 7, -1, 2, None, 8], 7]] for arr, expected in tests: tree = make_binary_tree(arr) self.assertEqual(find_second_largest(tree), expected)
def test_check_is_subtree(self): tree1 = make_binary_tree([i for i in range(1, 8)]) tree2 = make_binary_tree([2, 4, 5]) tree3 = make_binary_tree([i for i in range(1, 7)]) trues = [[tree1, tree1], [tree1, tree1.left], [tree1.left, tree1.left.left], [tree1, tree1.right.left], [tree1, tree2], [tree1, None], [None, None]] falses = [[tree1.left, tree1], [tree1.left.left, tree1.left], [tree1.left, tree1.right], [tree1.right, tree1.left], [tree2, tree1], [tree1, tree3], [None, tree1]] for t1, t2 in trues: with self.subTest(t1=t1.val if t1 is not None else None, t2=t2.val if t2 is not None else None): self.assertTrue(is_subtree(t1, t2)) for t1, t2 in falses: with self.subTest(t1=t1.val if t1 is not None else None, t2=t2.val if t2 is not None else None): self.assertFalse(is_subtree(t1, t2))
def test_first_common_ancestor(self): tree = make_binary_tree([1, 2, 3, 4, 5, 6, None, 7, 8]) othertree = make_binary_tree([-1]) tests = [ # u, v, ancestor [tree, tree, tree], [tree.left, tree, tree], [tree.right, tree, tree], [tree.left.left.left, tree.left.left.right, tree.left.left], [tree.right.left, tree.right, tree.right], [tree.left.left.right, tree.left.right, tree.left], [tree.left.right, tree.right, tree], [tree.left.left, None, None], [tree.left.left, othertree, None] ] for u, v, ancestor in tests: with self.subTest(u=u.val if u else None, v=v.val if v else None): self.assertIs(first_common_ancestor(tree, u, v), ancestor) self.assertIs(first_common_ancestor(tree, v, u), ancestor)
def test_is_balanced(self): trues = [ [], [1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, None, 4], [1, 2, 3, 4, None, 5, None], [1, 2, 3, 4, None, 5, None, 6], ] falses = [ [1, 2, 3, None, 4, None, None, None, None, 5], ] for arr in trues: tree = make_binary_tree(arr) self.assertTrue(is_balanced(tree)) for arr in falses: tree = make_binary_tree(arr) self.assertFalse(is_balanced(tree))
def test_is_balanced(self): balanced = [ [i for i in range(1, 7)], [i for i in range(1, 8)], [i for i in range(1, 9)], [i for i in range(1, 10)], [1, 2, 3, 4, 5, 6, None, 7] ] unbalanced = [ [1, 2, 3, 4, 5, None, None, 7], [1, 2, None, 4], [1, None, 3, None, None, None, 7] ] for arr in balanced: with self.subTest(arr=arr): tree = make_binary_tree(arr) self.assertTrue(is_balanced(tree)) for arr in unbalanced: with self.subTest(arr=arr): tree = make_binary_tree(arr) self.assertFalse(is_balanced(tree))
def test_list_of_depths(self): arrs = [[i for i in range(1, 7)], [i for i in range(1, 8)], [i for i in range(1, 9)], [i for i in range(1, 10)], [1, 2, 3, 4, 5, 6, None, 7]] for arr in arrs: tree = make_binary_tree(arr) depths = list_of_depths(tree) for depth in depths: for node in depth: print(node, end=" ") print() print()
def test_successor(self): tree = make_binary_tree([1, 2, 3, 4, 5, 6, None, 7]) tests = [ # node, successor [tree.left, tree.left.right], [tree.left.left, tree.left], [tree.left.left.left, tree.left.left], [tree.left.right, tree], [tree, tree.right.left], [tree.right.left, tree.right], [tree.right, None], [None, None] ] for node, successor in tests: with self.subTest(node=node.val if node else None): self.assertIs(in_order_successor(node), successor)
def test_bst_sequences(self): arrs = [[i for i in range(1, j + 1)] for j in range(1, 6)] for arr in arrs: tree = make_binary_tree(arr) print(bst_sequences(tree))
def test_paths_with_sum(self): tree = make_binary_tree([10, 5, -3, 3, 2, None, 11, 3, -2, None, 1]) self.assertEqual(paths_with_sum(tree, 8), 3)
def test_get_random_node(self): tree = make_binary_tree([4, 2, 6, 1, 3, 5, 7]) for i in range(30): print(get_random_node(tree), end=" ") print()