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 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]
Ejemplo n.º 3
0
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_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 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)
Ejemplo n.º 6
0
 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
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]
def test_test_with_string():
    tr1 = BinaryTree()
    tr2 = BinaryTree()
    tr1.root = Node(5)
    tr1.root.right = Node('omar')
    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('omar')
    assert tree_intersection(tr1, tr2) == [5, -2, 'omar', 15]
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]
Ejemplo n.º 10
0
def test_intersection_tree2():
    bt = BinaryTree()
    bt1 = BinaryTree()
    bt.root = Node(0)
    bt.root.left = Node(5)
    bt.root.right = Node(10)
    bt.root.left.left = Node(15)
    bt.root.left.right = Node(20)
    bt1.root = Node(5)
    bt1.root.left = Node(15)
    bt1.root.right = Node(10)
    bt1.root.left.left = Node(20)
    bt1.root.left.right = Node(0)
    actual = tree_intersection(bt, bt1)
    expected = [10]
    assert actual == expected
Ejemplo n.º 11
0
def test_intersection_tree1():
    bt = BinaryTree()
    bt1 = BinaryTree()
    bt.root = Node(0)
    bt.root.left = Node(5)
    bt.root.right = Node(10)
    bt.root.left.left = Node(15)
    bt.root.left.right = Node(20)
    bt1.root = Node(6)
    bt1.root.left = Node(12)
    bt1.root.right = Node(18)
    bt1.root.left.left = Node(24)
    bt1.root.left.right = Node(30)
    actual = tree_intersection(bt, bt1)
    expected = None
    assert actual == expected
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 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 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 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_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
Ejemplo n.º 17
0
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]
Ejemplo n.º 18
0
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
Ejemplo n.º 19
0
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():
    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
Ejemplo n.º 21
0
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_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_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_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 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_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_same_tree_type():
    tree1 = BinaryTree()
    tree1.root = Node(5)
    tree1.root.right = Node(6)
    tree1.root.left = Node(4)
    tree1.root.right.right = Node(7)
    tree1.root.right.left = Node(5)
    tree1.root.left.left = Node(2)
    tree1.root.left.right = Node(4)
    tree2 = BinaryTree()
    tree2.root = Node(4)
    tree2.root.right = Node(6)
    tree2.root.right.right = Node(7)
    tree2.root.right.left = Node(5)
    tree2.root.left = Node(3)
    tree2.root.left.right = Node(3)
    tree2.root.left.left = Node(2)
    actual = tree_intersection(tree1, tree2)
    expected = [6, 2, 5, 7]
    assert actual == expected
def test_add_left_and_right_childeren_to_a_node():
    tree = BinaryTree()
    left = tree.root.left = Node(2)
    right = tree.root.right = Node(3)
    assert tree.root.left.value == 2
    assert tree.root.right.value == 3
    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