Lab Manual 10
Lab Manual 10
UNIVERSITY OF SARGODHA
Lab 10 Manual
Binary Search Trees
Date Performed
Marks obtained
Instructor Signature
CLO-1 Illustrate understanding of key concepts of Data structures
and Algorithms using Python PyCharm Platform
CLO-2 Design a programming application by applying concepts of
Data Structures and algorithms leading to the solution of a moderate
scale-programming problem.
CLO-3 Write lab notes, effective communication and the analysis of
the given problem to perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or
individually with respect to the contribution.
OBJECTIVE:
Objective of this experiment is to design, develop and implement a Python program that creates a binary
search tree and is able to insert and search a data element in the BST.
The left subtree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
The left and right subtree each must also be a binary search tree.
Searching a key
To search a given key in Binary Search Tree, we first compare it with root, if the key is present at root, we
return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for
left subtree.
ALGORITHM
1. Start from root.
2. Compare the inserting element with root, if less than root, then recurse for left, else recurse for right.
3. If element to search is found anywhere, return true, else return false.
Insertion of a key
A new key is always inserted at leaf. We start searching a key from root till we hit a leaf node. Once a leaf
node is found, the new node is added as a child of the leaf node.
100 100
/ \ Insert 40 / \
20 500 ---------> 20 500
/ \ / \
10 30 10 30
\
40
ALGORITHM
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
class Node:
def __init__(self, key):
self.key = key
self.left = self.right = None
def new_node(item):
temp = Node(item)
return temp
def traverse_tree(root):
if root:
traverse_tree(root.left)
print(root.key, end=" \t")
traverse_tree(root.right)
def search(root, key):
if root is None or root.key == key:
return root
if root.key < key:
return search(root.right, key)
return search(root.left, key)
def main():
root = None
root = insert(root, 23)
insert(root, 18)
insert(root, 27)
insert(root, 38)
insert(root, 17)
insert(root, 19)
insert(root, 50)
key_to_search = 28
print(f"\nSearching for {key_to_search} in this tree")
if search(root, key_to_search):
print("Element found")
else:
print("Element not found")
if __name__ == "__main__":
main()
OUTPUT:
import time
import random
class Node:
def __init__(self, key):
self.key = key
self.left = self.right = None
def new_node(item):
temp = Node(item)
return temp
def traverse_tree_and_print(root):
if root:
traverse_tree_and_print(root.left)
print(root.key, end=" \t")
traverse_tree_and_print(root.right)
def main():
root = None
size = 200000
# Populate the tree with 200,000 random nodes
keys = random.sample(range(1, size + 1), size)
for key in keys:
root = insert(root, key)
if __name__ == "__main__":
main()
OUTPUT:
Outcomes Assessed:
CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using
Python PyCharm Platform.
CLO-2 Design a programming application by applying concepts of Data Structures and
algorithms leading to the solution of a moderate scale-programming problem.
CLO-3 Write lab notes, effective communication and analysis of the given problem to
perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually with respect to the
contribution.
Total