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"]])
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def test_bubble_down(self):
        tree = AVL(Node(5))
        tree._bubble_down_to_place(tree.get_root(), Node(12))
        tree._bubble_down_to_place(tree.get_root(), Node(0))
        tree._bubble_down_to_place(tree.get_root(), Node(6))
        tree._bubble_down_to_place(tree.get_root(), Node(8))

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

        expect(node_5.get_key()).to.be.equal(5)
        expect(node_12.get_key()).to.be.equal(12)
        expect(node_0.get_key()).to.be.equal(0)
        expect(node_6.get_key()).to.be.equal(6)
        expect(node_8.get_key()).to.be.equal(8)
 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)
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
    def test_rotate_right_subtree_is_right_child_from_root(self):
        A = create_right_rotable_tree()

        root = Node(10, name='root')
        root.set_right_child(A)
        A.set_parent(root)
        root._set_height(3)

        rotable_to_right_tree = AVL(root)
        rotable_to_right_tree._right_rotate(
            rotable_to_right_tree.get_root().get_right_child())

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

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

        expect(root.get_height()).to.be.equal(3)
        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(root.get_node_count()).to.be.equal(6)
        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)
Exemplo n.º 10
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)
 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"]])
Exemplo n.º 12
0
    def test_insert(self):
        tree = AVL()
        tree.insert(10)
        tree.insert(20)
        tree.insert(5)
        tree.insert(-3)
        tree.insert(-20)
        tree.insert(15)
        tree.insert(12)
        tree.insert(13)

        node_10 = tree.get_root()
        node_minus_3 = node_10.get_left_child()
        node_minus_20 = node_minus_3.get_left_child()
        node_5 = node_minus_3.get_right_child()
        node_15 = node_10.get_right_child()
        node_20 = node_15.get_right_child()
        node_12 = node_15.get_left_child()
        node_13 = node_12.get_right_child()

        expect(node_10.get_key()).to.be.equal(10)
        expect(node_minus_3.get_key()).to.be.equal(-3)
        expect(node_minus_20.get_key()).to.be.equal(-20)
        expect(node_5.get_key()).to.be.equal(5)
        expect(node_15.get_key()).to.be.equal(15)
        expect(node_20.get_key()).to.be.equal(20)
        expect(node_12.get_key()).to.be.equal(12)
        expect(node_13.get_key()).to.be.equal(13)

        expect(node_10.get_height()).to.be.equal(3)
        expect(node_minus_3.get_height()).to.be.equal(1)
        expect(node_minus_20.get_height()).to.be.equal(0)
        expect(node_5.get_height()).to.be.equal(0)
        expect(node_15.get_height()).to.be.equal(2)
        expect(node_20.get_height()).to.be.equal(0)
        expect(node_12.get_height()).to.be.equal(1)
        expect(node_13.get_height()).to.be.equal(0)

        expect(node_10.get_node_count()).to.be.equal(8)
        expect(node_minus_3.get_node_count()).to.be.equal(3)
        expect(node_minus_20.get_node_count()).to.be.equal(1)
        expect(node_5.get_node_count()).to.be.equal(1)
        expect(node_15.get_node_count()).to.be.equal(4)
        expect(node_20.get_node_count()).to.be.equal(1)
        expect(node_12.get_node_count()).to.be.equal(2)
        expect(node_13.get_node_count()).to.be.equal(1)
