You are on page 1of 7

EX.

NO: 8 Implementation of Hash Tables

Aim: To write a python program to implement hash table

Algorithm:

Step 1: Start the program


Step 2: Create a structure, data (hash table item) with key and value as data.
Step 3: Now create an array of structure, data of some certain size (10, in this case). But,
the size of array must be immediately updated to a prime number just greater than
initial array capacity (i.e 10, in this case).
Step 4: A menu is displayed on the screen.
Step 5: User must choose one option from four choices given in the menu Step 6: Perform
all the operations
Step 7: Stop the program

Program :

hashTable = [[],] * 10
def checkPrime(n):
if n == 1 or n == 0:
return 0
for i in range(2, n//2):
if n % i == 0:
return 0
return 1
def getPrime(n):
if n % 2 == 0:
n=n+1
while not checkPrime(n):
n += 2
return n
def hashFunction(key):
capacity = getPrime(10)
return key % capacity
def insertData(key, data):
index = hashFunction(key)
hashTable[index] = [key, data]
def removeData(key):
index = hashFunction(key)
hashTable[index] = 0
insertData(123, "apple")
insertData(432, "mango")
insertData(213, "banana")
insertData(654, "guava")
print(hashTable)
removeData(123)
print(hashTable)

Output:

[[], [], [123, 'apple'], [432, 'mango'], [213, 'banana'], [654, 'guava'], [], [], [], []]
[[], [], 0, [432, 'mango'], [213, 'banana'], [654, 'guava'], [], [], [], []]

Result:
Thus, the python program to implement hash table is executed successfully and output
isverified.
EX.NO:9 Implementation of Representation and Traversal Algorithm

Aim: To write a python program to implement tree representation and traversal algorithm.

Pre-order traversal.
Algorithm:
1. Visit the root (we will print it when we visit to show the order of visiting)
2. Traverse the left subtree in pre-order
3. Traverse the right subtree in pre-order
In-order traversal
Visit the root node in between the left and right node (in)
Algorithm:
1. Traverse the left subtree in in-order
2. Visit the root (we will print it when we visit to show the order of visiting)
3. Traverse the right subtree in in-order
Post-order traversal
Visit the root node after (post) visiting the left and right subtree.
Algorithm:
1. Traverse the left subtree in in-order
2. Traverse the right subtree in in-order
3. Visit the root (we will print it when we visit to show the order of visiting)

Program :

class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def printInorder(root):
if root:
printInorder(root.left)
print(root.val),
printInorder(root.right)
def printPostorder(root):
if root:
printPostorder(root.left)
printPostorder(root.right)
print(root.val),
def printPreorder(root):
if root:
print(root.val),
printPreorder(root.left)
printPreorder(root.right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print ("Preorder traversal of binary tree is")
printPreorder(root)
print ("\nInorder traversal of binary tree is")
printInorder(root)
print ("\nPostorder traversal of binary tree is")
printPostorder(root)
Output:

Preorder traversal of binary tree is


1
2
4
5
3
Inorder traversal of binary tree is
4
2
5
1
3
Postorder traversal of binary tree is
4
5
2
3
1

Result:
Thus, the above program for tree representation and traversal algorithm was successfully
completed.
EX.NO:10 Implementation of Binary Search Tree

Aim: To write a python program to implement Binary Search Tree.

Algorithm:

Step 1: Start the program


Step 2: Read the search element
Step 3: Find the middle element in the sorted list
Step 4: Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle
element Step 5: If the search element is smaller than the middle element, then repeat
steps 3 and 4 for the left sublist of the middle element
Step 6: If the search element is larger than the middle element, then repeat steps 3 and 4
for theright sublist of the middle element
Step 7: Repeat the process until the search element if found in the list
Step 8: If element is not found, loop terminates
Step 9: Stop the program

Program:

def binary_search(arr, low, high, x):


# Check base case
if high >= low:
mid = (high + low) // 2
# If element is present at the middle itself
if arr[mid] == x:
return mid
# If element is smaller than mid, then it can only
# be present in left subarray
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
# Else the element can only be present in right subarray
else:
return binary_search(arr, mid + 1, high, x)
else:
# Element is not present in the array
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")

Output:

Element is present at index 3

Result:
Thus, the above program for binary search tree was successfully completed.

You might also like