0% found this document useful (0 votes)
127 views

Ex. No. 6 Implementation of Binary Search Tree

The document describes a C program to implement a binary search tree. It includes algorithms for inserting and deleting nodes from the binary search tree. The insertion algorithm compares a new node's data to the root node and inserts in the left or right subtree. The deletion algorithm handles cases for leaf, single-child, and double-child nodes. The program contains functions for finding, inserting, deleting and displaying nodes in the binary search tree. Sample output is provided showing the program executing insertions, deletions, finds and traversals.

Uploaded by

addssdfa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Ex. No. 6 Implementation of Binary Search Tree

The document describes a C program to implement a binary search tree. It includes algorithms for inserting and deleting nodes from the binary search tree. The insertion algorithm compares a new node's data to the root node and inserts in the left or right subtree. The deletion algorithm handles cases for leaf, single-child, and double-child nodes. The program contains functions for finding, inserting, deleting and displaying nodes in the binary search tree. Sample output is provided showing the program executing insertions, deletions, finds and traversals.

Uploaded by

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

Ex. No.

6 IMPLEMENTATION OF BINARY SEARCH TREE

AIM:

To write a C program to implement binary search tree.

ALGORITHM:

Insertion:

1. Check whether the root of binary search tree is NULL.


2. If the condition is true, it means that the binary search tree is empty, hence consider the
new node as the root node. Otherwise ,follow the next steps.
3. Compare the new node data with root node data, for the following three conditions.
- If new node data is equal to root node data, insertion operation is teriminated.
- If new node data is lesser than root node data, check whether the left child of the
root node is NULL. If the condition is true, insert a new data and teriminate the
process. Otherwise, consider the left child of the root node as root node and check
for three conditions again.
- If new node data is greater than root node data, check whether the right child of
the root node is NULL. If the condition is true, insert a new data and terminate the
process. Otherwise, consider the right child of the root node as root node and
check for three conditions again.
Deletion:

1. If node to be deleted is leaf node then search the parent of the leaf node and make the link
of the leaf node as NULL.
2. If node to be deleted has only one child then search the parent of that node and assign the
link of the parent node to the child of the node to be deleted.
3. If the node to be deleted has two children then follow the following steps
- Copy the minimum value of its right subtree to deleted node position(del_node).
- Deallocte the memory of del_node.
PROGRAM:

#include<conio.h>
#include<stdio.h>
struct treenode;
typedef struct treenode *position;
typedef struct treenode *searchtree;
position find(int x,searchtree t);
position findmin(searchtree t);
position findmax(searchtree t);
searchtree insert(int x,searchtree t);
searchtree delete(int x,searchtree t);
void display(searchtree t);
struct treenode
{
int element;
struct treenode *left;
struct treenode *right;
};
position t,p;
void main()
{
int op,x;
clrscr();
t=NULL;
while(1)
{
printf("\nBINARY SEARCH TREE OPERATIONS");
printf("\n1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit\nenter option:");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nINSERT OPERATION");
printf("\nenter the element:");
scanf("%d",&x);
t=insert(x,t);
break;
case 2:
printf("\nDELETE OPERATION");
printf("\nenter the element:");
scanf("%d",&x);
t=delete(x,t);
break;
case 3:
printf("\nFIND OPERATION");
printf("\nenter the element:");
scanf("%d",&x);
p=find(x,t);
if(p==NULL)
printf("\nelement not found");
else
printf("\nthe element is found at the position %d",p);
break;
case 4:
printf("\nFIND MINIMUM OPERATION");
p=findmin(t);
printf("\nthe minimum element %d is found at the position %d",p->element,p);
break;
case 5:
printf("\nFIND MAXIMUM OPERATION");
p=findmax(t);
printf("\nthe maximum element %d is found at the position %d",p->element,p);
break;
case 6:
printf("\nDISPLAY OPERATION");
display(t);
break;
case 7:
exit(1);
break;
default:
printf("\nINVALID");
break;
}
}
}

position find(int x,searchtree t)


{
if(t==NULL)
return NULL;
else if(x<t->element)
return find(x,t->left);
else if(x>t->element)
return find(x,t->right);
else return t;
}

position findmin(searchtree t)
{
if(t!=NULL)
while(t->left!=NULL)
t=t->left;
return t;
}

position findmax(searchtree t)
{
if(t!=NULL)
while(t->right!=NULL)
t=t->right;
return t;
}
searchtree insert(int x,searchtree t)
{
if(t==NULL)
{
t=(searchtree)malloc(sizeof(struct treenode));
if(t==NULL)
printf("\nout of space");
t->element=x;
t->left=t->right=NULL;
}
else if(x<t->element)
t->left=insert(x,t->left);
else if(x>t->element)
t->right=insert(x,t->right);
return t;
}

searchtree delete(int x,searchtree t)


{
position tmpcell;
if(t==NULL)
printf("element not found");
else if(x<t->element)
t->left=delete(x,t->left);
else if(x>t->element)
t->right=delete(x,t->right);
else if(t->left && t->right)
{
tmpcell=findmax(t->right);
t->element=tmpcell->element;
t->right=delete(t->element,t->right);
}
else
{
tmpcell=t;
if(t->left==NULL)
t=t->right;
if(t->right==NULL)
t=t->left;
}
free(tmpcell);
return t;
}
void display(searchtree t)
{
if(t!=NULL)
{
display(t->left);
printf("\n%d %d",t,t->element);
display(t->right);
}
}
OUTPUT:
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:1
INSERT OPERATION
enter the element:1
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:1
INSERT OPERATION
enter the element:2
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:1
INSERT OPERATION
enter the element:3
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:2
DELETE OPERATION
enter the element:3
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:3
FIND OPERATION
enter the element:2
the element is found at the position 2352
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:4
FIND MINIMUM OPERATION
the minimum element 1 is found at the position 2342
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:5
FIND MAXIMUM OPERATION
the maximum element 2 is found at the position 2352
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:6
DISPLAY OPERATION
2342 1
2352 2
BINARY SEARCH TREE OPERATIONS
1.insert 2.delete 3.find 4.find min 5.find max 6.display 7.quit
enter option:7
Result:

Thus the program for binary search tree is implemented and executed successfully.

You might also like