def test_bst(self):
        bst = BinarySearchTree()

        bst.addNode(20, "Computer")
        self.assertEqual(bst.size(), 1)

        bst.addNode(10, "laptop")
        self.assertEqual(bst.size(), 2)

        bst.addNode(15, "PDA")
        self.assertEqual(bst.size(), 3)

        bst.addNode(40, "android")
        self.assertEqual(bst.size(), 4)

        self.assertListEqual(bst.inOrderTraverse(), [10, 15, 20, 40])

        self.assertListEqual(bst.preOrderTraverse(), [20, 10, 15, 40])

        self.assertListEqual(bst.postOrderTraverse(), [10, 15, 40, 20])
Beispiel #2
0
    def test_bestTest(self):
        bst=BinarySearchTree()

        #Test 'add' and 'size'
        bst.add(10,"A value")
        self.assertEqual(bst.size(),1)
        bst.add(5,"A value")
        self.assertEqual(bst.size(),2)
        bst.add(30,"A value")
        self.assertEqual(bst.size(),3)

        #Test 'inorder_walk'
        self.assertListEqual(bst.inorder_walk(),[5,10,30])

        bst.add(15,"value")
        self.assertListEqual(bst.inorder_walk(),[5,10,15,30])

        #Test 'preorder_walk'
        self.assertListEqual(bst.preorder_walk(),[10,5,30,15])

        #Test 'postorder_walk'
        self.assertListEqual(bst.postorder_walk(),[5,15,30,10])

        #test 'smallest'
        self.assertEqual(bst.smallest(),5)

        #test 'largest'
        self.assertEqual(bst.largest(),30)

        #test 'search'
        #the output itself gives the test. since the value is not returned, this test case cannot be applied
        #also delete implements search

        #test 'delete'
        bst.delete(5)
        self.assertListEqual(bst.inorder_walk(),[10,15,30])