Exemplo n.º 1
0
def test_empty():
    tree = BinaryTree()
    b, v = tree.find_max(tree)
    assert b == False

    b,v = BinaryTree().find_max(None)
    assert b == False
Exemplo n.º 2
0
 def fn3(newickTree, i):
     if newickTree[i[0]] == '(':
         #subTree1 = None
         subTreeList = []
         i[0] += 1
         k = []
         while newickTree[i[0]] != ')':
             if newickTree[i[0]] == ',':
                 i[0] += 1
             subTreeList.append(fn3(newickTree, i))
         i[0] += 1
         def cmp(i, j):
             if i.distance < j.distance:
                 return -1
             if i.distance > j.distance:
                 return 1
             return 0
         if sortNonBinaryNodes:
             subTreeList.sort(cmp)
         subTree1 = subTreeList[0]
         if len(subTreeList) > 1:
             for subTree2 in subTreeList[1:]:
                 subTree1 = BinaryTree(0.0, True, subTree1, subTree2, None)
             subTree1.iD = fn2(newickTree, i)
             subTree1.distance += fn(newickTree, i)
         elif reportUnaryNodes:
             subTree1 = BinaryTree(0.0, True, subTree1, None, None)
             subTree1.iD = fn2(newickTree, i)
             subTree1.distance += fn(newickTree, i)
         else:
             fn2(newickTree, i)
             subTree1.distance += fn(newickTree, i)
         return subTree1
     leafID = fn2(newickTree, i)
     return BinaryTree(fn(newickTree, i), False, None, None, leafID)
def test_return_all_common_values_from_both_trees():
    """
    Returns all common values from both trees
    """
    t1 = BinaryTree()
    t2 = BinaryTree()
    t1_list = [2, 6, 1, 5, 10, 9, 3, 2]
    t2_list = [22, 4, 6, 5, 9]
    for num in t1_list:
        t1.breadth_add(num)
    for num in t2_list:
        t2.breadth_add(num)
    actual = tree_intersection(t1, t2)
    expected = [5, 9, 6]
    assert actual == expected
Exemplo n.º 4
0
def test_max_val_same_el():
    tree = BinaryTree(1)
    tree.root.left = Node(-3)
    tree.root.left.left = Node(-3)
    tree.root.left.left.left = Node(-3)
    tree.root.left.left.left.left = Node(-3)
    assert tree.find_maximum_value() == 1
def test_func_match_some(tree):

    tree1 = tree
    tree2 = BinaryTree()
    one = Node(1)
    two = Node(2)
    three = Node(3)
    four = Node(4)
    five = Node(5)
    six = Node(6)
    seven = Node(7)
    eight = Node(8)
    nine = Node(9)

    one.left = two
    one.right = three
    three.left = four
    two.left = six
    six.right = seven
    seven.left = eight
    tree2.root = one

    expected = [6, 8, 7, 2, 1, 4, 3]
    actual = tree_intersection(tree1, tree2)
    assert expected == actual
Exemplo n.º 6
0
def prep():
    Binary_tree = BinaryTree()
    Binary_tree.root = Node(3)
    Binary_tree.root.right = Node(4)
    Binary_tree.root.left = Node(1)
    Binary_tree.root.right.left = Node(2)
    return Binary_tree
Exemplo n.º 7
0
def tree1():
    fifteen = Node(15)
    ten = Node(10)
    seven = Node(7)
    sixteen = Node(16)
    twelve = Node(12)
    seventeen = Node(17)
    twenty_five = Node(25)
    twenty = Node(20)
    thirty_five = Node(35)
    thirty = Node(30)
    fifty = Node(50)

    fifteen.left = ten
    ten.left = seven
    ten.right = sixteen
    sixteen.left = twelve
    sixteen.right = seventeen
    fifteen.right = twenty_five
    twenty_five.left = twenty
    twenty_five.right = thirty_five
    thirty_five.left = thirty
    thirty_five.right = fifty

    return BinaryTree(fifteen)
Exemplo n.º 8
0
def test_BinaryTree_root_right():
    node = Node(10)
    node.left = Node(5)
    node.right = Node(20)
    tree = BinaryTree(node)

    assert tree.root.right.value == 20
Exemplo n.º 9
0
def postorder_example_tree():
    tree = BinaryTree()
    n1 = Node('I')
    n2 = Node('N')
    n3 = Node('S')
    n4 = Node('C')
    n5 = Node('R')
    n6 = Node('E')
    n7 = Node('V')
    n8 = Node('A')
    n9 = Node('5')
    n0 = Node('3')

    n0.left = n6
    n0.right = n9
    n6.left = n1
    n6.right = n5
    n5.left = n2
    n5.right = n4
    n4.right = n3
    n9.left = n8
    n8.right = n7

    tree.root = n0
    return tree
