Beispiel #1
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)
Beispiel #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))
 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"]])
Beispiel #4
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)
    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))
Beispiel #6
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]))
Beispiel #7
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)
 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"]])
 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)
 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)
 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)
Beispiel #12
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)
Beispiel #13
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)
Beispiel #14
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)
 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))
Beispiel #16
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)
Beispiel #17
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
    
    def setUp(self):
        A = BSTNode(1, "A")
        B = BSTNode(3, "B")
        C = BSTNode(2, "C")

        B.left = C
        A.right = B

        #    A
        #     \
        #      B
        #     /
        #    C

        self.tree = AVL()
        self.nodes = (A, B, C)
 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"]])
 def test_insert_3_nodes_3(self):
     print("Running test_insert_3_nodes_ordered")
     h = AVL([5, 10, 6])
     root = h.get_root()
     self.assertEqual(root.get_key(), 6)
     self.assertEqual(root.get_left_child().get_key(), 5)
     self.assertEqual(root.get_left_child().get_parent().get_key(), 6)
     self.assertEqual(root.get_left_child().get_left_child(), None)
     self.assertEqual(root.get_left_child().get_right_child(), None)
     self.assertEqual(root.get_right_child().get_key(), 10)
     self.assertEqual(root.get_right_child().get_parent().get_key(), 6)
     self.assertEqual(root.get_right_child().get_left_child(), None)
     self.assertEqual(root.get_right_child().get_right_child(), None)
     self.check_multiline(
         str(h),
         [["6"], ["5", "10"], ["_", "_", "_", "_"], ["5", "6", "10"]])
Beispiel #21
0
 def setUp(self):
     self.avl = AVL()
     # insert random permutation of [1, ..., 1000) list to avl tree
     ls = list(range(1, 1000))
     random.shuffle(ls)
     for v, k in enumerate(ls):
         self.avl.insert(k, v + 1)
 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"]])
Beispiel #23
0
 def setUp(self):
     self.avl = AVL()
     self.avl.insert(277, 19)  # some random values choosen by me
     self.avl.insert(31, 14)  # order of inserted elements
     self.avl.insert(91, 99)  # forces rotation of nodes
     self.avl.insert(99, 22)
     self.avl.insert(111, 78)
     self.avl.insert(121, 91)
Beispiel #24
0
class RunningMedian():
    def __init__(self):
        self.tree = AVL()
        self.count = 0

    def add(self, val):
        self.count += 1
        self.tree.insert(val)
        if self.count % 2 == 1:
            return self.tree.root.key
        else:
            return (self.get_other_for_median() + self.tree.root.key) / 2

            if not self.tree.root.right or (self.tree.root.left
                                            and self.tree.root.right.height <
                                            self.tree.root.left.height):
                return (self.tree.root.key + self.tree.root.left.key) / 2
            else:
                return (self.tree.root.key + self.tree.root.right.key) / 2

    def get_other_for_median(self):
        highest_left = self.get_highest_left(self.tree.root.left, 0)
        lowest_right = self.get_lowest_right(self.tree.root.right, 0)

        if not highest_left or (lowest_right
                                and lowest_right[1] > highest_left[1]):
            return lowest_right[0]
        else:
            return highest_left[0]

    def get_highest_left(self, node, height):
        if not node and not height:
            return None
        if not node.right:
            return (node.key, height)
        else:
            return self.get_highest_left(node.right, height + 1)

    def get_lowest_right(self, node, height):
        if not node and not height:
            return None
        if not node.left:
            return (node.key, height)
        else:
            return self.get_lowest_right(node.left, height + 1)
Beispiel #25
0
 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
Beispiel #26
0
 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')
Beispiel #27
0
    def test_delete_of_node_with_no_children(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        tree.delete(3)

        node_10 = tree.get_root()
        node_minus_3 = node_10.get_left_child()
        node_5 = node_minus_3.get_right_child()
        node_3 = node_5.get_left_child()

        expect(node_10.get_key()).to.equal(10)
        expect(node_minus_3.get_key()).to.equal(-3)
        expect(node_5.get_key()).to.equal(5)
        expect(node_3).to.equal(None)

        expect(node_5.get_height()).to.equal(0)
        expect(node_minus_3.get_height()).to.equal(1)
        expect(node_10.get_height()).to.equal(3)
Beispiel #28
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)
Beispiel #29
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)
 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"]])
 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))
