コード例 #1
0
 def test_insert_1(self):
     print("Running test_insert_1")
     h = AVL([5, 2, 8, 1, 3, 9])
     self.check_multiline(
         str(h),
         [["5"], ["2", "8"], ["1", "3", "_", "9"],
          ["_", "_", "_", "_", "_", "_"], ["1", "2", "3", "5", "8", "9"]])
コード例 #2
0
def traversal():
    # a = [i for i in range(20)]
    # a = [randrange(11111) for i in range(1230)]
    a = [3, 1, 2]
    avl = AVL(a)
    l = avl.traversal()
    print(str(l))
コード例 #3
0
 def test_insert_single_node(self):
     print("Running test_insert_single_node")
     h = AVL()
     h.insert(5)
     self.assertEqual(h.get_root().get_key(), 5)
     self.assertEqual(str(h.get_root()), '5')
     self.check_multiline(str(h), [["5"], ["_", "_"], ["5"]])
コード例 #4
0
ファイル: teste_avl.py プロジェクト: giselaortt/Arvores
def teste_inicial():
    arv = AVL()

    # inserindo usuários
    arv.inserir(3, 'Igor')
    arv.inserir(6, 'Jurema')
    arv.inserir(1, 'Lidia')
    arv.inserir(4, 'Carlos')
    arv.inserir(24, 'gi')
    arv.inserir(2, 'aurora')

    # buscando usuários
    teste_busca1 = arv.busca(3)
    print(teste_busca1.nome)
    teste_busca2 = arv.busca(4)
    print(teste_busca2.nome)
    teste_busca3 = arv.busca(9)
    print(teste_busca3)

    arv.remover(6)
    arv.remover(1)
    arv.remover(2)
    arv.remover(4)
    arv.remover(24)
    arv.remover(2)
    arv.remover(3)

    print(arv.busca(6))
コード例 #5
0
    def test_balance_tree(self):
        node_1, node_3 = create_unbalanced_tree_with_zig_zag()
        tree = AVL(node_1)
        tree._balance(node_3)

        node_1 = tree.get_root()
        node_0 = node_1.get_left_child()
        node_3 = node_1.get_right_child()
        node_2 = node_3.get_left_child()
        node_4 = node_3.get_right_child()

        expect(node_0.get_key()).to.be.equal(0)
        expect(node_1.get_key()).to.be.equal(1)
        expect(node_2.get_key()).to.be.equal(2)
        expect(node_3.get_key()).to.be.equal(3)
        expect(node_4.get_key()).to.be.equal(4)

        expect(node_0.get_height()).to.be.equal(0)
        expect(node_1.get_height()).to.be.equal(2)
        expect(node_2.get_height()).to.be.equal(0)
        expect(node_3.get_height()).to.be.equal(1)
        expect(node_4.get_height()).to.be.equal(0)

        expect(node_0.get_node_count()).to.be.equal(1)
        expect(node_1.get_node_count()).to.be.equal(5)
        expect(node_2.get_node_count()).to.be.equal(1)
        expect(node_3.get_node_count()).to.be.equal(3)
        expect(node_4.get_node_count()).to.be.equal(1)
コード例 #6
0
    def test_update_heights(self):
        node_5 = Node(5)
        node_0 = Node(0)
        node_12 = Node(12)

        tree = AVL(node_5)
        tree._set_root(node_5)
        node_5.set_left_child(node_0)
        node_5.set_right_child(node_12)
        node_0.set_parent(node_5)
        node_12.set_parent(node_5)

        node_5._set_height(1)
        node_0._set_height(0)
        node_12._set_height(0)

        node_6 = Node(6)
        node_12.set_left_child(node_6)
        node_6.set_parent(node_12)

        tree._update_heights(node_6)

        expect(node_5.get_height()).to.be.equal(2)
        expect(node_0.get_height()).to.be.equal(0)
        expect(node_12.get_height()).to.be.equal(1)
        expect(node_6.get_height()).to.be.equal(0)
コード例 #7
0
 def test_long_tree(self):
     print("Running test_long_tree")
     h = AVL([20, 10, 25, 5, 18, 13, 15])
     self.check_multiline(str(h),
                          [["18"], ["10", "20"], ["5", "13", "_", "25"],
                           ["_", "_", "_", "15", "_", "_"], ['_', '_'],
                           ["5", "10", "13", "15", "18", "20", "25"]])