Exemplo n.º 10
0
def test_post_order_traversal():
    for nodes, expected_result in [
        (
            (
                8,
                [(4,
                  [2, 6]),
                 (10,
                  [None, 20])]
            ),
            "2->6->4->20->10->8"
        ),
        (
            (1, ), "1"
        ),

        (
            (1, [2]), "2->1"
        ),

        (
            (1, [None, 2]), "2->1"
        ),
    ]:
        result = []
        def visit(node: Node) -> None:
            result.append(node.val)

        bt = BinaryTree(BinaryTree.build(nodes))
        bt.post_order(visit)

        result = "->".join([str(val) for val in result])
        assert result == expected_result, "{} != {}".format(result, expected_result)
def test_Can_successfully_instantiate_a_tree_with_a_single_root_node():
    tree = BinaryTree()
    a = Node("A")
    tree.root = a
    actual = tree.root.value
    expected = "A"
    assert actual == expected
def buildParseTree(exp):
    exp_lst = exp.split()
    root_node = BinaryTree(" ")
    current_node = root_node
    parent_stack = Stack()
    parent_stack.push(root_node)

    for e in exp_lst:
        if e in ['(']:
            current_node.insertLeft(' ')
            parent_stack.push(current_node)
            current_node = current_node.getLeftChild()
        elif e in ['+', '-', '*', '/']:
            current_node.setRootVal(e)
            current_node.insertRight(' ')
            parent_stack.push(current_node)
            current_node = current_node.getRightChild()
        elif e in [')']:
            current_node = parent_stack.pop()

        elif e not in ['+', '-', '*', '/', '(', ')']:
            try:
                current_node.setRootVal(int(e))
                current_node = parent_stack.pop()
            except ValueError:
                raise ValueError("token '{}' is not a valid number".format(e))

    return current_node
Exemplo n.º 13
0
def buildParseTree(fpexp):
    '''
    1.If the current token is a '(', add a new node as the left child of
      the current node, and descend to the left child.
    2.If the current token is in the list ['+','-','/','*'], set the root
      value of the current node to the operator represented by the current
      token. Add a new node as the right child of the current node and
      descend to the right child.
    3.If the current token is a number, set the root value of the current
      node to the number and return to the parent.
    4.If the current token is a ')', go to the parent of the current node.
    '''
    fplist = fpexp.split()
    pStack = Stack()
    eTree = BinaryTree('')
    currentTree = eTree
    for i in fplist:
        if i == '(':
            currentTree.insertLeft('')
            pStack.push(currentTree)
            currentTree = currentTree.getLeftChild()
        elif i not in ['+', '-', '*', '/', ')']:
            currentTree.setRootValue(int(i))
            parent = pStack.pop()
            currentTree = parent
        elif i in ['+', '-', '*', '/']:
            currentTree.setRootValue(i)
            currentTree.insertRight('')
            pStack.push(currentTree)
            currentTree = currentTree.getRightChild()
        elif i == ')':
            currentTree = pStack.pop()
        else:
            raise ValueError
    return eTree
Exemplo n.º 14
0
 def binary_tree(self):
     binary_tree = BinaryTree(1)
     binary_tree.root.left = Node(2)
     binary_tree.root.right = Node(3)
     binary_tree.root.left.left = Node(4)
     binary_tree.root.left.right = Node(5)
     return binary_tree
def tree():

    one = Node(1)
    two = Node(2)
    three = Node(3)
    four = Node(4)
    five = Node(5)
    six = Node(6)
    seven = Node(7)
    eight = Node(8)
    nine = Node(9)

    one.left = two
    one.right = three
    three.left = four
    three.right = five
    two.left = six
    six.right = seven
    seven.left = eight
    seven.right = nine

    arbol = BinaryTree()
    arbol.root = one

    return arbol
Exemplo n.º 16
0
def test_emoty_tree():
    """
    Can successfully instantiate an empty tree
    """

    tree = BinaryTree()
    assert tree.root is None
def test_func_match_none(tree):

    tree1 = tree
    tree2 = BinaryTree()

    expected = []
    actual = tree_intersection(tree1, tree2)
    assert expected == actual
