#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct tree
{
int item;
struct tree*left;
struct tree*right;
};
struct tree*insert(struct tree*root,int x)
{
if(root==NULL)
{
root=(struct tree*)malloc(sizeof(struct tree));
root->left=NULL;
root->item=x;
root->right=NULL;
return root;
}
else
{
if(x<root->item)
{
root->left=insert(root->left,x);
return root;
}
else
{
root->right=insert(root->right,x);
return root;
}
}
}
void preorder(struct tree*root)
{
if(root!=NULL)
{
printf("\t%d",root->item);
if(root->left!=NULL)
preorder(root->left);
if(root->right!=NULL)
preorder(root->right);
}
}
void inorder(struct tree*root)
{
if(root!=NULL)
{
if(root->left!=NULL)
inorder(root->left);
printf("\t%d",root->item);
if(root->right!=NULL)
inorder(root->right);
}
}
void postorder(struct tree*root)
{
if(root!=NULL)
{
if(root->left!=NULL)
postorder(root->left);
if(root->right!=NULL)
postorder(root->right);
printf("\t%d",root->item);
}
}
void main()
{
struct tree*p,*q;
int x,i,n,j;
clrscr();
p=NULL;
printf("\n\n\t\t\t\t<<== BINARY TREE ==>>\n\n");
while(1)
{
printf("\nPRESS 1 FOR INSERT ELEMENT IN THE TREE=");
printf("\nPRESS 2 FOR PREORDER TRAVERSAL=");
printf("\nPRESS 3 FOR INORDER TRAVERSAL=");
printf("\nPRESS 4 FOR POSTORDER TRAVERSAL=");
printf("\nPRESS 5 FOR QUIT=");
printf("\nSELECT CHOICE=");
scanf("%d",&i);
clrscr();
printf("\n\n\t\t\t\t<<== BINARY TREE ==>>\n\n");
switch(i)
{
case 1: printf("\nELEMENT NUMBERS ? ");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
printf("\nINSERT %d ELEMENT=",j);
scanf("%d",&x);
p=insert(p,x);
}
break;
case 2: printf("\nPREORDER TRAVERSAL==>");
preorder(p);
break;
case 3: printf("\nINORDER TRAVERSAL==>");
inorder(p);
break;
case 4: printf("\nPOSTORDER TRAVERSAL==>");
postorder(p);
break;
case 5: exit(0);
default:printf("****WRONG CHOICE****");
break;
}
}
getch();
}
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct tree
{
int item;
struct tree*left;
struct tree*right;
};
struct tree*insert(struct tree*root,int x)
{
if(root==NULL)
{
root=(struct tree*)malloc(sizeof(struct tree));
root->left=NULL;
root->item=x;
root->right=NULL;
return root;
}
else
{
if(x<root->item)
{
root->left=insert(root->left,x);
return root;
}
else
{
root->right=insert(root->right,x);
return root;
}
}
}
void preorder(struct tree*root)
{
if(root!=NULL)
{
printf("\t%d",root->item);
if(root->left!=NULL)
preorder(root->left);
if(root->right!=NULL)
preorder(root->right);
}
}
void inorder(struct tree*root)
{
if(root!=NULL)
{
if(root->left!=NULL)
inorder(root->left);
printf("\t%d",root->item);
if(root->right!=NULL)
inorder(root->right);
}
}
void postorder(struct tree*root)
{
if(root!=NULL)
{
if(root->left!=NULL)
postorder(root->left);
if(root->right!=NULL)
postorder(root->right);
printf("\t%d",root->item);
}
}
void main()
{
struct tree*p,*q;
int x,i,n,j;
clrscr();
p=NULL;
printf("\n\n\t\t\t\t<<== BINARY TREE ==>>\n\n");
while(1)
{
printf("\nPRESS 1 FOR INSERT ELEMENT IN THE TREE=");
printf("\nPRESS 2 FOR PREORDER TRAVERSAL=");
printf("\nPRESS 3 FOR INORDER TRAVERSAL=");
printf("\nPRESS 4 FOR POSTORDER TRAVERSAL=");
printf("\nPRESS 5 FOR QUIT=");
printf("\nSELECT CHOICE=");
scanf("%d",&i);
clrscr();
printf("\n\n\t\t\t\t<<== BINARY TREE ==>>\n\n");
switch(i)
{
case 1: printf("\nELEMENT NUMBERS ? ");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
printf("\nINSERT %d ELEMENT=",j);
scanf("%d",&x);
p=insert(p,x);
}
break;
case 2: printf("\nPREORDER TRAVERSAL==>");
preorder(p);
break;
case 3: printf("\nINORDER TRAVERSAL==>");
inorder(p);
break;
case 4: printf("\nPOSTORDER TRAVERSAL==>");
postorder(p);
break;
case 5: exit(0);
default:printf("****WRONG CHOICE****");
break;
}
}
getch();
}
No comments:
Post a Comment