COMPLETE BST PROGRAM.
BST PROGRAM!
#include<conio .h>
#include<conio .h>
#include<stdio .h>
struct node{
int data ;
struct node *left;
struct node *right;
};
void preorder(struct node *root); //declare preorder for travsal
void searching(struct node *root);// searching in binary tree
void maximum(struct node *root);// find maximum in binary tree
void minimum(struct node *root);//find minimum in binary tree
void main()
{
struct node *root=NULL,*temp,*current,*parent;
int i,n,count;
clrscr();
printf("enter the number of nodes in bst\n");
scanf("%d",&n);
for(i=0;i {
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->data);
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
current=root;
while(current!=NULL)
{
parent=current;
if(temp->data > current->data)
current=current->right;
else
current=current->left;
}
if(parent->data >temp->data)
parent->left=temp;
else
parent->right=temp;
}
}
printf("traversal in binary search tree\n");
preorder(root);//function calling
printf("searching in binary search tree\n");
searching(root);//searching in bst
printf("finding maximum number in binary search tree\n");
maximum(root);//finding maximum
printf("finding minimum number in binary search tree\n");
minimum(root);//finding minimum
getch();
}
void preorder(struct node *root)//define preorder
{
if(root==NULL)
return;
printf("%d\n",root->data);
preorder(root->left);
preorder(root->right);
}
void searching(struct node *root)//define bst
{ struct node *ptr;
int n,count;
printf("enter the number which do you want search in binary search tree\n");
scanf("%d",&n);
ptr=root;
if(ptr==NULL)
printf("the number does not exit\n");
else
{
count=0;
while(ptr->data!=n)
{
if(ptr->data>n)
{ ptr=ptr->left;
count++;
}
else
{ ptr=ptr->right;
count++;
}}
printf("%d element is found at\n",count+1);
}}
void maximum(struct node *root)
{
struct node *ptr,*max;
max=root;
ptr=root;
while(ptr->right!=NULL)
{
ptr=ptr->right;
if(ptr->data >max->data)
max=ptr;
}
printf("maximum data in binary serch tree is %d\n",max->data);
}
void minimum(struct node *root)
{
struct node *ptr,*min;
min=root;
ptr=root;
while(ptr->left!=NULL)
{
ptr=ptr->left;
if(ptr->data data)
min=ptr;
}
printf("maximum data in binary serch tree is %d",min->data);
}
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->data);
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
current=root;
while(current!=NULL)
{
parent=current;
if(temp->data > current->data)
current=current->right;
else
current=current->left;
}
if(parent->data >temp->data)
parent->left=temp;
else
parent->right=temp;
}
}
printf("traversal in binary search tree\n");
preorder(root);//function calling
printf("searching in binary search tree\n");
searching(root);//searching in bst
printf("finding maximum number in binary search tree\n");
maximum(root);//finding maximum
printf("finding minimum number in binary search tree\n");
minimum(root);//finding minimum
getch();
}
void preorder(struct node *root)//define preorder
{
if(root==NULL)
return;
printf("%d\n",root->data);
preorder(root->left);
preorder(root->right);
}
void searching(struct node *root)//define bst
{ struct node *ptr;
int n,count;
printf("enter the number which do you want search in binary search tree\n");
scanf("%d",&n);
ptr=root;
if(ptr==NULL)
printf("the number does not exit\n");
else
{
count=0;
while(ptr->data!=n)
{
if(ptr->data>n)
{ ptr=ptr->left;
count++;
}
else
{ ptr=ptr->right;
count++;
}}
printf("%d element is found at\n",count+1);
}}
void maximum(struct node *root)
{
struct node *ptr,*max;
max=root;
ptr=root;
while(ptr->right!=NULL)
{
ptr=ptr->right;
if(ptr->data >max->data)
max=ptr;
}
printf("maximum data in binary serch tree is %d\n",max->data);
}
void minimum(struct node *root)
{
struct node *ptr,*min;
min=root;
ptr=root;
while(ptr->left!=NULL)
{
ptr=ptr->left;
if(ptr->data
min=ptr;
}
printf("maximum data in binary serch tree is %d",min->data);
}