Exemplo n.º 18
0
def tree():
    tree = BinaryTree(Node('A'))
    tree.root.left = Node('B')
    tree.root.right = Node('C')
    tree.root.left.left = Node('D')
    tree.root.left.right = Node('E')
    tree.root.right.left = Node('F')
    return tree
Exemplo n.º 19
0
def test_find_max():
    tree = BinaryTree(Node(1))
    tree.root.left = Node(3)
    tree.root.right = Node(8)
    tree.root.left.left = Node(72)
    tree.root.left.right = Node(9)
    tree.root.right.left = Node(13)
    assert tree.find_maximum_binary_tree() == 72
Exemplo n.º 20
0
def test_breadth_first_bt():
    tree = BinaryTree(4)
    tree.root.left = Node(5)
    tree.root.right = Node(7)
    tree.root.left.left = Node(9)
    tree.root.right.right = Node(3)
    tree.root.right.right.right = Node(8)
    assert tree.print_tree('breadth_first') == '4-5-7-9-3-8-'
Exemplo n.º 21
0
def test_max_val():
    tree = BinaryTree(2)
    tree.root = Node(-1)
    tree.root.left = Node(4)
    tree.root.left.left = Node(7)
    tree.root.left.left.left = Node(3)
    tree.root.left.left.left.left = Node(8)
    assert tree.find_maximum_value() == 8
Exemplo n.º 22
0
def test_tree_postorder():
    Binary_tree = BinaryTree()
    Binary_tree.root = Node(6)
    Binary_tree.root.right = Node(5)
    Binary_tree.root.left = Node(-1)
    Binary_tree.root.right.left = Node(7)
    Binary_tree.root.left.left = Node(10)
    Binary_tree.root.right.right = Node(3)
    assert Binary_tree.postOrder() == [10, -1, 7, 3, 5, 6]
Exemplo n.º 23
0
def test_return_preorder_traversal():
    tree = BinarySearchTree()
    traverse = BinaryTree()
    tree.add(3)
    tree.add(2)
    tree.add(6)
    actual = tree.pre_order()
    expected = [3,2,6]
    assert actual == expected
def test_fizz_buzz_empty():
    tree = BinaryTree()

    tree = fizz_buzz_tree(tree)

    actual = isinstance(tree, BinaryTree)
    expected = True

    assert actual == expected
Exemplo n.º 25
0
def test_tree_inorder():
    Binary_tree = BinaryTree()
    Binary_tree.root = Node(6)
    Binary_tree.root.right = Node(5)
    Binary_tree.root.left = Node(-1)
    Binary_tree.root.right.left = Node(7)
    Binary_tree.root.left.left = Node(10)
    Binary_tree.root.right.right = Node(3)
    assert Binary_tree.inOrder() == [10, -1, 6, 7, 5, 3]
def test_intersections_with_NO_dupes():
    x = BinaryTree()
    y = BinaryTree()
    a = Node("A")
    b = Node("B")
    c = Node("C")
    d = Node("D")
    e = Node("E")
    f = Node("F")
    x.root = a
    x.root.left = b
    x.root.right = c
    y.root = d
    y.root.left = e
    y.root.right = f
    actual = tree_intersection(x, y)
    expected = []
    assert actual == expected
Exemplo n.º 27
0
def test_three_node_neg_num_max():
    one = Node(-3)
    one.left = Node(-4)
    one.right = Node(-1)

    tree = BinaryTree(one)
    actual = tree.find_maximum_value(tree.root)
    expected = -1
    assert actual == expected
Exemplo n.º 28
0
def test_three_node_same_BT_max():
    one = Node(3)
    one.left = Node(3)
    one.right = Node(3)

    tree = BinaryTree(one)
    actual = tree.find_maximum_value(tree.root)
    expected = 3
    assert actual == expected
def test_intersections_with_all_dupes():
    x = BinaryTree()
    y = BinaryTree()
    a = Node("A")
    b = Node("B")
    c = Node("C")
    d = Node("A")
    e = Node("B")
    f = Node("C")
    x.root = a
    x.root.left = b
    x.root.right = c
    y.root = d
    y.root.left = e
    y.root.right = f
    actual = Counter(tree_intersection(x, y))
    expected = Counter(["C", "B", "A"])
    assert actual == expected
Exemplo n.º 30
0
def test_return_inorder_traversal():
    tree = BinarySearchTree()
    traverse = BinaryTree()
    tree.add(3)
    tree.add(2)
    tree.add(6)
    actual = tree.in_order()
    expected = [2,3,6]
    assert actual == expected