0% found this document useful (0 votes)
16 views7 pages

Lab Manual 10

Uploaded by

obaid ur rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Lab Manual 10

Uploaded by

obaid ur rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 416: Data Structure and Algorithms

Lab 10 Manual
Binary Search Trees

Instructor & Demonstrator: Engr. Nauman Ahmad Tariq

Student Name OBAID UR REHMAN

Roll No. ELEN51F20R010

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.

ABOUT THE EXPERIMENT:

BINARY SEARCH TREE:


Binary Search Tree is a node-based binary tree data structure which has the following properties:

 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;

Python Program Code:


import time

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 insert(node, key):


if node is None:
return new_node(key)
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
return node

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)

print("The tree is:")


start_time = time.time()
traverse_tree(root)
end_time = time.time()

print("\nExecution Time:", end_time - start_time, "seconds")

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:

The tree is:


17 18 19 23 27 38 50
Execution Time: 0.0 seconds

Searching for 28 in this tree


Element not found

Process finished with exit code 0

Python Program Code (for 200,000 nodes):

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 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 insert(node, key):


if node is None:
return new_node(key)
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
return node

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)

print("All nodes in the tree are:")


start_time = time.time()
traverse_tree_and_print(root)
end_time = time.time()

print("\nExecution Time:", end_time - start_time, "seconds")

key_to_search = random.randint(1, size) # Random number to search for


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:

Execution Time: 2.2738044261932373 seconds

Searching for 163039 in this tree


Element found

Process finished with exit code 0


Assessment Rubric for Lab 10
Method of Evaluation: Lab report.

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.

Performance 5 Excellent 4 Good 3 Satisfactory 2-1 Needs Marks


Improvement
Realization of Fully understand Close to fully Partially Unable to
Experiment & able to illustrate understand & able understands & understand & able
CLO-1 Binary Search to illustrate Binary able to illustrate to illustrate Binary
Trees Search Trees Binary Search Search Trees
Trees
Conducting Completely able to Close to Partially able to Unable to
Experiment successfully completely able to successfully successfully
CLO-2 design and successfully design and design and
compile Python design and compile Python compile Python
programs for compile Python programs for programs for
Binary Search programs for Binary Search Binary Search
Trees Binary Search Trees Trees
Trees
Data Collection Completely Close to Partially Unable to
and Data Analysis understand & able completely understand & able understand & able
CLO-3 to demonstrate understand & able to demonstrate to demonstrate
syntax of Binary to demonstrate syntax of Binary syntax of Binary
Search Trees syntax of Binary Search Trees Search Trees
Search Trees

Individual/Team Completely able to Close to Partially able to Unable to execute


Work execute different completely able to execute different different programs
CLO-4 programs for execute different programs for for Binary Search
Binary Search programs for Binary Search Trees
Trees Binary Search Trees
Trees

Total

You might also like