示例#1
0
文件: test.py 项目: manesajian/syswiz
class TestBinarySearchTree(unittest.TestCase):
    def setUp(self):
        self.bst = None

    def _initialize(self):
        self._cleanup()
        self.bst = BinarySearchTree()
        lst = [5, 8, 2, 1, 3, 9, 7, 4, 6, 0]
        for i in lst:
            self.bst.insert(i)

    def _cleanup(self):
        if self.bst:
            del(self.bst)
            self.bst = None

    def test_insert(self):
        self._initialize()
        self.assertEqual(len(self.bst), 10)

    def test_delete(self):
        self._initialize()
        self.bst.delete(5)
        self.assertEqual(len(self.bst), 9)

    def test_iteration(self):
        self._initialize()
        lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        for i in self.bst.root:
            self.assertTrue(i == lst[i])

    def tearDown(self):
        self._cleanup()
示例#2
0
 def test_insert_and_delete_tree(self):
     bst = BinarySearchTree(10)
     bst.insert(20)
     self.assertEqual(bst.size(), 2)
     bst = bst.delete(20)
     self.assertEqual(bst.size(), 1)
     bst = bst.delete(10)
     self.assertEqual(bst.size(), 0)
 def test_delete(self):
     # Create a complete binary search tree of 7 items in level-order
     items = [4, 2, 6, 1, 3, 5, 7]
     bst = BinarySearchTree()
     for item in items:
         bst.insert(item)
     # Delete the specific nodes
     bst.delete(7)
     assert bst.contains(7) is False
     bst.delete(2)
     assert bst.contains(2) is False
     assert bst.root.left.data == 1
     assert bst.root.left.left.data is None
示例#4
0
 def test_delete_root_node(self):
     bst = BinarySearchTree(10)
     bst.insert(20)
     bst.insert(30)
     bst.insert(5)
     bst.insert(9)
     self.assertEqual(bst.size(), 5)
     bst = bst.delete(10)
     self.assertEqual(bst.size(), 4)
 def test_delete_method(self):
     bst = BinarySearchTree(Node(50))
     bst.insert(bst, Node(40))
     bst.insert(bst, Node(70))
     bst.insert(bst, Node(60))
     bst.insert(bst, Node(80))
     bst.delete(bst, 50)
     self.assertEqual(bst.val, 60)
     bst.delete(bst, 40)
     self.assertIsNone(bst.left)
     bst.delete(bst, 80)
     self.assertIsNone(bst.right.right)
示例#6
0
    def test_delete(self):
        items = [4, 2, 6, 1, 3, 5, 7]
        bst = BinarySearchTree(items)
        bst.delete(1)
        assert bst.size == 6
        assert bst.root.left.left == None
        bst.delete(2)
        assert bst.size == 5
        assert bst.root.left.data == 3

        bst2 = BinarySearchTree(items)
        bst2.delete(4)
        assert bst2.size == 6
        assert bst2.root.data == 3
        assert bst2.root.left.right == None
示例#7
0
 def test_delete_tree_size_1(self):
     bst = BinarySearchTree(10)
     self.assertEqual(bst.size(), 1)
     bst.delete(10)
     self.assertEqual(bst.size(), 0)