def test_instantiate_and_single_root_node():
    bt = BinaryTree()
    actual = bt.preorder()
    expected = 'tree is empty'
    assert actual == expected
    bt.root = Node(5)
    assert bt.preorder() == [5]
def prep():
    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)
    return bt
def test_find_maximum_value(prep):
    assert prep.find_maximum_value() == 10
    prep.root.right.right.right = Node(99)
    assert prep.find_maximum_value() == 99
    new_tree = BinaryTree()
    assert new_tree.find_maximum_value() == 'tree is empty'
    new_tree.root = Node(8)
    new_tree.root.right = Node(5)
    new_tree.root.left = Node(-1)
    assert new_tree.find_maximum_value() == 8
Beispiel #4
0
def test_breadth_first():
    bt = BinaryTree()
    assert breadth_first(bt)=="tree is empty"
    bt.root = Node(6)
    bt.root.right = Node(5)
    assert breadth_first(bt)==[6, 5]
    bt.root.left = Node(-1)
    assert breadth_first(bt)==[6, -1, 5]
    bt.root.right.left = Node(7)
    bt.root.left.left = Node(10)
    bt.root.right.right = Node(100)
    bt.root.right.right.left = Node(199)
    assert breadth_first(bt)==[6, -1, 5, 10, 7, 100, 199]
Beispiel #5
0
def test_fizz_buzz():
    tree = BinaryTree()
    assert FizzBuzzTree(tree) == 'tree is empty'
    tree.root = Node(5)
    assert FizzBuzzTree(tree).preorder() == ['Buzz']
    tree.root.right = Node(8)
    assert FizzBuzzTree(tree).preorder() == ['Buzz', '8']
    tree.root.left = Node(6)
    assert FizzBuzzTree(tree).preorder() == ['Buzz', 'Fizz', '8']
    tree.root.right.left = Node(21)
    tree.root.right.right = Node(11)
    tree.root.left.left = Node(31)
    tree.root.left.right = Node(51)
    tree.root.right.left.right = Node(1)
    tree.root.right.right.left = Node(2)
    tree.root.left.left.right = Node(3)
    tree.root.left.right.left = Node(4)
    tree.root.left.right.right = Node(5)
    tree.root.left.left.left = Node(15)
    assert FizzBuzzTree(tree).preorder() == [
        'Buzz', 'Fizz', 'FizzBuzz', 'Buzz', '8', 'Fizz', 'Fizz', '1', '31',
        'Fizz', '11', '2', '4'
    ]
Beispiel #6
0
def FizzBuzzTree(bt):

    if not bt.root:
        return "tree is empty"
    new_tree = BinaryTree()

    def _walk(node):
        new_tree.insert(fizzBuzz(node))
        if node.left:
            _walk(node.left)
        if node.right:
            _walk(node.right)
        return new_tree

    return _walk(bt.root)
Beispiel #7
0
def test_tree_intersection():
    tr1 = BinaryTree()
    tr2 = BinaryTree()
    assert tree_intersection(tr1, tr2) == []
    tr1.root = Node(5)
    assert tree_intersection(tr1, tr2) == []
    tr1.root.right = Node(10)
    tr1.root.left = Node(-2)
    tr1.root.right.left = Node(15)
    tr1.root.left.left = Node('string')
    tr2.root = Node(5)
    assert tree_intersection(tr1, tr2) == [5]
    tr2.root.right = Node(11)
    tr2.root.left = Node(-2)
    tr2.root.right.left = Node(15)
    tr2.root.left.left = Node('string')
    assert tree_intersection(tr1, tr2) == [5, -2, 'string', 15]
Beispiel #8
0
        list1 = tree1.preorder()
        list2 = tree2.preorder()
        list2 = set(list2)
        for i in list1:
            if i in list2:
                output.append(i)
    return output


if __name__ == "__main__":
    # tree1=BinaryTree()
    # tree2=BinaryTree()
    # tree1.root= Node(5)
    # tree1.root.left= Node(7)
    # tree1.root.right= Node(-1)
    # tree2.root= Node(4)
    # tree2.root.left= Node(7)
    # tree2.root.right= Node(-1)
    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)
    print(tree_intersection(tr1, tr2))
Beispiel #9
0
        return "tree is empty"
    new_tree = BinaryTree()

    def _walk(node):
        new_tree.insert(fizzBuzz(node))
        if node.left:
            _walk(node.left)
        if node.right:
            _walk(node.right)
        return new_tree

    return _walk(bt.root)


if __name__ == "__main__":
    tree = BinaryTree()
    tree.root = Node(5)
    tree.root.right = Node(8)
    tree.root.left = Node(6)
    tree.root.right.left = Node(21)
    tree.root.right.right = Node(11)
    tree.root.left.left = Node(31)
    tree.root.left.right = Node(51)
    tree.root.right.left.right = Node(1)
    tree.root.right.right.left = Node(2)
    tree.root.left.left.right = Node(3)
    tree.root.left.right.left = Node(4)
    tree.root.left.right.right = Node(5)
    tree.root.left.left.left = Node(15)

    # print(tree.preorder())
Beispiel #10
0
from data_structures_and_algorithms.Data_Structures.tree.tree import BinaryTree, Node


def breadth_first(bt):
    arr = []
    if not bt.root:
        return "tree is empty"
    arr.append(bt.root)
    output = []
    while arr:
        front = arr.pop(0)
        output.append(front.value)
        if front.left:
            arr.append(front.left)
        if front.right:
            arr.append(front.right)
    return output


if __name__ == "__main__":
    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(100)
    bt.root.right.right.left = Node(199)
    print(breadth_first(bt))