BINARY SEARCH TREE USING LINK LIST WITH CREATION,INSERTION & TRAVERSAL PRE-ORDER,IN-ORDER,POST-ORDER
DOWNLOAD PROGRAM
CODING:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree=NULL;
struct node *insert(struct node *,int);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
void main()
{
int ch,data;
clrscr();
do
{
printf("\n ___MAIN MENU___");
printf("\n1.INSERTION");
printf("\n2.TRAVERSAL PRE-ORDER");
printf("\n3.TRAVERSAL IN-ORDER ");
printf("\n4.TRAVERSAL POST-ORDER");
printf("\n5.EXIT");
printf("\nENTER THE NUMBER:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER THE DATA:");
scanf("%d",&data);
tree=insert(tree,data);
break;
case 2:
preorder(tree);
break;
case 3:
inorder(tree);
break;
case 4:
postorder(tree);
break;
case 5:
printf("PRESS ENTER FOR EXIT...");
break;
default:
printf("INVALID ENTER THE NUMBER>TRY AGSIN...");
}
}while(ch!=5);
getch();
}
struct node *insert(struct node *tree,int data)
{
struct node *new_node,*ptr,*preptr;
new_node=(struct node *)malloc(sizeof(struct node ));
new_node->data=data;
new_node->left=NULL;
new_node->right=NULL;
if(tree==NULL)
{
tree=new_node;
tree->left=NULL;
tree->right=NULL;
}
else
{
preptr=NULL;
ptr=tree;
while(ptr!=NULL)
{
preptr=ptr;
if(data<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(data<preptr->data)
preptr->left=new_node;
else
preptr->right=new_node;
}
return tree;
}
void preorder(struct node *tree)
{
if(tree!=NULL)
{
printf("%d , ",tree->data);
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%d , ",tree->data);
preorder(tree->right);
}
}
void postorder(struct node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%d , ",tree->data);
}
}
CODING:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree=NULL;
struct node *insert(struct node *,int);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
void main()
{
int ch,data;
clrscr();
do
{
printf("\n ___MAIN MENU___");
printf("\n1.INSERTION");
printf("\n2.TRAVERSAL PRE-ORDER");
printf("\n3.TRAVERSAL IN-ORDER ");
printf("\n4.TRAVERSAL POST-ORDER");
printf("\n5.EXIT");
printf("\nENTER THE NUMBER:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER THE DATA:");
scanf("%d",&data);
tree=insert(tree,data);
break;
case 2:
preorder(tree);
break;
case 3:
inorder(tree);
break;
case 4:
postorder(tree);
break;
case 5:
printf("PRESS ENTER FOR EXIT...");
break;
default:
printf("INVALID ENTER THE NUMBER>TRY AGSIN...");
}
}while(ch!=5);
getch();
}
struct node *insert(struct node *tree,int data)
{
struct node *new_node,*ptr,*preptr;
new_node=(struct node *)malloc(sizeof(struct node ));
new_node->data=data;
new_node->left=NULL;
new_node->right=NULL;
if(tree==NULL)
{
tree=new_node;
tree->left=NULL;
tree->right=NULL;
}
else
{
preptr=NULL;
ptr=tree;
while(ptr!=NULL)
{
preptr=ptr;
if(data<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(data<preptr->data)
preptr->left=new_node;
else
preptr->right=new_node;
}
return tree;
}
void preorder(struct node *tree)
{
if(tree!=NULL)
{
printf("%d , ",tree->data);
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%d , ",tree->data);
preorder(tree->right);
}
}
void postorder(struct node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%d , ",tree->data);
}
}
Comments
Post a Comment