def _deserialize_binary_tree(serialized_tree: List[Any]) -> BinaryTree: current_value = serialized_tree.pop(0) if current_value: root = BinaryTree(current_value) root.left = _deserialize_binary_tree(serialized_tree) root.right = _deserialize_binary_tree(serialized_tree) return root
def test_two_nodes(): tree = BinaryTree() tree.root = Node("apples") tree.root.right = Node("bananas") expected = ["apples", "bananas"] actual = breadth_first(tree) assert actual == expected
def test_init(): tree = BinaryTree() assert not tree.root root = BinaryTreeNode(1) tree = BinaryTree(root) assert tree.root.data == 1
def merge_binary_trees(b1: BinaryTree, b2: BinaryTree) -> BinaryTree: if b1 and b2: root = BinaryTree(b1.value + b2.value) root.left = merge_binary_trees(b1.left, b2.left) root.right = merge_binary_trees(b1.right, b2.right) return root else: return b1 or b2
def solution(t1, t2): l1 = BinaryTree.get_list_representation(t1.root) l2 = BinaryTree.get_list_representation(t2.root) sum_list = [ fail_safe_add(x, y) for x, y in itertools.zip_longest(l1, l2, fillvalue=None) ] return BinaryTree.build_tree_from_list(sum_list)
def test_traversing(): my_tree = BinaryTree() for i in range(5): my_tree.insert_key(i) expected_res = 0 tree = my_tree.traverse_tree() for n in tree: assert expected_res == n expected_res += 1
def test_four_nodes(): tree = BinaryTree() tree.root = Node("apples") tree.root.left = Node("bananas") tree.root.right = Node("cucumbers") tree.root.right.right = Node("dates") expected = ["apples", "bananas", "cucumbers", "dates"] actual = breadth_first(tree) assert actual == expected
def test_max_val(): tree = BinaryTree() tree.root = Node(10) tree.root.left = Node(30) tree.root.right = Node(-7) actual = tree.find_maximum_value() expected = 30 assert actual == expected
def test_tree_intersection(): tree_a = BinaryTree() values = [150, 100, 250, 75, 160, 200, 350, 125, 175, 300, 500] add_values_to_empty_tree(tree_a, values) tree_b = BinaryTree() values = [42, 100, 100, 15, 160, 200, 350, 125, 175, 4, 500] add_values_to_empty_tree(tree_b, values) actual = tree_intersection(tree_a, tree_b) expected = [125, 175, 100, 160, 500, 200, 350] assert sorted(actual) == sorted(expected)
def test_find(self): # Arrange tree = BinaryTree() for i in range(10): tree.insert(i) # Act # Assert for i in range(10): self.assertEqual(i, tree.get(i))
def tree(): """ a b c d e f g """ tree = BinaryTree() tree.root = Node("a") tree.root.left = Node("b") tree.root.right = Node("c") tree.root.left.left = Node("d") tree.root.left.right = Node("e") tree.root.right.left = Node("f") tree.root.right.right = Node("g") return tree
def test_example_from_reading(): """ We build these out by hand because the example has some gaps i.e. it is not added to left-to-right 2 7 5 2 6 9 5 11 4 result = [2,7,5,2,6,9,5,11,4] """ tree = BinaryTree() level_0 = Node(2) level_1_first = Node(7) level_1_second = Node(5) level_2_first = Node(2) level_2_second = Node(6) level_2_third = Node(9) level_3_first = Node(5) level_3_second = Node(11) level_3_third = Node(4) tree.root = level_0 level_0.left = level_1_first level_0.right = level_1_second level_1_first.left = level_2_first level_1_first.right = level_2_second level_1_second.right = level_2_third level_2_second.left = level_3_first level_2_second.right = level_3_second level_2_third.right = level_3_third expected = [2, 7, 5, 2, 6, 9, 5, 11, 4] actual = breadth_first(tree) assert actual == expected
def prepare_tree1(self): b = BinaryTree() b.insert(8) b.insert(10) b.insert(3) b.insert(1) b.insert(6) b.insert(4) b.insert(7) b.insert(14) b.insert(13) return b
def testBinaryTreeRemove(self): tree = BinaryTree() tree.insert(10) tree.insert(8) tree.insert(15) tree.insert(6) tree.insert(9) tree.insert(13) tree.insert(25) tree.insert(12) tree.insert(14) tree.insert(20) tree.insert(39) tree.remove(60) tree.remove(0) tree.remove(6) tree.remove(8) self.assertEqual(None, tree.find(6)) self.assertEqual(None, tree.find(8)) self.assertEqual([10, 9, 15, 13, 12, 14, 25, 20, 39], tree.traversePreorder())
def testBinaryTree(self): tree = BinaryTree() self.assertTrue(tree.isEmpty()) tree.insert(3) self.assertFalse(tree.isEmpty()) self.assertEqual(3, tree.find(3)) tree.insert(5) tree.insert(9) tree.insert(2) tree.insert(20) self.assertEqual(5, tree.find(5)) self.assertEqual(2, tree.findMin()) self.assertEqual(20, tree.findMax()) self.assertEqual(2, tree.find(2)) self.assertEqual([3, 2, 5, 9, 20], tree.traversePreorder()) self.assertEqual([2, 3, 5, 9, 20], tree.traverseInorder()) self.assertEqual([2, 20, 9, 5, 3], tree.traversePostorder()) tree.clear() self.assertTrue(tree.isEmpty())
def test_rootless_tree(): tree = BinaryTree() expected = [] actual = breadth_first(tree) assert actual == expected
def test_find_min(): my_tree = BinaryTree() for i in range(5): my_tree.insert_key(i) min_item = my_tree.find_min() assert 0 == min_item
def test_root(): tree = BinaryTree() root = BinaryTreeNode(1) tree.root = root assert tree.root == root
If only one input tree has a node in a given position, the corresponding node in the new tree should match that input node. """ from data_structures.binary_tree import BinaryTree def merge_binary_trees(b1: BinaryTree, b2: BinaryTree) -> BinaryTree: if b1 and b2: root = BinaryTree(b1.value + b2.value) root.left = merge_binary_trees(b1.left, b2.left) root.right = merge_binary_trees(b1.right, b2.right) return root else: return b1 or b2 if __name__ == '__main__': tree1 = BinaryTree(3) tree1.left = BinaryTree(2) tree1.right = BinaryTree(1) tree1.display() tree2 = BinaryTree(5) tree2.left = BinaryTree(3) tree2.right = BinaryTree(2) tree2.right.right = BinaryTree(1) tree2.display() merged_tree = merge_binary_trees(tree1, tree2) merged_tree.display()
# Same Tree # Given two binary trees, write a function to check if they are the same or not. from data_structures.binary_tree import BinaryTree def solution(p, q): if p is None and q is None: return True elif p is None and q is not None: return False elif p is not None and q is None: return False else: if p.value == q.value: return solution(p.left, q.left) and solution(p.right, q.right) else: return False if __name__ == '__main__': list1 = [1, 2, 3] list2 = [1, 2, 3] p = BinaryTree.build_tree_from_list(list1) q = BinaryTree.build_tree_from_list(list2) print("Are trees the same: ", solution(p.root, q.root))
def test_single_node(): tree = BinaryTree() tree.root = Node("apples") expected = ["apples"] actual = breadth_first(tree) assert actual == expected
def test_add_root(self): root = BinaryTreeNode('root node') binary_tree = BinaryTree(root) self.assertEqual(binary_tree.get_root().value, 'root node')
def fail_safe_add(x, y): if x and y: return x + y elif x: return x elif y: return y else: return None def recursive_solution(t1, t2): if not t1: return t2 if not t2: return t1 t1.value = fail_safe_add(t1.value, t2.value) t1.left = recursive_solution(t1.left, t2.left) t1.right = recursive_solution(t1.right, t2.right) return t1 if __name__ == '__main__': t1 = BinaryTree.build_tree_from_list([1, 3, 2, 5]) t2 = BinaryTree.build_tree_from_list([2, 1, 3, None, 4, 7]) # merged_tree = solution(t1, t2) # BinaryTree.level_order_traversal(merged_tree.root) BinaryTree.level_order_traversal(recursive_solution(t1.root, t2.root))
def test_find_max(): my_tree = BinaryTree() for i in range(5): my_tree.insert_key(i) max_item = my_tree.find_max() assert 4 == max_item
# Invert a Binary Tree # "Google: 90% of our engineers use the software you wrote (Homebrew), # but you can’t invert a binary tree on a whiteboard so f*** off." from data_structures.binary_tree import BinaryTree def solution(root): if not root: return root tmp_node = root.left root.left = root.right root.right = tmp_node solution(root.left) solution(root.right) return root if __name__ == '__main__': tree = BinaryTree.build_tree_from_list([4, 2, 7, 1, None, 6, 9]) BinaryTree.level_order_traversal(tree.root) inverted_tree = solution(tree.root) BinaryTree.level_order_traversal(inverted_tree)
def test_insert_element_into_binary_tree(): my_tree = BinaryTree() for i in range(5): my_tree.insert_key(i) assert my_tree.search_tree(1).key == 1 assert my_tree.search_tree(11) is None
def deserialize_binary_tree(serialized_tree: str) -> BinaryTree: return _deserialize_binary_tree(ast.literal_eval(serialized_tree)) def _deserialize_binary_tree(serialized_tree: List[Any]) -> BinaryTree: current_value = serialized_tree.pop(0) if current_value: root = BinaryTree(current_value) root.left = _deserialize_binary_tree(serialized_tree) root.right = _deserialize_binary_tree(serialized_tree) return root if __name__ == '__main__': root = BinaryTree(6) root.left = BinaryTree(4) root.left.left = BinaryTree(2) root.left.left.left = BinaryTree(1) root.left.left.right = BinaryTree(3) root.left.right = BinaryTree(5) root.right = BinaryTree(9) root.right.left = BinaryTree(7) root.right.left.right = BinaryTree(8) root.right.right = BinaryTree(10) root.display() serialized_binary_tree = serialize_binary_tree(root) print(serialized_binary_tree) root = deserialize_binary_tree(serialized_binary_tree)