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"]])
 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"]])
Exemple #3
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)
 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))
Exemple #5
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)
Exemple #6
0
    def test_delete_that_requires_a_balance(self):
        node_10 = create_valid_avl_tree()
        tree = AVL(node_10)

        tree.delete(-20)

        node_3 = node_10.get_left_child()
        node_15 = node_10.get_right_child()
        node_minus_3 = node_3.get_left_child()
        node_5 = node_3.get_right_child()

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

        expect(node_10.get_height()).to.equal(3)
        expect(node_minus_3.get_height()).to.equal(0)
        expect(node_3.get_height()).to.equal(1)
        expect(node_5.get_height()).to.equal(0)
        expect(node_15.get_height()).to.equal(2)
 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)
Exemple #8
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)
Exemple #9
0
def test():
    arr = [randrange(11111) for i in range(11130)]
    # arr = [i for i in range(20)]
    # arr = [3, 1, 2, 4]
    avl = AVL(arr)
    # avl.delete(2)
    l = avl.traversal()
    print('init', str(l))
    for n in arr:
        # print('delete ', n)
        avl.delete(n)
        # break
    print('deleted', avl.traversal())
    #
    dot = Digraph(comment='bst')
    # 默认是 pdf
    dot.format = 'png'
    #
    n = avl.root
    v = n.value
    name = '{}'.format(v)
    dot.node(name, name)
    draw_tree(n, dot)
Exemple #10
0
    def test_delete_of_a_single_root_node(self):
        tree = AVL(Node(10))
        tree.delete(10)

        expect(tree.get_root()).to.equal(None)
Exemple #11
0
from avl import AVL
if __name__ == '__main__':
    t = AVL()
    t.put(75, 'apple')
    t.put(80, 'grape')
    t.put(85, 'lime')
    t.put(20, 'mango')
    t.put(10, 'strawberry')
    t.put(50, 'banana')
    t.put(30, 'cherry')
    t.put(40, 'watermelon')
    t.put(70, 'melon')
    t.put(90, 'plum')
    print('전위순회:\t', end='')
    t.preorder(t.root)
    print('\n중위순회:\t', end='')
    t.inorder(t.root)
    print('\n75와 85 삭제 후:')
    t.delete(75)
    t.delete(85)
    print('전위순회:\t', end='')
    t.preorder(t.root)
    print('\n중위순회:\t', end='')
    t.inorder(t.root)
Exemple #12
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)
 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"]])