Experiment No.: 9 Implement Binary Search Tree ADT Using Linked List
Experiment No.: 9 Implement Binary Search Tree ADT Using Linked List
Experiment No.: 9 Implement Binary Search Tree ADT Using Linked List
: 9
Implement Binary Search Tree ADT using Linked
List.
CLASS: SE COMPS
DIVISION: A
BATCH: A1
THEORY:
In the binary tree, each node can have at most two children. Each node can have zero, one or
two children. Each node in the binary tree contains the following information:
The properties that separate a binary search tree from a regular binary tree is
All nodes of left subtree are less than the root node
All nodes of right subtree are more than the root node
Both subtrees of each node are also BSTs i.e. they have the above two properties
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −
The value of the key of the left sub-tree is less than the value of its parent (root) node's key.
The value of the key of the right sub-tree is greater than or equal to the value of its parent
(root) node's key.
Basic Operations
ALGORITHM:
1. Define Node class which has three attributes namely: data left and right. Here, left
represents the left child of the node and right represents the right child of the node.
2. When a node is created, data will pass to data attribute of the node and both left and
right will be set to null.
3. Define another class which has an attribute root.
4. Root represents the root node of the tree and initialize it to null.
5. insert() will add a new node to the tree:
6. It checks whether the root is null, which means the tree is empty. It will add the new
node as root.
7. Else, it will add root to the queue.
8. The variable node represents the current node.
9. First, it checks whether a node has a left and right child. If yes, it will add both nodes to
queue.
10.If the left child is not present, it will add the new node as the left child.
11.If the left is present, then it will add the new node as the right child.
12.Inorder() will display nodes of the tree in inorder fashion.
13.It traverses the entire tree then prints out left child followed by root then followed by
the right child.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
int data;
};
int main()
create_tree(tree);
clrscr();
do
scanf("%d", &option);
switch(option)
case 1:
scanf("%d", &val);
break;
case 2:
preorderTraversal(tree);
break;
case 3:
inorderTraversal(tree);
break;
case 4:
postorderTraversal(tree);
break;
case 5:
ptr = findSmallestElement(tree);
break;
case 6:
ptr = findLargestElement(tree);
break;
case 7:
scanf("%d", &val);
break;
case 8:
break;
case 9:
totalExternalNodes(tree));
break;
case 10:
totalInternalNodes(tree));
break;
case 11:
printf("\n The height of the tree = %d",Height(tree));
break;
case 12:
tree = mirrorImage(tree);
break;
case 13:
tree = deleteTree(tree);
break;
}while(option!=14);
getch();
return 0;
tree = NULL;
ptr–>data = val;
ptr–>left = NULL;
ptr–>right = NULL;
if(tree==NULL)
tree=ptr;
tree–>left=NULL;
tree–>right=NULL;
else
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
parentptr=nodeptr;
if(val<nodeptr–>data)
nodeptr=nodeptr–>left;
else
nodeptr = nodeptr–>right;
if(val<parentptr–>data)
parentptr–>left = ptr;
else
parentptr–>right = ptr;
return tree;
if(tree != NULL)
printf("%d\t", tree–>data);
preorderTraversal(tree–>left);
preorderTraversal(tree–>right);
if(tree != NULL)
inorderTraversal(tree->left);
printf("%d\t", tree->data);
inorderTraversal(tree->right);
}
}
if(tree != NULL)
postorderTraversal(tree->left);
postorderTraversal(tree->right);
printf("%d\t", tree->data);
return tree;
else
return tree;
else
return findLargestElement(tree->right);
}
if(tree–>left==NULL)
return(tree);
parent = tree;
cur = tree–>left;
parent = cur;
if(cur == NULL)
return(tree);
if(cur–>left == NULL)
ptr = cur–>right;
else if(cur–>right == NULL)
ptr = cur–>left;
else
psuc = cur;
cur = cur–>left;
while(suc–>left!=NULL)
psuc = suc;
suc = suc–>left;
if(cur==psuc)
// Situation 1
suc–>left = cur–>right;
}
else
// Situation 2
suc–>left = cur–>left;
psuc–>left = suc–>right;
suc–>right = cur–>right;
ptr = suc;
if(parent–>left == cur)
parent–>left=ptr;
else
parent–>right=ptr;
free(cur);
return tree;
if(tree==NULL)
return 0;
else
return(totalNodes(tree–>left) + totalNodes(tree–>right) + 1);
if(tree==NULL)
return 0;
return 1;
else
return (totalExternalNodes(tree–>left) +
totalExternalNodes(tree–>right));
return 0;
else
return (totalInternalNodes(tree–>left)
+ totalInternalNodes(tree–>right) + 1);
if(tree==NULL)
return 0;
else
leftheight = Height(tree–>left);
rightheight = Height(tree–>right);
else
if(tree!=NULL)
mirrorImage(tree–>left);
mirrorImage(tree–>right);
ptr=tree–>left;
ptr–>left = ptr–>right;
tree–>right = ptr;
if(tree!=NULL)
deleteTree(tree–>left);
deleteTree(tree–>right);
free(tree);
OUTPUT:
CONCLUSION:
Thus, we have successfully Binary Search Tree ADT using Linked list.