def check_sequences(tree, sequences): """ Creates a tree for each sequence in sequence and checks if it is equal to the original tree that generated all the sequences """ for sequence in sequences: root, *rest = sequence sequence_tree = TreeNode(root) for val in rest: sequence_tree.insert(val) if not is_equal(tree, sequence_tree): print(sequence) sequence_tree.display() return False return True
return float('-inf') right_height = check_height(root.right) if right_height == float('-inf'): # Pass error up return float('-inf') height_diff = abs(left_height - right_height) if height_diff > 1: return float('-inf') return max(left_height, right_height) + 1 # -- Testing from binary_search_tree import TreeNode from random import randint if __name__ == "__main__": root = TreeNode(50) for _ in range(10): root.insert(randint(25, 75)) root.display() print(check_balanced(root)) # True or False print('-' * 50) minimal = minimal_tree([1, 2, 3, 4, 5, 6, 7, 8]) minimal.display() print(check_balanced(minimal)) # True
sequence_tree = TreeNode(root) for val in rest: sequence_tree.insert(val) if not is_equal(tree, sequence_tree): print(sequence) sequence_tree.display() return False return True if __name__ == "__main__": tree = TreeNode(randint(0, 100)) for _ in range(6): tree.insert(randint(0, randint(0, 100))) print('--- Original Tree ---') tree.display() print('\n--- Weaved ---') sequences = all_sequences(tree) print('\n--- BST Sequences ---') for sequence in sequences: print(sequence) print('\n--- Checking Equality Between Sequences ---') if check_sequences(tree, sequences): print('All OK') else:
if not root: return 0 return (paths_with_sum(root.left, target) + paths_with_sum(root.right, target) + pws_helper(root, 0, target)) def pws_helper(node, curr, target): if not node: return 0 curr += node.val return (1 if curr == target else 0 + pws_helper(node.left, curr, target) + pws_helper(node.right, curr, target)) # -- Testing from binary_search_tree import TreeNode from random import randint, seed if __name__ == "__main__": tree = TreeNode(5) for _ in range(25): tree.insert(randint(0, 20)) tree.display() target = 38 total_paths = paths_with_sum(tree, target) print(f'\nPaths with sum {target}: {total_paths} path(s)')
if not T1 and not T2: return True # One empty if not T1 or not T2: return False # Non-equal values if T1.val != T2.val: return False # Otherwise, check that subtrees are equivalent return is_equal(T1.left, T2.left) and is_equal(T1.right, T2.right) # -- Testing from random import randint, seed if __name__ == "__main__": seed(1209) T1 = TreeNode(50) for _ in range(50): T1.insert(randint(0, 100)) T1.display() T2 = T1.search(42) T2.display() print(check_subtree(T1, T2))
def validate_bst_util(node, low, high): """ Helper function for validating a binary search tree """ if not node: return True if not low < node.val < high: return False return validate_bst_util(node.left, low, node.val) and \ validate_bst_util(node.right, node.val, high) # -- Testing from binary_search_tree import TreeNode from random import randint if __name__ == "__main__": invalid_tree = TreeNode(4, TreeNode(5), TreeNode(6)) invalid_tree.display() print(validate_bst(invalid_tree)) print('-' * 25) valid_tree = TreeNode(10) for _ in range(20): valid_tree.insert(randint(0, 20)) valid_tree.display() print(validate_bst(valid_tree))