def main(): # 3 # 2 6 # 1 4 6 root = BinaryTreeNode(3) root.left = BinaryTreeNode(2) root.left.left = BinaryTreeNode(1) root.right = BinaryTreeNode(6) root.right.left = BinaryTreeNode(4) root.right.right = BinaryTreeNode(6) assert not find_first_equal_k(root, 7) assert (find_first_equal_k(root, 6).data == 6 and find_first_equal_k(root, 6).right.data == 6) # 3 # 3 5 # 2 5 6 root = BinaryTreeNode(3) root.left = BinaryTreeNode(3) root.left.left = BinaryTreeNode(2) root.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(5) root.right.right = BinaryTreeNode(6) assert not find_first_equal_k(root, 7) assert find_first_equal_k(root, 3) is root.left assert find_first_equal_k(root, 5) is root.right.left assert find_first_equal_k(root, 6).data == 6
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) assert has_path_sum(tree, 3) tree.left = BinaryTreeNode(2) assert has_path_sum(tree, 5) tree.left.left = BinaryTreeNode(1) assert has_path_sum(tree, 6) tree.right = BinaryTreeNode(5) assert has_path_sum(tree, 8) assert not has_path_sum(tree, 7) tree.right.left = BinaryTreeNode(4) assert has_path_sum(tree, 12) assert not has_path_sum(tree, 1) assert not has_path_sum(tree, 3) assert not has_path_sum(tree, 5) tree.right.right = BinaryTreeNode(6) assert has_path_sum(tree, 6) assert not has_path_sum(tree, 7) assert has_path_sum(tree, 14) assert not has_path_sum(tree, -1) assert not has_path_sum(tree, 2**64 - 1) assert not has_path_sum(tree, -2**64)
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) # should output 3 x = lca(tree, tree.left, tree.right) assert x.data == 3 print(x.data) # should output 5 x = lca(tree, tree.right.left, tree.right.right) assert x.data == 5 print(x.data) # should output 5 x = lca(tree, tree.right, tree.right.right) assert x.data == 5 print(x.data) # should output 3 x = lca(tree, tree, tree.left.left) assert x.data == 3 print(x.data) # should output 2 x = lca(tree, tree.left, tree.left.left) assert x.data == 2 print(x.data)
def small_test(): root = BinaryTreeNode(5) assert not pair_includes_ancestor_and_descendant_of_m(root, root, root) root.left = BinaryTreeNode(2) root.left.right = BinaryTreeNode(4) assert not pair_includes_ancestor_and_descendant_of_m( root, root.left, root.left.right) assert pair_includes_ancestor_and_descendant_of_m(root, root.left.right, root.left) # Example of the first figure of BST chapter. root = BinaryTreeNode(19) root.left = BinaryTreeNode(7) root.left.left = BinaryTreeNode(3) root.left.left.left = BinaryTreeNode(2) root.left.left.right = BinaryTreeNode(5) root.left.right = BinaryTreeNode(11) root.left.right.right = BinaryTreeNode(17) root.left.right.right.left = BinaryTreeNode(13) root.right = BinaryTreeNode(43) root.right.left = BinaryTreeNode(23) root.right.left.right = BinaryTreeNode(37) root.right.left.right.left = BinaryTreeNode(29) root.right.left.right.left.right = BinaryTreeNode(31) root.right.left.right.right = BinaryTreeNode(41) root.right.right = BinaryTreeNode(47) root.right.right.right = BinaryTreeNode(53) assert not pair_includes_ancestor_and_descendant_of_m( root.right, root.left, root.right.left) assert pair_includes_ancestor_and_descendant_of_m( root, root.right.left.right.left.right, root.right.left)
def small_test(): root = BinaryTreeNode(5) assert not pair_includes_ancestor_and_descendant_of_m(root, root, root) root.left = BinaryTreeNode(2) root.left.right = BinaryTreeNode(4) assert not pair_includes_ancestor_and_descendant_of_m(root, root.left, root.left.right) assert pair_includes_ancestor_and_descendant_of_m(root, root.left.right, root.left) # Example of the first figure of BST chapter. root = BinaryTreeNode(19) root.left = BinaryTreeNode(7) root.left.left = BinaryTreeNode(3) root.left.left.left = BinaryTreeNode(2) root.left.left.right = BinaryTreeNode(5) root.left.right = BinaryTreeNode(11) root.left.right.right = BinaryTreeNode(17) root.left.right.right.left = BinaryTreeNode(13) root.right = BinaryTreeNode(43) root.right.left = BinaryTreeNode(23) root.right.left.right = BinaryTreeNode(37) root.right.left.right.left = BinaryTreeNode(29) root.right.left.right.left.right = BinaryTreeNode(31) root.right.left.right.right = BinaryTreeNode(41) root.right.right = BinaryTreeNode(47) root.right.right.right = BinaryTreeNode(53) assert not pair_includes_ancestor_and_descendant_of_m(root.right, root.left, root.right.left) assert pair_includes_ancestor_and_descendant_of_m( root, root.right.left.right.left.right, root.right.left)
def simple_test(): symm_tree = BinaryTreeNode() assert is_symmetric(symm_tree) symm_tree.left = BinaryTreeNode() assert not is_symmetric(symm_tree) symm_tree.right = BinaryTreeNode() assert is_symmetric(symm_tree) symm_tree.right.right = BinaryTreeNode() assert not is_symmetric(symm_tree)
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) tree_traversal(tree)
def main(): # 3 # 2 5 # 1 4 6 L = BinaryTreeNode(3) L.left = BinaryTreeNode(2) L.left.left = BinaryTreeNode(1) L.right = BinaryTreeNode(5) L.right.left = BinaryTreeNode(4) L.right.right = BinaryTreeNode(6) # 7 # 2 8 # 0 R = BinaryTreeNode(7) R.left = BinaryTreeNode(2) R.left.left = BinaryTreeNode(0) R.right = BinaryTreeNode(8) root = merge_two_bsts(L, R) # should output 0 1 2 2 3 4 5 6 7 8 print_bst_inorder(root, float('-inf'))
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) assert 3 == find_LCA(tree, tree.left.left, tree.right.left).data assert 5 == find_LCA(tree, tree.right.left, tree.right.right).data assert 2 == find_LCA(tree, tree.left.left, tree.left).data
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) res = postorder_traversal(tree) golden_res = generate_postorder(tree) assert list(res) == golden_res
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) res = preorder_traversal(tree) golden_res = generate_preorder(tree) assert res == golden_res
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) # should output 1 2 3 4 5 6 inorder_traversal(tree) golden_res = [1, 2, 3, 4, 5, 6] assert result == golden_res
def main(): # 3 # 2 5 # 1 4 7 root = BinaryTreeNode(3) root.left = BinaryTreeNode(2) root.left.left = BinaryTreeNode(1) root.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(4) root.right.right = BinaryTreeNode(7) assert find_first_greater_than_k(root, 1) is root.left assert find_first_greater_than_k(root, 5) is root.right.right assert find_first_greater_than_k(root, 6) is root.right.right assert not find_first_greater_than_k(root, 7)
def main(): # 3 # 2 4 # 1 5 6 almost_bst = BinaryTreeNode(3) almost_bst.left = BinaryTreeNode(2) almost_bst.left.left = BinaryTreeNode(1) almost_bst.right = BinaryTreeNode(4) almost_bst.right.left = BinaryTreeNode(5) almost_bst.right.right = BinaryTreeNode(6) reconstruct_bst(almost_bst) result = generate_inorder(almost_bst) print(*result) assert all(result[i] <= result[i + 1] for i in range(len(result) - 1))
def main(): # 3 # 2 5 # 1 4 6 root = BinaryTreeNode(3) root.left = BinaryTreeNode(2) root.left.left = BinaryTreeNode(1) root.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(4) root.right.right = BinaryTreeNode(6) k = 0 ans = find_k_unbalanced_node(root, k) assert ans.data == 2 if ans: print(ans.data)
def main(): # A min-first BST # 1 # 2 4 # 3 5 7 tree = BinaryTreeNode(1) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(3) tree.right = BinaryTreeNode(4) tree.right.left = BinaryTreeNode(5) tree.right.right = BinaryTreeNode(7) assert search_min_first_bst(tree, 1) assert search_min_first_bst(tree, 3) assert search_min_first_bst(tree, 5) assert not search_min_first_bst(tree, 6)
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) if len(sys.argv) == 2: k = int(sys.argv[1]) else: k = random.randint(1, 6) print('k =', k) ans = find_k_largest_in_bst(tree, k) print(*ans, sep='\n') for i in range(1, len(ans)): assert ans[i - 1] >= ans[i] ans = find_k_largest_in_bst(tree, 2) assert ans[0] == 6 assert ans[1] == 5 # 3 # 3 4 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(3) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(4) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) ans = find_k_largest_in_bst(tree, 3) assert ans[0] == 6 assert ans[1] == 4 assert ans[2] == 4
def main(): simple_test() # Non symmetric tree test. # 3 # 2 5 # 1 4 6 non_symm_tree = BinaryTreeNode() non_symm_tree.left = BinaryTreeNode() non_symm_tree.left.left = BinaryTreeNode() non_symm_tree.right = BinaryTreeNode() non_symm_tree.right.left = BinaryTreeNode() non_symm_tree.right.right = BinaryTreeNode() assert not is_symmetric(non_symm_tree) print(is_symmetric(non_symm_tree)) # Symmetric tree test. symm_tree = BinaryTreeNode() symm_tree.left = BinaryTreeNode() symm_tree.right = BinaryTreeNode() assert is_symmetric(symm_tree) print(is_symmetric(symm_tree)) # Empty tree test. symm_tree = None assert is_symmetric(symm_tree) print(is_symmetric(symm_tree))
def main(): # 1 # 1 0 # 0 1 0 root = BinaryTreeNode(1) assert sum_root_to_leaf(root) == 1 root.left = BinaryTreeNode(1) assert sum_root_to_leaf(root) == 3 root.left.left = BinaryTreeNode(0) assert sum_root_to_leaf(root) == 6 root.right = BinaryTreeNode(0) assert sum_root_to_leaf(root) == 8 root.right.left = BinaryTreeNode(1) assert sum_root_to_leaf(root) == 11 root.right.right = BinaryTreeNode(0) assert sum_root_to_leaf(root) == 15
def main(): # 3 # 2 5 # 1 4 6 root = BinaryTreeNode(3) root.left = BinaryTreeNode(2) root.left.left = BinaryTreeNode(1) root.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(4) root.right.right = BinaryTreeNode(6) L = bst_to_doubly_linked_list(root) curr = L pre = float('-inf') while curr: assert pre <= curr.data print(curr.data) pre = curr.data curr = curr.right
def main(): # balanced binary tree test # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode() tree.left = BinaryTreeNode() tree.left.left = BinaryTreeNode() tree.right = BinaryTreeNode() tree.right.left = BinaryTreeNode() tree.right.right = BinaryTreeNode() assert is_balanced_binary_tree(tree) == True print(is_balanced_binary_tree(tree)) # Non-balanced binary tree test. tree = BinaryTreeNode() tree.left = BinaryTreeNode() tree.left.left = BinaryTreeNode() assert is_balanced_binary_tree(tree) == False print(is_balanced_binary_tree(tree))
def main(): # balanced binary tree test # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode() tree.left = BinaryTreeNode() tree.left.left = BinaryTreeNode() tree.right = BinaryTreeNode() tree.right.left = BinaryTreeNode() tree.right.right = BinaryTreeNode() assert is_balanced_binary_tree(tree) print(is_balanced_binary_tree(tree)) # Non-balanced binary tree test. tree = BinaryTreeNode() tree.left = BinaryTreeNode() tree.left.left = BinaryTreeNode() assert not is_balanced_binary_tree(tree) print(is_balanced_binary_tree(tree))
def main(): # 3 # 2 5 # 1 4 6 # 10 # 13 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.left.left.left = BinaryTreeNode(10) tree.left.left.left.right = BinaryTreeNode(13) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) # should output 3 # 2 5 # 1 4 6 # 10 # 13 assert binary_tree_depth_order(tree) == [[3], [2, 5], [1, 4, 6], [10], [13]]
def main(): simple_test() # 3 # 2 5 # 1 0 4 6 # -1 -2 tree = BinaryTreeNode(3) assert create_output_list(exterior_binary_tree(tree)) == [3] tree.left = BinaryTreeNode(2) assert create_output_list(exterior_binary_tree(tree)) == [3, 2] tree.left.right = BinaryTreeNode(0) tree.left.right.left = BinaryTreeNode(-1) tree.left.right.right = BinaryTreeNode(-2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) assert create_output_list( exterior_binary_tree(tree)) == [3, 2, 1, -1, -2, 4, 6, 5]
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) L = create_list_of_leaves(tree) assert len(L) == 1 and L[0].data == 2 tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) L = create_list_of_leaves(tree) output = [] # should output 1, 4, 6 for l in L: output.append(l.data) print(l.data) golden_res = [1, 4, 6] assert output == golden_res
def test(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) # should output True. assert is_binary_tree_bst(tree) assert is_binary_tree_bst_alternative(tree) # 10 # 2 5 # 1 4 6 tree.data = 10 # should output False. assert not is_binary_tree_bst(tree) assert not is_binary_tree_bst_alternative(tree) # should output True. assert is_binary_tree_bst(None) assert is_binary_tree_bst_alternative(None)
def main(): small_test() # 3 # 2 5 # 1 4 6 root = BinaryTreeNode(3) root.left = BinaryTreeNode(2) root.left.left = BinaryTreeNode(1) root.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(4) root.right.right = BinaryTreeNode(6) assert pair_includes_ancestor_and_descendant_of_m(root, root.right.right, root.right) assert pair_includes_ancestor_and_descendant_of_m(root.right.right, root, root.right) assert not pair_includes_ancestor_and_descendant_of_m( root, root.right, root.right.right) assert not pair_includes_ancestor_and_descendant_of_m( root.right, root, root.right.right) assert (not pair_includes_ancestor_and_descendant_of_m( root.right.left, root.right.right, root.right)) assert (not pair_includes_ancestor_and_descendant_of_m( root.right.left, root.left.left, root.right))
def main(): # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6) # should output True. assert is_binary_tree_BST(tree) == True print(is_binary_tree_BST(tree)) # 10 # 2 5 # 1 4 6 tree.data = 10 # should output False. assert not is_binary_tree_BST(tree) print(is_binary_tree_BST(tree)) # should output True. assert is_binary_tree_BST(None) == True print(is_binary_tree_BST(None))
def main(): small_test() # 3 # 2 5 # 1 4 6 root = BinaryTreeNode(3) root.left = BinaryTreeNode(2) root.left.left = BinaryTreeNode(1) root.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(4) root.right.right = BinaryTreeNode(6) assert pair_includes_ancestor_and_descendant_of_m(root, root.right.right, root.right) assert pair_includes_ancestor_and_descendant_of_m(root.right.right, root, root.right) assert not pair_includes_ancestor_and_descendant_of_m(root, root.right, root.right.right) assert not pair_includes_ancestor_and_descendant_of_m(root.right, root, root.right.right) assert (not pair_includes_ancestor_and_descendant_of_m( root.right.left, root.right.right, root.right)) assert (not pair_includes_ancestor_and_descendant_of_m( root.right.left, root.left.left, root.right))
return sorted_head.right def print_bst_inorder(n, pre): if n: print_bst_inorder(n.left, pre) assert pre <= n.data print(n.data, end=' ') print_bst_inorder(n.right, n.data) # 3 # 2 5 # 1 4 6 L = BinaryTreeNode(3) L.left = BinaryTreeNode(2) L.left.left = BinaryTreeNode(1) L.right = BinaryTreeNode(5) L.right.left = BinaryTreeNode(4) L.right.right = BinaryTreeNode(6) # 7 # 2 8 # 0 R = BinaryTreeNode(7) R.left = BinaryTreeNode(2) R.left.left = BinaryTreeNode(0) R.right = BinaryTreeNode(8) root = merge_two_bsts(L, R) # should output 0 1 2 2 3 4 5 6 7 8 print_bst_inorder(root, float('-inf'))
from binary_tree_prototype import BinaryTreeNode import random import sys # @include def find_k_largest_in_bst(tree, k): def find_k_largest_in_bst_helper(tree): # Perform reverse inorder traversal. if tree and len(k_largest_elements) < k: find_k_largest_in_bst_helper(tree.right) if len(k_largest_elements) < k: k_largest_elements.append(tree.data) find_k_largest_in_bst_helper(tree.left) k_largest_elements = [] find_k_largest_in_bst_helper(tree) return k_largest_elements # 3 # 2 5 # 1 4 6 tree = BinaryTreeNode(3) tree.left = BinaryTreeNode(2) tree.left.left = BinaryTreeNode(1) tree.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(4) tree.right.right = BinaryTreeNode(6)
right = check_balance(root.right) if not right.balanced: return BalancedStatusWithHeight(False, 0) height = max(left.height, right.height) + 1 is_balance = abs(left.height - right.height) <= 1 # print('data => %s height => %d is_balance => %d' % (root.data, height, is_balance)) return BalancedStatusWithHeight(is_balance, height) return check_balance(tree).balanced # balanced binary tree test # 3 # 2 5 # 1 4 6 #0 tree = BinaryTreeNode('3') tree.left = BinaryTreeNode('2') tree.left.left = BinaryTreeNode('1') tree.right = BinaryTreeNode('5') tree.right.left = BinaryTreeNode('4') tree.right.right = BinaryTreeNode('6') assert is_balanced_binary_tree(tree) print(is_balanced_binary_tree(tree)) # Non-balanced binary tree test. tree = BinaryTreeNode('0') tree.left = BinaryTreeNode('1') tree.left.left = BinaryTreeNode('2') assert not is_balanced_binary_tree(tree) print(is_balanced_binary_tree(tree))
from binary_tree_prototype import BinaryTreeNode from reconstruct_preorder import reconstruct_preorder def consntruct_right_sibling(tree): def populate_children_next_field(start_node): while start_node and start_node.left: start_node.left.next = start_node.right start_node.right.next = start_node.next and start_node.next.left start_node = start_node.next while tree: populate_children_next_field(tree) tree = tree.left tree = BinaryTreeNode(0) left_temp = BinaryTreeNode(2, left=BinaryTreeNode(1), right=BinaryTreeNode(3)) right_temp = BinaryTreeNode(6, left=BinaryTreeNode(5), right=BinaryTreeNode(7)) tree.left = BinaryTreeNode(4, left_temp, right_temp) left_temp = BinaryTreeNode(10, left=BinaryTreeNode(9), right=BinaryTreeNode(11)) right_temp = BinaryTreeNode(13, left=BinaryTreeNode(12), right=BinaryTreeNode(14)) tree.right = BinaryTreeNode(15, left_temp, right_temp) tree_travesal(tree)
# up. next = tree.right or tree.parent else: # Done with both children, so move up. next = tree.parent prev, tree = tree, next return result # @exclude # 3 # 2 5 # 1 4 6 root = BinaryTreeNode(3) root.parent = None assert inorder_traversal(root) == [3] root.left = BinaryTreeNode(2) root.left.parent = root root.left.left = BinaryTreeNode(1) root.left.left.parent = root.left assert inorder_traversal(root) == [1, 2, 3] root.right = BinaryTreeNode(5) root.right.parent = root root.right.left = BinaryTreeNode(4) root.right.left.parent = root.right root.right.right = BinaryTreeNode(6) root.right.right.parent = root.right assert inorder_traversal(root) == [1, 2, 3, 4, 5, 6]