Exemplo n.º 13
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)
 def test_delete_connections(self):
     print("Running test_delete_connections")
     h = AVL([20, 25, 10, 18, 13, 5, 15])
     self.check_multiline(str(h),
                          [["13"], ["10", "20"], ["5", "_", "18", "25"],
                           ["_", "_", "15", "_", "_", "_"], ['_', '_'],
                           ["5", "10", "13", "15", "18", "20", "25"]])
     h.delete(5)
     node_18 = h.get_root()
     node_13 = node_18.get_left_child()
     node_20 = node_18.get_right_child()
     node_10 = node_13.get_left_child()
     node_15 = node_13.get_right_child()
     node_25 = node_20.get_right_child()
     self.assertEqual(node_18.get_key(), 18)
     self.assertEqual(node_18.get_parent(), None)
     self.assertEqual(node_18.get_left_child().get_key(), 13)
     self.assertEqual(node_18.get_right_child().get_key(), 20)
     self.assertEqual(node_13.get_key(), 13)
     self.assertEqual(node_13.get_parent().get_key(), 18)
     self.assertEqual(node_13.get_left_child().get_key(), 10)
     self.assertEqual(node_13.get_right_child().get_key(), 15)
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent().get_key(), 18)
     self.assertEqual(node_20.get_left_child(), None)
     self.assertEqual(node_20.get_right_child().get_key(), 25)
     self.assertEqual(node_10.get_key(), 10)
     self.assertEqual(node_10.get_parent().get_key(), 13)
     self.assertEqual(node_10.get_left_child(), None)
     self.assertEqual(node_10.get_right_child(), None)
     self.assertEqual(node_15.get_key(), 15)
     self.assertEqual(node_15.get_parent().get_key(), 13)
     self.assertEqual(node_15.get_left_child(), None)
     self.assertEqual(node_15.get_right_child(), None)
     self.assertEqual(node_25.get_key(), 25)
     self.assertEqual(node_25.get_parent().get_key(), 20)
     self.assertEqual(node_25.get_left_child(), None)
     self.assertEqual(node_25.get_right_child(), None)
Exemplo n.º 15
0
    def test_delete_of_node_with_two_children(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        tree.delete(10)

        node_12 = tree.get_root()
        node_15 = node_12.get_right_child()
        node_13 = node_15.get_left_child()
        node_20 = node_15.get_right_child()
        node_minus_3 = node_12.get_left_child()

        expect(node_12.get_key()).to.equal(12)
        expect(node_15.get_key()).to.equal(15)
        expect(node_13.get_key()).to.equal(13)
        expect(node_20.get_key()).to.equal(20)
        expect(node_minus_3.get_key()).to.equal(-3)

        expect(node_12.get_height()).to.equal(3)
        expect(node_minus_3.get_height()).to.equal(2)
        expect(node_15.get_height()).to.equal(1)
        expect(node_13.get_height()).to.equal(0)
        expect(node_20.get_height()).to.equal(0)
Exemplo n.º 16
0
 def test_find_smallest(self):
     node_1 = create_balanced_tree()
     balanced_tree = AVL(node_1)
     expect(balanced_tree.find_smallest()).to.be.equal(
         balanced_tree.get_root().get_left_child())
 def test_height_delete(self):
     print("Running test_height_delete")
     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"]])
     h.delete(9)
     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(), 10)
     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", "10"], ["1", "3", "8", "_"],
          ["_", "_", "_", "_", "_", "_"], ["1", "2", "3", "5", "8", "10"]])
Exemplo n.º 18
0
 def test_has_unbalanced_children_with_balanced_tree(self):
     root = create_balanced_tree()
     tree = AVL(root)
     expect(tree._has_unbalanced_children(tree.get_root())).to.be(False)
Exemplo n.º 19
0
 def test_is_heavy_path_zig_zag_left_right_tree(self):
     node_12 = create_zig_zag_tree_left_right()
     tree = AVL(node_12)
     expect(tree._is_heavy_path_zig_zag(tree.get_root())).to.be(True)
