def fizz_buzz_tree(k_ary): """ Function to change all values in the given tree according to fizz_buzz """ new_tree = BinaryTree() if not k_ary.root: return new_tree def helper(current): """ Helper function to use in recurtion to add new values in the new_tree according to their positions in the original tree """ node = Node(fizz_buzz(current.value)) if current.left: node.left = helper(current.left) if current.right: node.right = helper(current.right) return node new_tree.root = helper(k_ary.root) return new_tree
def fizz_buzz_tree(tree): """ Function to change all values in the given tree according to fizz_buzz: inp ---> tree to be checked.. out >>> new tree with fizz - buzz or the same number if it not.. """ try: new_tree = BinaryTree() if not tree.root: return new_tree def helper(current): """ Helper function to use in recurtion to add new values in the new_tree according to their positions in the original tree """ node = Node(fizz_buzz(current.value)) if current.left: node.left = helper(current.left) if current.right: node.right = helper(current.right) return node new_tree.root = helper(tree.root) return new_tree except: raise ValueError('your input not tree')
def test_tree_with_one_value(): test = BinaryTree() test.root = _Node(5) expected = ['Buzz'] actual = breadth_first_traversal(fizz_buzz_tree(test).root) assert expected == actual
def test_find_max__imbalanced_binarytree(): tree = BinaryTree() tree._root = _Node(-2) tree._root.left = _Node(5) tree._root.left.left = _Node(9) tree._root.left.left.left = _Node(2) tree._root.left.left.left.left = _Node(2) assert tree.find_maximum_value() == 9
def prep(): bt = BinaryTree() bt.root = Node(5) bt.root.right = Node(10) bt.root.left = Node(-2) bt.root.right.left = Node(5) bt.root.left.left = Node(17) bt.root.right.right = Node(8) return bt
def test_leftchild_rightchild(): bt = BinaryTree() bt.root = Node(5) bt.root.left = Node(4) bt.root.right = Node(3) assert bt.root.value == 5 assert bt.root.left.value == 4 assert bt.root.right.value == 3 assert bt.preorder() == [5, 4, 3]
def test_post_order_traversal(): tree = BinaryTree(1) tree.root.left = Node(2) tree.root.right = Node(3) tree.root.left.left = Node(4) tree.root.left.right = Node(5) tree.root.right.left = Node(6) tree.root.right.right = Node(7) assert tree.print_tree("postorder") == '4-5-2-6-7-3-1-'
def test_find_max_value(): tree = BinaryTree(1) tree.root.left = Node(2) tree.root.right = Node(3) tree.root.left.left = Node(4) tree.root.left.right = Node(5) tree.root.right.left = Node(6) tree.root.right.right = Node(7) assert tree.find_maximum_value() == 7
def test_breadth_first_correct_order(): tree = BinaryTree(1) tree.root.left = Node(2) tree.root.right = Node(3) tree.root.left.left = Node(4) tree.root.left.right = Node(5) tree.root.right.left = Node(6) tree.root.right.right = Node(7) assert tree.print_tree('breadthfirst') == [1, 2, 3, 4, 5, 6, 7]
def prep(): bt = BinaryTree() bt.root = Node(6) bt.root.right = Node(10) bt.root.left = Node(5) bt.root.left.left = Node(-1) bt.root.right.right = Node(15) bt.root.right.left = Node(7) return bt
def prep_max_min(): bt = BinaryTree() bt.root = Node(6) bt.root.right = Node(10) bt.root.left = Node(5) bt.root.left.left = Node(20) bt.root.right.right = Node(15) bt.root.right.left = Node(-1) return bt
def fizzy_tree(): ## preparing data tree = BinaryTree() tree.root = Node(3) tree.root.left = Node(5) tree.root.right = Node(7) tree.root.left.left = Node(15) return tree
def test_in_order_traversal(): tree = BinaryTree(1) tree.root.left = Node(2) tree.root.right = Node(3) tree.root.left.left = Node(4) tree.root.left.right = Node(5) tree.root.right.left = Node(6) tree.root.right.right = Node(7) assert tree.print_tree("inorder") == '4-2-5-1-6-3-7-'
def test_fizz_buzz_tree3(): bt = BinaryTree() bt.root = Node(3) bt.root.left = Node(5) bt.root.right = Node(6) bt.root.left.left = Node(9) bt.root.right.right = Node(15) actual = fizz_buzz_tree(bt).preorder() expected = ['Fizz', 'Buzz', 'Fizz', 'Fizz', 'FizzBuzz'] assert actual == expected
def test_pre_order_traversal(): tree = BinaryTree(1) tree.root.left = Node(2) tree.root.right = Node(3) tree.root.left.left = Node(4) tree.root.left.right = Node(5) tree.root.right.left = Node(6) tree.root.right.right = Node(7) assert tree.print_tree("preorder") == '1-2-4-5-3-6-7-'
def prepare_bt(): bt = BinaryTree() bt.root = Node(7) bt.root.left = Node(13) bt.root.right = Node(5) bt.root.left.left = Node(8) bt.root.left.right = Node(9) bt.root.right.left = Node(1) bt.root.right.right = Node(-4) return bt
def test_breadth_first_bt(): bt = BinaryTree() bt.root = Node(6) bt.root.right = Node(5) bt.root.left = Node(-1) bt.root.right.left = Node(7) bt.root.left.left = Node(10) bt.root.right.right = Node(3) actual = bt.breadth_first(bt) expected = [6, 5, 3, 7, -1, 10] assert actual == expected
def test_breadth_first_bt2(): bt = BinaryTree() bt.root = Node(2) bt.root.right = Node(7) bt.root.left = Node(6) bt.root.right.left = Node(2) bt.root.left.left = Node(9) bt.root.right.right = Node(5) actual = bt.breadth_first(bt) expected = [2, 7, 5, 2, 6, 9] assert actual == expected
def test_fizz_buzz_tree2(): bt = BinaryTree() bt.root = Node(1) bt.root.left = Node(2) bt.root.right = Node(4) bt.root.left.left = Node(13) bt.root.left.right = Node(7) bt.root.right.right = Node(8) bt.root.right.right = Node(17) actual = fizz_buzz_tree(bt).preorder() expected = [1, 2, 13, 7, 4, 17] assert actual == expected
def test_breadth_first_traversal1(): bt = BinaryTree() bt.root = Node(2) bt.root.left = Node(7) bt.root.right = Node(5) bt.root.left.left = Node(2) bt.root.left.right = Node(6) bt.root.left.right.left = Node(5) bt.root.left.right.right = Node(11) bt.root.right.right = Node(9) bt.root.right.right.left = Node(4) assert bt.breadthFirst(bt.root) == [2,7,5,2,6,9,5,11,4]
def test_find_max_binarytree_several_el(): tree = BinaryTree() tree._root = _Node(8) tree._root.left = _Node(10) tree._root.right = _Node(-2) assert tree.find_maximum_value() == 10 tree._root.left.left = _Node(195) tree._root.left.right = _Node(-195) tree._root.right.right = _Node(10000) tree._root.left.left.left = _Node(-0.567) tree._root.left.left.right = _Node(10000) assert tree.find_maximum_value() == 10000
def fizz_buzz_tree(k_ary): tree = BinaryTree() if not k_ary.root: return tree def _walk(current): node = Node(comapre(current.value)) if current.left: node.left = _walk(current.left) if current.right: node.right = _walk(current.right) return node tree.root = _walk(k_ary.root) return tree
def test_find_max_binarytree_same_values(): tree = BinaryTree() tree._root = _Node(-2) tree._root.left = _Node(-2) tree._root.right = _Node(-2) tree._root.left.left = _Node(-2) tree._root.left.right = _Node(-2) tree._root.right.right = _Node(-2) tree._root.left.left.left = _Node(-2) tree._root.left.left.right = _Node(-2) assert tree.find_maximum_value() == -2 tree._root.right.left = _Node(1) assert tree.find_maximum_value() == 1
def fizz_buzz_tree(tree): ''' takes a k-ary tree as an argument determine whether or not the value of each node is divisible by 3, 5 or both. ''' new_tree = BinaryTree() new_node = Node() if tree.root == None: return new_tree old_node = tree.root # root is ref to first node def visit_node(old_node, new_node): new_node.data = fizz_buzz(old_node.data) if old_node.left: new_node.left = Node() visit_node(old_node.left, new_node.left) if old_node.right: new_node.right = Node() visit_node(old_node.right, new_node.right) def fizz_buzz(old_value): result = '' if old_value % 3 == 0: result += 'Fizz' if old_value % 5 == 0: result += 'Buzz' return result or str(old_value) visit_node(old_node, new_node) new_tree.root = new_node return new_tree # if __name__ == "__main__": # tree = BinaryTree() # tree.root = Node(3) # tree.root.left = Node(5) # tree.root.right = Node(7) # tree.root.left.left = Node(15) # print(fizz_buzz_tree(tree))
def test_fizz_buzz_tree(): bt = BinaryTree() bt.root = _Node(2) bt.root.left = _Node(7) bt.root.right = _Node(5) bt.root.left.left = _Node(2) bt.root.left.right = _Node(6) bt.root.right.right = _Node(9) bt.root.right.right.left = _Node(4) bt.root.left.right.right = _Node(11) bt.root.left.right.left = _Node(15) new = fizz_buzz_tree(bt) assert breadth_first_traversal(new.root) == [ '2', '7', 'Buzz', '2', 'Fizz', 'Fizz', 'FizzBuzz', '11', '4' ]
def test_everything_match(my_tree): tree1 = my_tree values = [7, 3, 2, 6, 9, 10, 1, 5, 8] tree2 = build(values) assert BinaryTree.tree_intersection(tree1, tree2) == {7, 3, 2, 6, 9, 10, 1, 5, 8}
def test_tree_with_minus(): test = BinaryTree() test.root = _Node(-2) test.root.left = _Node(-7) test.root.right = _Node(-5) test.root.left.left = _Node(-2) test.root.left.right = _Node(-6) test.root.right.right = _Node(-9) test.root.right.right.left = _Node(-4) test.root.left.right.right = _Node(-15) test.root.left.right.left = _Node(0) expected = [ '-2', '-7', 'Buzz', '-2', 'Fizz', 'Fizz', 'FizzBuzz', 'FizzBuzz', '-4' ] actual = breadth_first_traversal(fizz_buzz_tree(test).root) assert expected == actual
def test_difrent_tree_type(): tree3 = BinaryTree() tree3.root = Node(5) tree3.root.right = Node(6) tree3.root.left = Node(4) tree3.root.right.right = Node(7) tree3.root.right.left = Node(5) tree3.root.left.left = Node(2) tree3.root.left.right = Node(4) tree4 = BinarySearchTree() tree4.add(4) tree4.add(6) tree4.add(7) tree4.add(5) tree4.add(3) tree4.add(3) tree4.add(2) actual = tree_intersection(tree3, tree4) expected = [6, 2, 5, 7] assert actual == expected
def test_intersection_tree(): bt = BinaryTree() bt1 = BinaryTree() bt.root = Node(150) bt.root.left = Node(100) bt.root.right = Node(250) bt.root.left.left = Node(75) bt.root.left.right = Node(160) bt.root.right.left = Node(200) bt.root.right.right = Node(350) bt.root.left.right.left = Node(125) bt.root.left.right.right = Node(175) bt.root.right.right.left = Node(300) bt.root.right.right.right = Node(500) bt1.root = Node(42) bt1.root.left = Node(100) bt1.root.right = Node(600) bt1.root.left.left = Node(15) bt1.root.left.right = Node(160) bt1.root.right.left = Node(200) bt1.root.right.right = Node(350) bt1.root.left.right.left = Node(125) bt1.root.left.right.right = Node(175) bt1.root.right.right.left = Node(4) bt1.root.right.right.right = Node(500) actual = tree_intersection(bt, bt1) expected = [100, 160, 125, 175, 200, 350, 500] assert actual == expected
def test_test_three_common(): tr1 = BinaryTree() tr2 = BinaryTree() tr1.root = Node(5) tr1.root.right = Node(10) tr1.root.left = Node(-2) tr1.root.right.left = Node(15) tr1.root.left.left = Node(17) tr2.root = Node(5) tr2.root.right = Node(11) tr2.root.left = Node(-2) tr2.root.right.left = Node(15) tr2.root.left.left = Node(111) assert tree_intersection(tr1, tr2) == [5, -2, 15]