Beispiel #1
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])
Beispiel #2
0
from binary_search import BinarySearchTree

bst = BinarySearchTree()

bst.add(10, "A value")
bst.add(20, "B value")
bst.add(5, "A value")
bst.add(12, "D value")
bst.add(30, "E value")
bst.add(2, "G value")
bst.add(35, "F value")

print("The inorder traversal of the tree is")
print(bst.inorder_walk())
print("The preorder traversal of the tree is")
print(bst.preorder_walk())
print("The postorder traversal of the tree is")
print(bst.postorder_walk())

print(bst.smallest(), "is the smallest")
print(bst.largest(), "is the largest")

x = int(input("enter the no. u want to search? "))
bst.search(x)

del_value = int(input("enter the key u want to delte."))
bst.delete(del_value)
print("after deletion the search tree becomes.. ", bst.inorder_walk())