In computer science, a binary search tree (BST) or ordered binary tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.
The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.
Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays.
----------------------------------------------------------------------------------------------
A C PROGRAM TO IMPLEMENT OPERATIONS IN BINARY SEARCH TREE
COMPILER EMPLOYED: DEV C++ COMPILER-4.9.9.2
SOURCE FILE SIZE :2 kb
EXE FILE SIZE :22 kb
NOTE: PLEASE INCLUDE THE DESIRED HEADER FILE
---------------------------------------------------------------------------------------------
C PROGRAM SOURCE & EXE DOWNLOAD:
Click download button to download
DISCLAIMER: The following program cannot be ensured of perfection.so any flaws in the program can be notified in the comments section.
----------------------------------------------------------------------------------------------
CODE:
typedef struct treenode *searchtree;
typedef struct treenode *position;
struct treenode
{
int dat;
searchtree left,right;
}*t;
searchtree makeempty(searchtree t)
{
if(t!=NULL)
{
makeempty(t->left);
makeempty(t->right);
free(t);
}
return NULL;
}
position find(int a, searchtree t)
{
if(t==NULL)
return NULL;
if(a
return find(a,t->left);
else
if(a>t->dat)
return find(a,t->right);
else
return t;
}
position findmin(searchtree t)
{
if(t==NULL)
return NULL;
else
if(t->left==NULL)
return t;
else
return findmin(t->left);
}
position findmax(searchtree t)
{
if(t==NULL)
return NULL;
else
if(t->right==NULL)
return t;
else
findmax(t->right);
}
searchtree insert(int a, searchtree t)
{
if(t==NULL)
{
t=malloc(sizeof(struct treenode));
t->dat=a;
t->left=t->right=NULL;
}
else
{
if(a
t->left= insert(a,t->left);
else
if(a>t->dat)
t->right=insert(a,t->right);
}
return t;
}
searchtree delete(int a,searchtree t)
{
position tmpcell;
if(t==NULL)
printf("element not present");
else
if(a
t->left=delete(a,t->left);
else
if(a>t->dat)
t->right=delete(a,t->right);
else
if(t->left&&t->right)
{
tmpcell=findmin(t->right);
t->dat=tmpcell->dat;
t->right=delete(t->dat,t->right);
}
else
{
tmpcell=t;
if(t->left==NULL)
t=t->right;
else
if(t->right==NULL)
t=t->left;
free(tmpcell);
}
return t;
}
void disp()
{
struct treenode *temp;
printf("\t%d",t->dat);
temp=t->left;
while(temp!=NULL)
{
printf("\n%d",temp->dat);
temp=temp->left;
}
temp=t->right;;
while(temp!=NULL)
{
printf("\n\t\t%d",temp->dat);
temp=temp->right;
}}
int main()
{
int OPT,CH,a;
struct treenode *temp;
t=NULL;
temp=makeempty(t);
do
{
printf("\n BINARY SEARCH TREE LINKED LIST IMPLEMENTATION");
printf("\n\n MENU:");
printf("\n [1].INSERTION");
printf("\n [2].DELETION");
printf("\n [3].DISPLAY");
printf("\n [4].SEARCH");
printf("\n [5].EXIT");
printf("\n\n OPTION:");
scanf("%d",&OPT);
switch(OPT)
{
case 1:
printf("\nINSERTION PROCESS INVOKED...");
printf("\nENTER ELEMENT:");
scanf("%d",&a);
t=insert(a,t);
disp();
break;
case 2:
printf("\nDELETION PROCESS INVOKED...");
printf("\nENTER ELEMENTS TO DELETE");
scanf("%d",&a);
t=delete(a,t);
disp();
break;
case 3:
printf("\nDISPLAY PROCESS INVOKED...");
printf("\nTHE ELEMENTS:");
disp();
break;
case 4:
printf("\nSEARCH PROCESS INVOKED...");
printf("\nENTER DATA :");
scanf("%d",&a);
temp=find(a,t);
if(temp!=NULL)
printf("\n ELEMENT NOT FOUND");
else
printf("\n ELEMENT NOT FOUND");
break;
default:
printf("\nTERMINATING...");
break;
}
printf("\n DO YOU WISH TO CONTINUE?1~0:");
scanf("%d",&CH);
}while(CH==1);
system("pause");
return 0;
}
----------------------------------------------------------------------------------------------
Your's friendly,
[MOHANRAM.G],
ADMIN...
0 comments:
Post a Comment