DSP PR 20
DSP PR 20
Date: ……………
Practical No.20: Write a menu driven program to perform following operation on Binary Search
Tree:
a. Create a BST.
b. Insert an element in BST.
c. Pre-order traversal of BST.
d. In-order traversal of BST.
e. Post-order traversal of BST.
f. Delete an element from BST.
A. Objective:
E. Practical Outcome(PRO)
Write a program to implement different operations on Binary Search Tree.
136 | Page
226120316058
Data Structure with Python (4331601)
Search Operation:
Whenever an element is to be searched, start searching from the root node. Then if
the data is less than the key value, search for the element in the left subtree.
Otherwise, search for the element in the right subtree. Follow the same algorithm for
each node.
The basic steps to perform search operation:
• Check whether the tree is empty or not If the tree is empty, search is not
possible
• Otherwise, first search the root of the tree.
• If the key does not match with the value in the root, search its subtrees. If
the value of the key is less than the root value, search the left subtree If the
value of the key is greater than the root value, search the right subtree.
• If the key is not found in the tree, return unsuccessful search.
Insert Operation:
Whenever an element is to be inserted, first locate its proper location. Start searching
from the root node, then if the data is less than the key value, search for the empty
location in the left subtree and insert the data. Otherwise, search for the empty
location in the right subtree and insert the data.
The basic steps to perform insert operation:
• If the tree is empty, insert the first element as the root node of the tree. The
following elements are added as the leaf nodes.
• If an element is less than the root value, it is added into the left subtree as a
leaf node.
137 | Page
226120316058
Data Structure with Python (4331601)
• If an element is greater than the root value, it is added into the right subtree
as a leaf node.
• The final leaf nodes of the tree point to NULL values as their child nodes.
Inorder Traversal:
The inorder traversal operation in a Binary Search Tree visits all its nodes in the
following order:
• Traverse the left subtree, recursively Traverse the right subtree, recursively.
• Then, traverse the root node Delete Operation:
The basic steps to perform delete operation:
• Create a recursive function which returns the correct node that will be in the
position.
• If the root is NULL, then return root (Base case)
• If the key is less than the root’s value, then set root->left = deleteNode (root-
>left, key)
• If the key is greater than the root’s value, then set root->right = deleteNode
(root->right, key)
• Else check o If the root is a leaf node then return null o Else if it has only the
left child, then return the left child o Else if it has only the right child, then
return the right child o Otherwise, set the value of root as of its inorder
successor and recur to delete the node with the value of the inorder successor.
138 | Page
226120316058
Data Structure with Python (4331601)
We observe that the root node key (27) has all less-valued keys on the left sub-tree
and the higher valued keys on the right sub-tree.
I. Resources/Equipment Required
Sr.No. Instrument/Equipment Specification
Quanti
/Components/Trainer kit
ty
2. PYTHON IDE(any) 1
K. Source code:
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
139 | Page
226120316058
Data Structure with Python (4331601)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
140 | Page
226120316058
Data Structure with Python (4331601)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
141 | Page
226120316058
Data Structure with Python (4331601)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
142 | Page
226120316058
Data Structure with Python (4331601)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
143 | Page
226120316058
Data Structure with Python (4331601)
OUTPUT :
144 | Page
226120316058
Data Structure with Python (4331601)
2. Draw the binary search tree that is created if the following numbers are inserted in
the tree in the given order.
12 15 3 35 21 42 14
145 | Page
226120316058
Data Structure with Python (4331601)
O. Assessment-Rubrics
Agenda Rubric Level of Achievement
Paramet
ers Excellent Very Good Good (3) Average Poor (1)
(5) (4) (2)
146 | Page
226120316058
Data Structure with Python (4331601)
147 | Page
226120316058