Saturday, August 25, 2007

C program inorder preorder postorder traversal

/* Data structure program to create a tree and traverse the tree in
1. preorder
2. inorder
3. postorder */


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct treelist
{
int data;
struct treelist *left;
struct treelist *right;
};
typedef struct treelist tree;

tree *cretree(tree *t,int data);
void preorder(tree *t);
void inorder(tree *t);
void postorder(tree *t);

void main()
{
tree *t = NULL;
int n,a,i,ch;
clrscr();
printf("How many numbers do you wan't to enter : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number %d :....: ",i+1);
scanf("%d",&a);
t = cretree(t,a);
}
while(1)
{
printf("\nEnter the order of traversal of tree \n");
printf("1. preorder \n");
printf("2. inorder \n");
printf("3. postorder \n");
printf("4. Terminate the program\n");
printf("Enter the choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 :
preorder(t);
break;
case 2 :
inorder(t);
break;
case 3 :
postorder(t);
break;
case 4 :
printf("Good Bye");
getch();
exit(0);
default :
printf("You have entered wrong choice ");
break;
}
}
}

tree *cretree(tree *t,int x)
{
tree *newnode;
if(t == NULL )
{
newnode = (tree*)malloc(sizeof(tree));
newnode->data = x;
newnode->left = NULL;
newnode->right= NULL;
return(newnode);
}
else if(x < t->data)
{
if(t->left == NULL)
{
newnode = (tree*)malloc(sizeof(tree));
newnode->data = x;
t->left = newnode;
newnode->left = NULL;
newnode->right = NULL;
return(t);
}
else
cretree(t->left,x);
}
else
{
if(t->right == NULL)
{
newnode = (tree*)malloc(sizeof(tree));
newnode->data = x;
t->right = newnode;
newnode->left = NULL;
newnode->right = NULL;
return(t);
}
else
cretree(t->right,x);
}
return(t);
}
void preorder(tree *t)
{
if( t != NULL)
{
printf("Preorder : %d\n",t->data);
}
else
{
printf("Empty Tree");
return;
}
if(t->left != NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
}


void inorder(tree *t)
{
if( t == NULL)
{
printf("Empty Tree");
return;
}
if(t->left != NULL)
inorder(t->left);

printf("Inorder : %d \n",t->data);

if(t->right != NULL)
inorder(t->right);
}

void postorder(tree *t)
{
if( t == NULL)
{
printf("Empty Tree");
return;
}
if(t->left != NULL)
postorder(t->left);


if(t->right != NULL)
postorder(t->right);

printf("Post Order : %d\n",t->data);
}

5 comments:

Anonymous said...

the program is not perfect from expression trversal point of view

Anonymous said...

viagra logo viagra no prescription viagra suppliers india viagra cialis vicodin viagra online uk viagra liver damage viagra prescription uk viagra dosages cheapest uk supplier viagra viagra results viagra for cheap Buy Viagra Online No Prescription recreational viagra use non prescription viagra

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...

Programme very easy ,,,othern than..

Anonymous said...

cheap xanax cheap xanax from canada - over counter pills look like xanax