Experiment No.
4
Name of Experiment: Binary Search Tree and Operations
Aim: Implement Insert, Delete, Search and Display operations on Binary Search Tree and
analyze space and time complexity
Software Required: C/C++, Java Compiler
Pre-Requisites: C Programming Concepts, Data Structures
Theory:
A Binary Search Tree (BST) is a node-based binary tree where:
● The left child of a node contains only nodes with keys less than the node’s key.
● The right child contains only nodes with keys greater than the node’s key.
● BST allows efficient insertion, searching, and deletion.
Common Operations:
● Insertion: Adds a node while maintaining the BST property.
● Search: Locates a node with a given value.
● Deletion: Removes a node with proper restructuring.
● Display: Shows tree contents (typically via In-order traversal)
Algorithm:
1. Insert Operation:
INSERT(root, key):
if root is NULL:
return new node with key
if key < root.data:
root.left = INSERT(root.left, key)
else if key > root.data:
root.right = INSERT(root.right, key)
return root
2. Search Operation:
SEARCH(root, key):
if root is NULL or root.data == key:
return root
if key < root.data:
return SEARCH(root.left, key)
else:
return SEARCH(root.right, key)
3. Delete Operation:
DELETE(root, key):
if root is NULL:
return root
if key < root.data:
root.left = DELETE(root.left, key)
else if key > root.data:
root.right = DELETE(root.right, key)
else:
// Node with only one child or no child
if root.left is NULL:
return root.right
else if root.right is NULL:
return root.left
// Node with two children
temp = MINVALUE(root.right)
root.data = temp.data
root.right = DELETE(root.right, temp.data)
return root
MINVALUE(node):
current = node
while current.left is not NULL:
current = current.left
return current
4. Display Operation (In-order Traversal):
INORDER(root):
if root is not NULL:
INORDER(root.left)
print(root.data)
INORDER(root.right)
Conclusion:
The Binary Search Tree (BST) is a fundamental data structure that supports efficient searching,
insertion, and deletion operations. With proper implementation, a BST maintains sorted order
and allows dynamic updates. Traversals like in-order can be used to display elements in sorted
order, making BSTs highly useful in many applications like databases, file systems, and more.