コード例 #8
0
    def test_rotate_left_subtree_with_no_parent(self):
        A = create_left_rotable_tree()

        rotable_to_left_tree = AVL(A)
        rotable_to_left_tree._left_rotate(rotable_to_left_tree.get_root())

        B = rotable_to_left_tree.get_root()
        a = B.get_right_child()
        A = B.get_left_child()
        b = A.get_right_child()
        c = A.get_left_child()

        expect(B.get_name()).to.be.equal('B')
        expect(A.get_name()).to.be.equal('A')
        expect(c.get_name()).to.be.equal('c')
        expect(a.get_name()).to.be.equal('a')
        expect(b.get_name()).to.be.equal('b')

        expect(B.get_height()).to.be.equal(2)
        expect(A.get_height()).to.be.equal(1)
        expect(a.get_height()).to.be.equal(0)
        expect(b.get_height()).to.be.equal(0)
        expect(c.get_height()).to.be.equal(0)

        expect(B.get_node_count()).to.be.equal(5)
        expect(A.get_node_count()).to.be.equal(3)
        expect(a.get_node_count()).to.be.equal(1)
        expect(b.get_node_count()).to.be.equal(1)
        expect(c.get_node_count()).to.be.equal(1)
コード例 #9
0
 def test_delete_case_1(self):
     print("Running test_delete_case_1")
     h = AVL([50, 30, 70, 20, 40, 60, 80])
     h.delete(20)
     self.check_multiline(str(h),
                          [["50"], ["30", "70"], ["_", "40", "60", "80"],
                           ["_", "_", "_", "_", "_", "_"],
                           ["30", "40", "50", "60", "70", "80"]])
