Exemple #1
0
def test_breadth_on_big_left_branch():
    tree = BinaryTree()
    tree.add(1)
    tree.root.left = Node(2)
    tree.root.left.left = Node(3)
    tree.root.left.left.left = Node(4)
    tree.root.left.left.left.left = Node(5)
    assert tree.breadth_traverse() == [1, 2, 3, 4, 5]
Exemple #2
0
def test_post_order_traversal():
    tree = BinaryTree()
    tree.root = Node(1)
    tree.root.left = Node(2)
    tree.root.right = Node(3)
    tree.root.left.left = Node(4)
    tree.root.left.right = Node(5)
    actual = tree.post_order()
    expected = [4, 5, 2, 3, 1]
    assert actual == expected
def test_ret_in_order():
    tree = BinaryTree()
    tree.root = Node(1)
    tree.root.left = Node(2)
    tree.root.right = Node(3)
    tree.root.left.left = Node(4)
    tree.root.left.right = Node(5)
    actual = BinaryTree.breadth_first(tree)
    expected = [1, 2, 3, 4, 5]
    assert actual == expected
def test_find_maximum_on_left():
    grok = BinaryTree()
    groot = Node(12)
    i = Node(9)
    am = Node(5)
    starlord = Node(3)
    rocket = Node(7)
    grok.root = i
    grok.root.left = am
    grok.root.right = starlord
    grok.root.left.left = groot
    grok.root.left.right = rocket
    actual = grok.find_maximum_value()
    assert actual == 12
def test_breadth_first():
    grok = BinaryTree()
    groot = Node('Groot')
    i = Node('I')
    am = Node('am')
    starlord = Node('Iowa')
    rocket = Node('Trash Panda')
    grok.root = i
    grok.root.left = am
    grok.root.right = groot
    grok.root.left.left = starlord
    grok.root.left.right = rocket

    expected = ['I', 'am', 'Groot', 'Iowa', 'Trash Panda']
    assert grok.breadth_first() == expected
def test_breadth_first_one_node():
    tree = BinaryTree()
    a = Node('A')
    tree.root = a
    actual = tree.breadth_first()
    expected = ['A']
    assert actual == expected
def test_one_node():
    tree = BinaryTree()
    node1 = Node(5)
    tree.root = node1
    actual = tree.breadth_first()
    expected = [5]
    assert actual == expected
def test_post_order():
    grok = BinaryTree()
    groot = Node('Groot')
    i = Node('I')
    am = Node('Am')
    starlord = Node('Future Antman')
    rocket = Node('Trash Panda')
    grok.root = i
    grok.root.left = am
    grok.root.right = starlord
    grok.root.left.left = groot
    grok.root.left.right = rocket

    assert grok.post_order() == [
        'Groot', 'Trash Panda', 'Am', 'Future Antman', 'I'
    ]
def test_binary_tree_instance():
    grok = BinaryTree()
    assert grok.root == None
    groot = Node('I am Groot')
    grok.root = groot
    assert grok.root.value == 'I am Groot'
    assert grok.__repr__() == 'An instance of a BinaryTree, root is I am Groot'
Exemple #10
0
def test_one_interstions():
    tree1 = BinarySearchTree()

    tree1.add(2)
    tree1.root.left = Node(1)
    tree1.root.right = Node(3)

    tree2 = BinarySearchTree()

    tree2.add(2)
    tree2.root.left = Node(0)
    tree2.root.right = Node(4)

    actual = tree1.tree_intersections(tree2)
    expect = [2]

    assert actual == expect
Exemple #11
0
def test_multiple_intersections():
    tree1 = BinarySearchTree()

    tree1.add(2)
    tree1.root.left = Node(1)
    tree1.root.right = Node(3)

    tree2 = BinarySearchTree()

    tree2.add(2)
    tree2.root.left = Node(1)
    tree2.root.right = Node(3)

    actual = tree1.tree_intersections(tree2)
    expect = [1, 2, 3]

    assert actual == expect
def test_assign_left_right():
    groot = Node('Groot')
    eye = Node('I')
    am = Node('Am')
    groot.left = eye
    groot.right = am
    assert groot.left.value == 'I'
    assert groot.right.value == 'Am'
def test_add_left_and_right():
    tree = BinaryTree()
    node1 = Node(1)
    node2 = Node(3)
    node3 = Node(5)
    node1.left = node2
    node1.right = node3
    tree.root = node1
    assert node1.left.value == 3 and node1.right.value == 5
Exemple #14
0
def test_breadth_balanced_negative_tree():
    tree = BinaryTree()
    tree.add(-1)
    tree.root.left = Node(-2)
    tree.root.left.left = Node(-4)
    tree.root.left.right = Node(-5)
    tree.root.right = Node(-3)
    tree.root.right.left = Node(-6)
    tree.root.right.right = Node(-7)
    assert tree.breadth_traverse() == [-1, -2, -3, -4, -5, -6, -7]
