Ejemplo n.º 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])
Ejemplo n.º 2
0
#https://www.cs.usfca.edu/~galles/visualization/BST.html

from binary_search import BinarySearchTree

tree = BinarySearchTree()

tree.add(8)
tree.add(10)
tree.add(3)
tree.add(14)
tree.add(13)
tree.add(1)
tree.add(6)
tree.add(4)
tree.add(7)

print('Order')
tree.show_in_order(tree.root)
print('Pos-order')
tree.show_pos_order(tree.root)
print('pre -order')
tree.show_pre_order(tree.root)

#print(tree.search(tree.root, 10).value)
#print(tree.search(tree.root, 10).is_left)
#print(tree.search(tree.root, 10).parent.value)
"""
En orden
izquierda - raiz - derecha
# 1, 3, 4, 6, 7, 8, 10, 13, 14 
"""
Ejemplo n.º 3
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())