예제 #1
0
 def teste_insercao(self):
     print("TESTE DE INSERCAO NA ARVORE B")
     keys = [
         20, 10, 40, 50, 30, 55, 3, 11, 4, 28, 36, 33, 52, 17, 25, 13, 54,
         9, 43, 8, 48
     ]
     arvore_b_ordem_2 = BTree(2)
     for i in keys:
         arvore_b_ordem_2.insert_key(i)
     arvore_b_ordem_2.print_tree()
     print()
예제 #2
0
    def teste_delete(self):
        print("TESTE DE DELECAO NA ARVORE B")
        keys = [
            20, 10, 40, 50, 30, 55, 3, 11, 4, 28, 36, 33, 52, 17, 25, 13, 54,
            9, 43, 8, 48
        ]
        arvore_b_ordem_2 = BTree(2)
        for i in keys:
            arvore_b_ordem_2.insert_key(i)

        chave_deletada = [36, 33, 3, 10, 25]
        for i in chave_deletada:
            print("Key retirada:", i)
            arvore_b_ordem_2.delete_key(i)
            arvore_b_ordem_2.print_tree()
        print()
예제 #3
0
def test():
    data = [1,2,3,4,5,6,7,8,9,10]
    node = mini_tree(data)
    print("tree hight:", BTree.hight(node))
    BTree.print_tree(node)

    # list of depths
    lst = list_of_depths(node)
    for i in range(len(lst)):
        node = lst[i]
        s = ''
        while node:
            s = s + '{},'.format(node.data.value)
            node = node.next
        #print("level {}: {}".format(i, s))


    # check balance
    print('-- Check balance --')
    node = mini_tree([1])
    print(check_balance(node))   # True
    node = mini_tree([1,2,3])   
    print(check_balance(node))   # True
    node = mini_tree([1,2,3,4,5,6])   
    print(check_balance(node))   # False
    node = mini_tree([1,2,3,4,5,6,7])   
    print(check_balance(node))   # True
    node = mini_tree([1,2,3,4,5,6,7,8,9])   
    print(check_balance(node))   # False
    node = mini_tree([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])   
    print(check_balance(node))   # True

    # validate BST
    print('-- Validate BST --')
    node = mini_tree([1,2,3,4,5,6])   
    print(validate_bst(node))   # True
    node = mini_tree([10, 1,2,3,4,5,6])   
    print(validate_bst(node))   # False
    node = BTree.generate([20,10,30,5,15,3,7,15,17])
    print(validate_bst(node))   # True

    # build dependency
    print("-- Build dependency --")
    projects = ['a','b','c','d','e','f']
    dependencies = [('a', 'd'), ('f', 'b'), ('b', 'd'), ('f', 'a'), ('d', 'c')]
    print(build_dependency(projects, dependencies))    # f, e, a, b, d, c

    # first_common_ancestor
    print('first_common_ancestor')
    data = [7,5,15,2,6,13,3,4,9,1,11,8,10,0,14,12]
    tree = BDTree.generate(data)
    print(tree)
    node1 = tree.get_node(data=13)
    node2 = tree.get_node(data=9)
    node = first_common_ancestor(node1, node2)
    print(node.data)

    # check_subtree
    print('check_subtree: ', end='')
    data1 = [20,10,50,7,13,36,98,4,8,12,17,28,44,80,127,2,5,9,6,60,79,1]
    data2 = [4,5,2,6,1]
    t1 = BTree.generate(data1)
    t2 = BTree.generate(data2)
    print(check_subtree(t1, t2))
    t3 = BTree.generate(data2, sorted=False)
    print(check_subtree(t1, t3))

    # path_with_sum
    print('path_with_sum')
    t = BTree.generate([10,5,15,3,8,12,19,2,4,6,9,0,1,7])
    print(path_with_sum(t, 20))
    t2 = BTree.generate([2,1,3,1,2,1,3,1,1,3,1,2,1,3,1,1,1,1,2,1])
    print(path_with_sum(t2, 5))

    # tree_min_max_depth
    print('tree_min_max_depth')
    t = BTree.generate([10,5,15,3,8,12,19,2,4,6,9,0,1,7])
    print(t)
    print(tree_min_max_depth(t))
    t = BTree.generate([7,4,10,2,5,8,9])
    print(t)
    print(tree_min_max_depth(t))