Exemplo n.º 1
0
 def test_left_rotate(self):
     """
     左旋测试
     :return:
     """
     self.assertGreaterEqual(abs(avl.balance_factor(rr_tree.root)), 2,
                             'rr_tree is not a NON balance tree!')
     self.assertLess(abs(avl.balance_factor(avl.left_rotate(rr_tree.root))),
                     2, 'rr_tree is NOT balance after right_rotate!')
Exemplo n.º 2
0
 def test_right_left_rotate(self):
     """
     右左旋测试
     :return:
     """
     self.assertGreaterEqual(abs(avl.balance_factor(lr_tree.root)), 2,
                             'lr_tree is not a NON balance tree!')
     self.assertLess(
         abs(avl.balance_factor(avl.right_left_rotate(lr_tree.root))), 2,
         'rl_tree is not a NON balance tree!')
Exemplo n.º 3
0
 def test_left_rotate(self):
     """
     左旋测试
     :return:
     """
     rr_tree = rbt.RBTree(root=bst.BSTree(keys=[7, 4, 10, 8, 15, 12]).root)
     self.assertGreaterEqual(abs(avl.balance_factor(rr_tree.root)), 2,
                             'The rr tree is NOT a NON balance tree!')
     rr_tree.left_rotate(rr_tree.root)
     self.assertLess(avl.balance_factor(rr_tree.root), 2,
                     'The rr tree do NOT balance after right rotate!')
Exemplo n.º 4
0
 def test_right_rotate(self):
     """
     右旋测试
     :return:
     """
     ll_tree = rbt.RBTree(root=bst.BSTree(keys=[7, 4, 3, 5, 1, 8]).root)
     self.assertGreaterEqual(abs(avl.balance_factor(ll_tree.root)), 2,
                             'The ll tree is NOT a NON balance tree!')
     ll_tree.right_rotate(ll_tree.root)
     self.assertLess(avl.balance_factor(ll_tree.root), 2,
                     'The ll tree do NOT balance after left rotate!')
Exemplo n.º 5
0
 def test_rebalance(self):
     """
     再平衡测试
     :return:
     """
     bst_tree = bst.BSTree(keys=[7, 4, 12, 10, 9, 14])
     t = avl.AVLTree(root=bst_tree.root)
     self.assertGreaterEqual(abs(avl.balance_factor(t.root)), 2,
                             'The tree has already balance!')
     t.rebalance()
     self.assertLess(abs(avl.balance_factor(t.root)), 2,
                     'The tree do NOT balance after rebalance!')
Exemplo n.º 6
0
 def test_balance_factor(self):
     """
     平衡因子测试
     :return:
     """
     factor = avl.balance_factor(demo_tree.root)
     self.assertEqual(1, factor, 'balance factor wrong!')
Exemplo n.º 7
0
 def test_delete_node(self):
     """
     删除结点
     :return:
     """
     bst_tree = bst.BSTree(keys=[7, 4, 12, 10, 9, 14])
     t = avl.AVLTree(root=bst_tree.root)
     t.delete_node(t.root.left)
     self.assertLess(abs(avl.balance_factor(t.root)), 2,
                     'The tree do NOT balance after delete node!')
Exemplo n.º 8
0
 def test_insert_node(self):
     """
     插入新节点
     :return:
     """
     bst_tree = bst.BSTree(keys=[7, 4, 12, 10, 9, 14])
     t = avl.AVLTree(root=bst_tree.root)
     t.insert_node(Node(key=8))
     self.assertLess(abs(avl.balance_factor(t.root)), 2,
                     'The tree do NOT balance after insert node!')