Expt 8 DSA Lab (ECS) Implement Binary Search Tree Using Linked List
Expt 8 DSA Lab (ECS) Implement Binary Search Tree Using Linked List
_____________________________________________________________________________________
Experiment No. 8
Title Implement Binary Search Tree ADT using Linked List
Name
Year/Branch
Roll No.
UIN
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Theory:
In a binary tree, every node can have a maximum of two children but there is no need to maintain the order
of nodes basing on their values. In a binary tree, the elements are arranged in the order they arrive at the tree
from top to bottom and left to right.
Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and
only larger values in its right subtree.
To enhance the performance of binary tree, we use a special type of binary tree known as Binary Search
Tree. Binary search tree mainly focuses on the search operation in a binary tree. Binary search tree can be
defined as follows...
Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and
only larger values in its right subtree.
In a binary search tree, all the nodes in the left subtree of any node contains smaller values and all the nodes
in the right subtree of any node contains larger values as shown in the following figure...
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains nodes with
smaller values and right subtree of every node contains larger values.
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Every binary search tree is a binary tree but every binary tree need not to be binary search tree.
ALGORITHMS:
● Step 9 - If we reach to the leaf node and if it is also not matched with the search element, then
display "Element is not found" and terminate the function.
● Step 4 - Then check whether deleting node came to case 1 or case 2 or else goto step 2
● Step 5 - If it comes to case 1, then delete using case 1 logic.
● Step 6- If it comes to case 2, then delete using case 2 logic.
● Step 7 - Repeat the same process until the node is deleted from the tree.
Example
Construct a Binary Search Tree by inserting the following sequence of numbers...
10,12,5,4,20,8,7,15 and 13
Above elements are inserted into a Binary Search Tree as follows...
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Program: (This is just sample prog for ref, students should develop their own prog.)
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
int data;
} node;
if(root == NULL){
root = (node*)malloc(sizeof(node));
root->data = data;
root->left = Insert(root->left,data);
else
root->right = Insert(root->right,data);
return root;
if (temp != NULL){
inorder(temp->left);
inorder(temp->right);
}
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
if (temp != NULL){
preorder(temp->left);
preorder(temp->right);
if (temp != NULL){
postorder(temp->left);
postorder(temp->right);
void main() {
int choice,num;
while(1) {
printf("\n --------MENU------------\n");
printf("1\tInsert\n");
printf("2\tPreorder Display\n");
printf("4\tPostorder Display\n");
printf("5\tExit\n");
scanf("%d",&choice);
switch(choice){
case 1:
scanf("%d",&num);
root=Insert(root,num);
break;;
case 2:
printf("Pre-Order Display\n");
preorder(root);
break;;
case 3:
printf("In-Order Display\n");
inorder(root);
break;;
case 4:
printf("Post-Order Display\n");
postorder(root);
break;;
case 5:
exit(0);
default:
break;
}
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Conclusion: In conclusion, implementing a Binary Search Tree ADT using a linked list
allows for efficient insertion, deletion, and search operations, maintaining a dynamic
structure that adapts to varying data sizes. This approach exemplifies the advantages of
linked data structures in managing hierarchical data effectively.