Beispiel #32
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)
Beispiel #33
0
class AVLTester(unittest.TestCase):
    """First set of tests for avl"""
    def setUp(self):
        self.avl = AVL()
        # insert random permutation of [1, ..., 1000) list to avl tree
        ls = list(range(1, 1000))
        random.shuffle(ls)
        for v, k in enumerate(ls):
            self.avl.insert(k, v + 1)

    def test_is_all(self):
        """This function tests if all values are present in test tree"""
        test_set = set(range(1, 1000))
        # checks whether get_value_by_key works fine
        all_values = {self.avl.get_value_by_key(x) for x in range(1, 1000)}
        self.assertSetEqual(all_values, test_set, "get_value_by_key fails")
        # checks whether get_value_by_position works fine
        all_values = {self.avl.get_value_by_position(x) for x in range(1, 1000)}
        self.assertSetEqual(all_values, test_set, "get_value_by_position fails")
        # checks whether iteration over tree works fine
        all_values = {value for key, value in self.avl}
        self.assertSetEqual(all_values, test_set, "tree iterator fails")

    def test_all_values_removal(self):
        """Checks if tree's empty after removing all values"""
        for k in range(1, 1000):
            self.avl.remove_by_key(k)
        self.assertTrue(self.avl.is_empty())

    def test_count(self):
        """Check's whether counting tree elements works fine"""
        self.assertEqual(self.avl.count(), 999)
        self.assertEqual(self.avl.count_subset(24, 124), 100)
        self.assertEqual(self.avl.count_subset(-10, 10), 9)
        self.assertEqual(self.avl.count_subset(999, 1001), 1)
        self.assertEqual(self.avl.count_subset(997, 1001), 3)
        self.assertEqual(self.avl.count_subset(1001, 1002), 0)

    def test_special_cases(self):
        """'Edge' cases"""
        # we want value of key not present in tree
        self.assertIsNone(self.avl.get_value_by_key(-7))
        self.assertIsNone(self.avl.get_value_by_key(6666))
        self.assertIsNone(self.avl.get_value_by_position(6666))
        self.assertIsNone(self.avl.get_value_by_position(-21))
        self.assertIsNone(self.avl.get_value_by_closest_match(1001))
        self.assertEqual(self.avl.get_value_by_closest_match(-1), self.avl.get_value_by_key(1))
        self.assertIsNone(self.avl.get_value_by_closest_match(1001))
        self.assertIsNone(self.avl.get_closest_element_position(10001))
        # we try to remove nodes with key not present in tree
        self.avl.remove_by_key(-23)
        self.avl.remove_by_key(1001)
        self.avl.remove_by_iterator(self.avl.get_iterator_by_key(10001))
        self.assertEqual(self.avl.count(), 999)
Beispiel #34
0
class AVLTester2(unittest.TestCase):
    """Second set of tests for avl"""
    def setUp(self):
        self.avl = AVL()
        self.avl.insert(277, 19)  # some random values choosen by me
        self.avl.insert(31, 14)  # order of inserted elements
        self.avl.insert(91, 99)  # forces rotation of nodes
        self.avl.insert(99, 22)
        self.avl.insert(111, 78)
        self.avl.insert(121, 91)

    def test_modifications(self):
        """Checks whether modification is ok"""
        self.avl.modify_by_key(111, 19)
        self.avl.modify_by_key(200, 21)
        self.assertEqual(self.avl.get_value_by_key(111), 19)

    def test_removal(self):
        """Checks whether removal works fine"""
        self.avl.remove_by_key(31)
        self.avl.remove_by_key(99)
        self.assertListEqual([v for k, v in self.avl], [99, 78, 91, 19])
        self.assertIsNone(self.avl.get_value_by_key(31))
        self.assertEqual(self.avl.get_value_by_closest_match(31), 99)

    def test_some_things(self):
        """Test some random things"""
        self.assertListEqual([78, 91, 19], [value for key, value in self.avl.get_iterator_by_key(111)])
        self.assertListEqual([78, 91, 19], [value for key, value in self.avl.get_iterator_by_position(4)])
        self.assertEqual(self.avl.get_value_by_closest_match(91), 99)
        self.assertEqual(self.avl.get_value_by_closest_match(71), 99)
        self.assertEqual(self.avl.get_value_by_closest_match(95), 22)
        self.assertEqual(self.avl.get_closest_element_position(21), 1)
        self.assertEqual(self.avl.get_closest_element_position(98), 3)
        self.avl.clear()
        self.assertIsNone(self.avl.get_value_by_closest_match(111))
Beispiel #35
0
from avl import AVL 

l = []

tree = AVL()
tree.insert(100)
tree.insert(90)
tree.insert(80)
tree.insert(6)
tree.delete(100)
tree.inorder(l, tree.root)
print(l)