Exemplo n.º 1
0
 def test_val_height_valid_on_remove(self):
     from ch06.balanced import BinaryTree
     bt1 = BinaryTree()
     bt1.insert(7)
     self.assertEqual(7, bt1.min())
     bt1.insert(4)
     bt1.insert(10)
     bt1.insert(8)
     self.assertEqual(2, bt1.root.height)
     self.assertEqual(4, bt1.root.size())
     check_avl_property(bt1.root)
     bt1.remove(7)
     self.assertEqual(3, bt1.root.size())
     self.assertEqual(1, bt1.root.height)
     check_avl_property(bt1.root)
     self.assertEqual(4, bt1.min())
     self.assertTrue(4 in bt1)
     self.assertTrue(10 in bt1)
     self.assertTrue(8 in bt1)
     self.assertFalse(7 in bt1)
Exemplo n.º 2
0
    def test_count_rotations_avl(self):
        from ch06.balanced import BinaryTree

        bt1 = BinaryTree()
        self.assertTrue(bt1.min() is None)
        for i in [50, 30, 70, 20, 40, 60, 10, 45]:
            bt1.insert(i)
        self.assertEqual(50, bt1.root.value)
        self.assertEqual(30, bt1.root.left.value)
        self.assertEqual(20, bt1.root.left.left.value)
        bt1.insert(5)
        self.assertEqual(10, bt1.root.left.left.value)  # rotate
Exemplo n.º 3
0
    def test_tree(self):
        from ch06.tree import BinaryTree
        bt1 = BinaryTree()
        for n in [19, 14, 53, 3, 15, 26, 58]:
            bt1.insert(n)

        last = -1
        while not bt1.is_empty():
            m = bt1.min()
            self.assertTrue(m > last)
            last = m
            bt1.remove(m)
Exemplo n.º 4
0
    def test_bt(self):
        from ch06.tree import BinaryTree

        bt1 = BinaryTree()
        self.assertTrue(bt1.remove(7) is None)  # can work even when empty
        self.assertTrue(bt1.min() is None)

        bt1.insert(5)
        self.assertTrue(5 in bt1)

        bt1.insert(2)
        self.assertEqual(5, bt1.root.value)
        self.assertTrue(2 in bt1)
        self.assertEqual([2, 5], list(bt1))

        bt1.insert(1)
        self.assertTrue(1 in bt1)
        self.assertEqual([1, 2, 5], list(bt1))