Пример #1
0
def test_tree_intersection_five():
    bt = BinaryTree()
    bt.root = Node(10)
    bt.root.left = Node(6)
    bt.root.right = Node(7)
    bt.root.left.left = Node(5)
    bt.root.left.right = Node(5)
    bt.root.left.right.left = Node(50)

    bt1 = BinaryTree()
    bt1.root = Node(10)
    expected = [10]
    assert expected == tree_intersection(bt, bt1)
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
Пример #3
0
def test_tree_intersection_one():
    bt = BinaryTree()
    bt.root = Node(1)
    bt.root.left = Node(2)
    bt.root.right = Node(3)
    bt.root.left.left = Node(4)
    bt.root.left.right = Node(5)

    bt1 = BinaryTree()
    bt1.root = Node(1)
    bt1.root.left = Node(8)
    bt1.root.right = Node(9)
    bt1.root.left.left = Node(6)
    bt1.root.left.right = Node(5)

    expected = [1, 5]
    assert expected == tree_intersection(bt, bt1)
def test_tree_bfs():
    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)
    assert bt.breadthFirst() == [6, -1, 5, 10, 7, 3]
def test_tree_max():
    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)
    assert bt.findMaximumValue() == 10
def test_tree_postorder():
    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)
    assert bt.postOrder() == [10, -1, 7, 3, 5, 6]
def test_tree_inorder():
    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)
    assert bt.inOrder() == [10, -1, 6, 7, 5, 3]
def test_tree_preorder():
    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)
    assert bt.preOrder() == [6, -1, 10, 5, 7, 3]
def test_tree_single_left_right():
    bt = BinaryTree()
    bt.root = Node(7)
    bt.root.left = Node(3)
    bt.root.right = Node(2)
    assert bt.root.value == 7
    assert bt.root.left.value == 3
    assert bt.root.right.value == 2
    assert bt.preOrder() == [7, 3, 2]
Пример #10
0
def test_tree_intersection_three():
    bt = BinaryTree()
    bt.root = Node(10)
    bt.root.left = Node(6)
    bt.root.right = Node(7)
    bt.root.left.left = Node(5)
    bt.root.left.right = Node(5)
    bt.root.left.right.left = Node(50)

    bt1 = BinaryTree()
    bt1.root = Node(11)
    bt1.root.left = Node(8)
    bt1.root.right = Node(9)
    bt1.root.left.left = Node(6)
    bt1.root.left.right = Node(12)
    bt1.root.left.right.left = Node(0)
    expected = None
    assert expected == tree_intersection(bt, bt1)
Пример #11
0
def my_tree():
    tree = BinaryTree()
    tree.root = Node(6)
    tree.root.left = Node(15)
    tree.root.right = Node(7)
    tree.root.left.left = Node(23)
    tree.root.left.right = Node(21)
    tree.root.right.right = Node(5)
    tree.root.left.left.left = Node(15)
    tree.root.right.right.left = Node(7)
    return tree
Пример #12
0
def test_find_max_value():
    bt = BinaryTree()
    bt.root = Node(2)
    bt.root.right = Node(5)
    bt.root.left = Node(7)
    bt.root.right.right = Node(9)
    bt.root.right.right.left = Node(4)
    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)
    expected = 11
    actual = bt.find_maximum_value()
    assert expected == actual
Пример #13
0
def test_breadth_first_binarytree():
    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.left.right.left = Node(5)
    bt.root.left.right.right = Node(11)
    bt.root.right.right.left = Node(4)

    assert BinaryTree.breadth_first_traversal(bt) == [
        2, 7, 5, 2, 6, 9, 5, 11, 4
    ]
def test_tree_single():
    bt = BinaryTree()
    bt.root = Node(7)
    assert bt.root.value == 7
Пример #15
0
def test_breadth_first_binarytree_one_element():
    bt = BinaryTree()
    bt.root = Node(8)
    assert BinaryTree.breadth_first_traversal(bt) == [8]
        """
        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


if __name__ == "__main__":
    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.left.right.left = Node(5)
    bt.root.left.right.right = Node(11)
    bt.root.right.right.left = Node(4)

    print(fizz_buzz_tree(bt))