コード例 #10
0
    def test_count(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        expect(tree.count(-3, 15)).to.equal(7)
        expect(tree.count(10, 15)).to.equal(4)
        expect(tree.count(-2, 10)).to.equal(3)
        expect(tree.count(-100, 100)).to.equal(9)
コード例 #11
0
 def test_simple_delete_4(self):  # XX
     print("Running test_simple_delete_4")
     h = AVL([6, 4, 5, 10])
     self.check_multiline(str(h), [["5"], ["4", "6"], ["_", "_", "_", "10"],
                                   ["_", "_"], ["4", "5", "6", "10"]])
     h.delete(4)
     self.check_multiline(
         str(h),
         [["6"], ["5", "10"], ["_", "_", "_", "_"], ["5", "6", "10"]])
コード例 #12
0
ファイル: dictionaryAVL.py プロジェクト: wu2014/Hash-table
 def __init__(self, capacity=None):
     if capacity is None:
         self._capacity = AVLHashDict.DEFAULT_CAPACITY
     else:
         self._capacity = capacity
     self._table = Array(self._capacity)
     for i in xrange(self._capacity):
         self._table[i] = AVL()
     self._size = 0
コード例 #13
0
    def test_list_node_in_range_list(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        nodes_from_minus_3_to_15 = \
            set([node.get_key() for node in tree.list_nodes_in_range(-4, 16)])

        expect(nodes_from_minus_3_to_15).to.equal(
            set([10, -3, 5, 3, 15, 12, 13]))
コード例 #14
0
    def test_insert(self):
        for j in range(100):
            newbst = AVL()
            for i in range(100):
                newbst.insert(randint(1, 10000), 10)

            self.assertEqual(count_bst_nodes(newbst.root), newbst.size)
            self.assertTrue(newbst.root is not None)
            self.assertTrue(has_avl_property(newbst.root))
コード例 #15
0
    def test_lower_inmediate(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        node_5 = tree.search_lower_inmediate(5)
        node_minus_3 = tree.search_lower_inmediate(2)

        expect(node_5.get_key()).to.equal(5)
        expect(node_minus_3.get_key()).to.equal(-3)
コード例 #16
0
    def test_rank(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        expect(tree.rank(15)).to.equal(8)
        expect(tree.rank(20)).to.equal(9)
        expect(tree.rank(12)).to.equal(6)
        expect(tree.rank(-20)).to.equal(1)
        expect(tree.rank(3)).to.equal(3)
コード例 #17
0
 def test_child_parent_link(self):
     print("Running test_child_parent_link")
     h = AVL([21, 17, 11, 15, 6, 5, 8, 1, 9])
     n = h.get_root().get_left_child()
     self.assertEqual(n.get_key(), 6)
     self.assertEqual(n.get_right_child().get_key(), 8)
     self.assertEqual(n.get_parent().get_key(), 11)
     self.assertEqual(n.get_left_child().get_left_child().get_key(), 1)
     self.assertEqual(
         n.get_left_child().get_left_child().get_parent().get_key(), 5)
コード例 #18
0
 def test_traverse_inorder(self):
     # for j in range(100):
     newbst = AVL()
     array = []
     for i in range(100):
         key = randint(1, 100)
         array.append(key)
         newbst.insert(key, key)
     # Remove duplicate entries because duplicate keys are ignored
     arr = sorted(list(set(array)))
     self.assertListEqual(newbst.traverse_inorder(), arr)
コード例 #19
0
    def test_delete_of_root_with_one_children(self):
        tree = AVL()
        tree.insert(0)
        tree.insert(2)

        tree.delete(0)

        node_2 = tree.get_root()
        expect(node_2.get_parent()).to.equal(None)
        expect(node_2.get_left_child()).to.equal(None)
        expect(node_2.get_right_child()).to.equal(None)
コード例 #20
0
 def test_unique_insert(self):
     print("Running test_unique_insert")
     h = AVL()
     a = h.insert(5)
     b = h.insert(5)
     c = h.insert(5)
     self.assertEqual(type(a), type(Node(5)))
     self.assertEqual(a.get_left_child(), None)
     self.assertEqual(a.get_right_child(), None)
     self.assertEqual(a.get_parent(), None)
     self.assertEqual(b, None)
     self.assertEqual(b, None)
コード例 #21
0
ファイル: test_avl.py プロジェクト: shuvava/python_algorithms
 def test_delete_node(self):
     if __print__:
         print('test_delete_node >>>>')
     bst = AVL().from_list(self.bst_data)
     if __print__:
         bst.print_tree()
     bst.delete_node(bst.root.left)
     if __print__:
         bst.print_tree()
     self.assertEqual(bst.length, len(self.bst_data) - 1)
     if __print__:
         print('<<<< test_delete_node')
コード例 #22
0
 def test_height_insert_only_2(self):
     print("Running test_height_insert_only_2")
     h = AVL()
     h.insert(5)
     self.assertEqual(h.get_root().get_height(), 0)
     h.insert(2)
     self.assertEqual(h.get_root().get_height(), 1)
     self.assertEqual(h.get_root().get_left_child().get_height(), 0)
     h.insert(8)
     self.assertEqual(h.get_root().get_height(), 1)
     self.assertEqual(h.get_root().get_left_child().get_height(), 0)
     self.assertEqual(h.get_root().get_right_child().get_height(), 0)
     h.insert(1)
     self.assertEqual(h.get_root().get_height(), 2)
     self.assertEqual(h.get_root().get_left_child().get_height(), 1)
     self.assertEqual(
         h.get_root().get_left_child().get_left_child().get_height(), 0)
     self.assertEqual(h.get_root().get_right_child().get_height(), 0)
     h.insert(3)
     self.assertEqual(h.get_root().get_height(), 2)
     self.assertEqual(h.get_root().get_left_child().get_height(), 1)
     self.assertEqual(
         h.get_root().get_left_child().get_left_child().get_height(), 0)
     self.assertEqual(
         h.get_root().get_left_child().get_right_child().get_height(), 0)
     self.assertEqual(h.get_root().get_right_child().get_height(), 0)
     h.insert(9)
     self.assertEqual(h.get_root().get_height(), 2)
     self.assertEqual(h.get_root().get_left_child().get_height(), 1)
     self.assertEqual(
         h.get_root().get_left_child().get_left_child().get_height(), 0)
     self.assertEqual(
         h.get_root().get_left_child().get_right_child().get_height(), 0)
     self.assertEqual(h.get_root().get_right_child().get_height(), 1)
     self.assertEqual(
         h.get_root().get_right_child().get_right_child().get_height(), 0)
     # Balancing after this point
     h.insert(10)
     self.assertEqual(h.get_root().get_height(), 2)
     self.assertEqual(h.get_root().get_right_child().get_height(), 1)
     self.assertEqual(h.get_root().get_right_child().get_key(), 9)
     self.assertEqual(
         h.get_root().get_right_child().get_right_child().get_key(), 10)
     self.assertEqual(
         h.get_root().get_right_child().get_right_child().get_height(), 0)
     self.assertEqual(
         h.get_root().get_right_child().get_left_child().get_key(), 8)
     self.assertEqual(
         h.get_root().get_right_child().get_left_child().get_height(), 0)
     self.check_multiline(str(h), [["5"], ["2", "9"], ["1", "3", "8", "10"],
                                   ["_", "_", "_", "_", "_", "_", "_", "_"],
                                   ["1", "2", "3", "5", "8", "9", "10"]])
コード例 #23
0
    def test_double_rotation_with_left_zig_zag_tree(self):
        node_12 = create_zig_zag_tree_left_right()
        tree = AVL(node_12)
        returned_node = tree._double_rotation(node_12)

        node_8 = tree.get_root()
        node_6 = node_8.get_left_child()
        node_12 = node_8.get_right_child()

        expect(node_8.get_key()).to.be.equal(8)
        expect(node_6.get_key()).to.be.equal(6)
        expect(node_12.get_key()).to.be.equal(12)
        expect(returned_node).to.be.equal(node_8)
コード例 #24
0
    def test_simple_rotation_with_right_right_tree(self):
        node_10 = create_not_zig_zag_tree_right()
        tree = AVL(node_10)
        returned_tree = tree._simple_rotation(tree.get_root())

        node_11 = tree.get_root()
        node_10 = node_11.get_left_child()
        node_12 = node_11.get_right_child()

        expect(node_10.get_key()).to.be.equal(10)
        expect(node_11.get_key()).to.be.equal(11)
        expect(node_12.get_key()).to.be.equal(12)
        expect(returned_tree).to.be.equal(node_11)
コード例 #25
0
 def test_search(self):
     for j in range(100):
         newbst = AVL()
         for i in range(100):
             key = randint(1, 1000)
             if key != 500 and key != 600 and key != 400:
                 newbst.insert(key, key)
         newbst.insert(400, 500)
         newbst.insert(500, 50)
         newbst.insert(600, 5)
         self.assertEqual(newbst.search(400).data, 500)
         self.assertEqual(newbst.search(500).data, 50)
         self.assertEqual(newbst.search(600).data, 5)
コード例 #26
0
    def test_higher_inmediate(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        node_5 = tree.search_higher_inmediate(5)
        node_3 = tree.search_higher_inmediate(2)
        node_minus_20 = tree.search_higher_inmediate(-30)
        node_minus_3 = tree.search_higher_inmediate(-15)

        expect(node_5.get_key()).to.equal(5)
        expect(node_3.get_key()).to.equal(3)
        expect(node_minus_20.get_key()).to.equal(-20)
        expect(node_minus_3.get_key()).to.equal(-3)
コード例 #27
0
    def test_balance_tree_with_same_weight_on_second_level(self):
        node_5 = create_unbalanced_tree_with_equal_weight_on_second_level()
        tree = AVL(node_5)
        tree._balance(node_5)

        node_4 = tree.get_root()
        node_2 = node_4.get_left_child()
        node_5 = node_4.get_right_child()
        node_3 = node_5.get_left_child()

        expect(node_4.get_key()).to.equal(4)
        expect(node_2.get_key()).to.equal(2)
        expect(node_5.get_key()).to.equal(5)
        expect(node_3.get_key()).to.equal(3)
コード例 #28
0
 def test_general_functions(self):  # XX
     print("Running test_general_functions")
     # Add a number of elements to at least prevent some possible toy-example working out
     h = AVL([50, 70, 80, 60, 55, 67, 90, 57, 20, 10, 15, 0])
     self.assertEqual(h.find_min(), 0)
     self.assertEqual(h.find_max(), 90)
     self.assertFalse(h.is_empty())
     n = h.search(67)
     m = h.search(99)
     self.assertTrue(isinstance(n, Node))
     self.assertEqual(n.get_key(), 67)
     self.assertEqual(m, None)
     self.assertTrue(h.contains(67))
     self.assertFalse(h.contains(100))
コード例 #29
0
 def test_delete(self):
     # sys.setrecursionlimit(1500)
     for j in range(100):
         newbst = AVL()
         target = randint(1, 1000)
         for i in range(100):
             key = randint(1, 1000)
             if key != target:
                 newbst.insert(key, key)
         newbst.insert(target, 1)
         self.assertEqual(newbst.search(target).data, 1)
         newbst.delete(target)
         self.assertEqual(newbst.search(target), None)
         self.assertTrue(has_avl_property(newbst.root))
コード例 #30
0
def generate_avl(data):
    data = json.loads(data)
    data = data["DATA"]
	
    list_dictionary = []
    addStudent(data, list_dictionary)
    new_avl = AVL()

    
    for student in list_dictionary:
    	student = str(student).split("-")
    	new_avl.add(int(student[0]), student[1])
	
    return new_avl