Exemplo n.º 20
0
 def test_is_heavy_path_zig_zag_right_right_tree(self):
     node_10 = create_not_zig_zag_tree_right()
     tree = AVL(node_10)
     expect(tree._is_heavy_path_zig_zag(tree.get_root())).to.be(False)
 def test_insert_connections(self):
     print("Running test_delete_case_3")
     h = AVL()
     # Start checking all connections
     node_20 = h.insert(20)
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent(), None)
     self.assertEqual(node_20.get_left_child(), None)
     self.assertEqual(node_20.get_right_child(), None)
     node_25 = h.insert(25)
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent(), None)
     self.assertEqual(node_20.get_left_child(), None)
     self.assertEqual(node_20.get_right_child().get_key(), 25)
     self.assertEqual(node_25.get_key(), 25)
     self.assertEqual(node_25.get_parent().get_key(), 20)
     self.assertEqual(node_25.get_left_child(), None)
     self.assertEqual(node_25.get_right_child(), None)
     node_10 = h.insert(10)
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent(), None)
     self.assertEqual(node_20.get_left_child().get_key(), 10)
     self.assertEqual(node_20.get_right_child().get_key(), 25)
     self.assertEqual(node_25.get_key(), 25)
     self.assertEqual(node_25.get_parent().get_key(), 20)
     self.assertEqual(node_25.get_left_child(), None)
     self.assertEqual(node_25.get_right_child(), None)
     self.assertEqual(node_10.get_key(), 10)
     self.assertEqual(node_10.get_parent().get_key(), 20)
     self.assertEqual(node_10.get_left_child(), None)
     self.assertEqual(node_10.get_right_child(), None)
     node_18 = h.insert(18)
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent(), None)
     self.assertEqual(node_20.get_left_child().get_key(), 10)
     self.assertEqual(node_20.get_right_child().get_key(), 25)
     self.assertEqual(node_25.get_key(), 25)
     self.assertEqual(node_25.get_parent().get_key(), 20)
     self.assertEqual(node_25.get_left_child(), None)
     self.assertEqual(node_25.get_right_child(), None)
     self.assertEqual(node_10.get_key(), 10)
     self.assertEqual(node_10.get_parent().get_key(), 20)
     self.assertEqual(node_10.get_left_child(), None)
     self.assertEqual(node_10.get_right_child().get_key(), 18)
     self.assertEqual(node_18.get_key(), 18)
     self.assertEqual(node_18.get_parent().get_key(), 10)
     self.assertEqual(node_18.get_left_child(), None)
     self.assertEqual(node_18.get_right_child(), None)
     node_13 = h.insert(13)
     # Incase students somehow swap values instead of connections and we do have a rotation here:
     node_20 = h.get_root()
     node_25 = h.get_root().get_right_child()
     node_10 = h.get_root().get_left_child().get_left_child()
     node_18 = h.get_root().get_left_child().get_right_child()
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent(), None)
     self.assertEqual(node_20.get_left_child().get_key(), 13)
     self.assertEqual(node_20.get_right_child().get_key(), 25)
     self.assertEqual(node_25.get_key(), 25)
     self.assertEqual(node_25.get_parent().get_key(), 20)
     self.assertEqual(node_25.get_left_child(), None)
     self.assertEqual(node_25.get_right_child(), None)
     self.assertEqual(node_10.get_key(), 10)
     self.assertEqual(node_10.get_parent().get_key(), 13)
     self.assertEqual(node_10.get_left_child(), None)
     self.assertEqual(node_10.get_right_child(), None)
     self.assertEqual(node_18.get_key(), 18)
     self.assertEqual(node_18.get_parent().get_key(), 13)
     self.assertEqual(node_18.get_left_child(), None)
     self.assertEqual(node_18.get_right_child(), None)
     self.assertEqual(node_13.get_key(), 13)
     self.assertEqual(node_13.get_parent().get_key(), 20)
     self.assertEqual(node_13.get_left_child().get_key(), 10)
     self.assertEqual(node_13.get_right_child().get_key(), 18)
     node_5 = h.insert(5)
     # Incase students somehow swap values instead of connections and we do have a rotation here:
     node_13 = h.get_root()
     node_20 = h.get_root().get_right_child()
     node_25 = h.get_root().get_right_child().get_right_child()
     node_10 = h.get_root().get_left_child()
     node_18 = h.get_root().get_right_child().get_left_child()
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent().get_key(), 13)
     self.assertEqual(node_20.get_left_child().get_key(), 18)
     self.assertEqual(node_20.get_right_child().get_key(), 25)
     self.assertEqual(node_25.get_key(), 25)
     self.assertEqual(node_25.get_parent().get_key(), 20)
     self.assertEqual(node_25.get_left_child(), None)
     self.assertEqual(node_25.get_right_child(), None)
     self.assertEqual(node_10.get_key(), 10)
     self.assertEqual(node_10.get_parent().get_key(), 13)
     self.assertEqual(node_10.get_left_child().get_key(), 5)
     self.assertEqual(node_10.get_right_child(), None)
     self.assertEqual(node_18.get_key(), 18)
     self.assertEqual(node_18.get_parent().get_key(), 20)
     self.assertEqual(node_18.get_left_child(), None)
     self.assertEqual(node_18.get_right_child(), None)
     self.assertEqual(node_13.get_key(), 13)
     self.assertEqual(node_13.get_parent(), None)
     self.assertEqual(node_13.get_left_child().get_key(), 10)
     self.assertEqual(node_13.get_right_child().get_key(), 20)
     self.assertEqual(node_5.get_key(), 5)
     self.assertEqual(node_5.get_parent().get_key(), 10)
     self.assertEqual(node_5.get_left_child(), None)
     self.assertEqual(node_5.get_right_child(), None)
     node_15 = h.insert(15)
     self.assertEqual(node_20.get_key(), 20)
     self.assertEqual(node_20.get_parent().get_key(), 13)
     self.assertEqual(node_20.get_left_child().get_key(), 18)
     self.assertEqual(node_20.get_right_child().get_key(), 25)
     self.assertEqual(node_25.get_key(), 25)
     self.assertEqual(node_25.get_parent().get_key(), 20)
     self.assertEqual(node_25.get_left_child(), None)
     self.assertEqual(node_25.get_right_child(), None)
     self.assertEqual(node_10.get_key(), 10)
     self.assertEqual(node_10.get_parent().get_key(), 13)
     self.assertEqual(node_10.get_left_child().get_key(), 5)
     self.assertEqual(node_10.get_right_child(), None)
     self.assertEqual(node_18.get_key(), 18)
     self.assertEqual(node_18.get_parent().get_key(), 20)
     self.assertEqual(node_18.get_left_child().get_key(), 15)
     self.assertEqual(node_18.get_right_child(), None)
     self.assertEqual(node_13.get_key(), 13)
     self.assertEqual(node_13.get_parent(), None)
     self.assertEqual(node_13.get_left_child().get_key(), 10)
     self.assertEqual(node_13.get_right_child().get_key(), 20)
     self.assertEqual(node_5.get_key(), 5)
     self.assertEqual(node_5.get_parent().get_key(), 10)
     self.assertEqual(node_5.get_left_child(), None)
     self.assertEqual(node_5.get_right_child(), None)
     self.assertEqual(node_15.get_key(), 15)
     self.assertEqual(node_15.get_parent().get_key(), 18)
     self.assertEqual(node_15.get_left_child(), None)
     self.assertEqual(node_15.get_right_child(), None)
     self.check_multiline(str(h),
                          [["13"], ["10", "20"], ["5", "_", "18", "25"],
                           ["_", "_", "15", "_", "_", "_"], ['_', '_'],
                           ["5", "10", "13", "15", "18", "20", "25"]])