Exemple #15
0
def test_breadth_balanced_tree():
    tree = BinaryTree()
    tree.add(1)
    tree.root.left = Node(2)
    tree.root.left.left = Node(4)
    tree.root.left.right = Node(5)
    tree.root.right = Node(3)
    tree.root.right.left = Node(6)
    tree.root.right.right = Node(7)
    assert tree.breadth_traverse() == [1, 2, 3, 4, 5, 6, 7]
def test_left_right_child_tree():
    tree = BinaryTree()
    one = Node(25)
    two = Node(12)
    three = Node(30)
    tree.root = one
    one.left = two
    one.right = three
    assert one.left and one.right
    expected = two and three
def test_return_breadth_list():
    tree = BinaryTree()
    node1 = Node(1)
    node2 = Node(9)
    node3 = Node(5)
    node1.left = node2
    node1.right = node3
    tree.root = node1
    actual = tree.breadth_first()
    expected = [1, 9, 5]
    assert actual == expected
def test_return_desired_value():
    tree = BinaryTree()
    node1 = Node(1)
    node2 = Node(3)
    node3 = Node(5)
    node1.left = node2
    node1.right = node3
    tree.root = node1
    actual = tree.find_max_value()
    expected = 5
    assert actual == expected
def test_postorder_traversal():
    tree = BinaryTree()
    node1 = Node(1)
    node2 = Node(3)
    node3 = Node(5)
    node1.left = node2
    node1.right = node3
    tree.root = node1
    actual = tree.post_order()
    expected = [3, 5, 1]
    assert actual == expected
def test_post_order():
    tree = BinaryTree()
    tree.root = Node(50)
    tree.root.left = Node(25)
    tree.root.right = Node(75)
    tree.root.left.left = Node(20)
    tree.root.right.right = Node(80)
    tree.root.left.right = Node(40)
    """
                50
            25      75
        20    40          80
    """
    actual = tree.post_order()
    expected = [20, 40, 25, 80, 75, 50]
    assert actual == expected
def test_bst():
    bst = BinarySearchTree()
    one = Node(15)
    two = Node(12)
    three = Node(25)
    four = Node(24)
    five = Node(29)
    six = Node(36)
    bst.root = one
    one.left = two
    one.right = three
    three.left = four
    three.right = five
    five.right = six
    return bst
def test_tree():
    tree = BinaryTree()
    one = Node(1)
    two = Node(2)
    three = Node(3)
    four = Node(4)
    five = Node(5)
    six = Node(6)

    tree.root = one
    one.left = two
    one.right = three
    two.left = four
    two.right = five
    three.left = six

    return tree
def test_tree2():
    tree = BinaryTree()
    tree.root = Node(50)
    tree.root.left = Node(25)
    tree.root.right = Node(75)
    tree.root.left.left = Node(20)
    tree.root.right.right = Node(80)
    tree.root.left.right = Node(40)
    tree2 = BinaryTree()
    tree2.root = Node(50)
    tree2.root.left = Node(3)
    tree2.root.right = Node(4)
    tree2.root.left.left = Node(6)
    tree2.root.right.right = Node(7)
    tree2.root.left.right = Node(8)
    actual = tree_intersection(tree, tree2)
    expected = [50]
    assert actual == expected
def test_breadth_first_odd():
    tree = BinaryTree()
    a = Node('A')
    b = Node('B')
    c = Node('C')
    d = Node('D')
    e = Node('E')
    f = Node('F')
    g = Node('G')

    tree.root = a
    a.left = b
    a.right = c
    b.left = d
    b.right = e
    c.left = f
    c.right = g
    actual = tree.breadth_first()
    expected = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
    assert actual == expected
def test_find_maxval_onenode_bt():
    tree = BinaryTree(Node(-10))
    expected = -10
    actual = tree.find_maximum_value()
    assert actual == expected
Exemple #26
0
def test_one_root_on_tree():
    tree = BinaryTree(Node())
    actual = tree.root.value
    expected = None
    assert actual == expected
Exemple #27
0
def test_node_has_value():
    node = Node("apple")
    assert node.value == "apple"
Exemple #28
0
def test_node_has_left_of_none():
    node = Node("apple")
    assert node.left is None
Exemple #29
0
def test_node_has_right_of_none():
    node = Node("apple")
    assert node.right is None
def test_tree1():
    tree = BinaryTree()
    tree.root = Node(50)
    tree.root.left = Node(25)
    tree.root.right = Node(75)
    tree.root.left.left = Node(20)
    tree.root.right.right = Node(80)
    tree.root.left.right = Node(40)
    tree2 = BinaryTree()
    tree2.root = Node(50)
    tree2.root.left = Node(25)
    tree2.root.right = Node(75)
    tree2.root.left.left = Node(20)
    tree2.root.right.right = Node(80)
    tree2.root.left.right = Node(40)
    actual = tree_intersection(tree, tree2)
    expected = [50, 25, 20, 40, 75, 80]
    assert actual == expected