Exemplo n.º 22
0
    def test_delete_of_a_single_root_node(self):
        tree = AVL(Node(10))
        tree.delete(10)

        expect(tree.get_root()).to.equal(None)
Exemplo n.º 23
0
 def test_search_with_valid_key(self):
     node_1 = create_balanced_tree()
     balanced_tree = AVL(node_1)
     expect(balanced_tree.search(2)).to.be.equal(
         balanced_tree.get_root().get_right_child())
 def test_height_insert_only_1(self):
     print("Running test_height_insert_only_1")
     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)
     self.check_multiline(
         str(h),
         [["5"], ["2", "8"], ["1", "3", "_", "9"],
          ["_", "_", "_", "_", "_", "_"], ["1", "2", "3", "5", "8", "9"]])
Exemplo n.º 25
0
 def test_succesor_when_succesor_is_in_right_subtree(self):
     node_90 = create_tree_for_testing_successor()
     tree = AVL(node_90)
     expect(tree.get_successor(tree.search(50))).to.be.equal(tree.get_root(
     ).get_left_child().get_right_child().get_left_child().get_left_child())
Exemplo n.º 26
0
 def test_predecessor_when_predecessor_is_parent(self):
     node_90 = create_tree_for_testing_predecessor()
     tree = AVL(node_90)
     expect(tree.get_predecessor(tree.search(160))).to.be.equal(
         tree.get_root().get_right_child())