123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
Program for demonstration of Tree Operations - INSERTION . INORDER . PREORDER . POSTORDER TRAVERSAL

--------------------------------------------------------------------------------



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

struct node
{
struct node *left;
int data;
struct node *right;
} ;

void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();
printf("
Program for Tree Traversal Demo
@ Amit Mathur
<BR>);
printf("
Enter the number of terms you want to add to the tree.<BR>);
scanf("%d",&will);

/* Getting Input */
for(i=0;i<will;i++)
{
printf("
Enter the item");
scanf("%d",&num);
insert(&ptr,num);
}

getch();
printf("

INORDER TRAVERSAL
<BR>);
inorder(ptr);
getch();
printf("

PREORDER TRAVERSAL
<BR>);
preorder(ptr);
getch();
printf("

POSTORDER TRAVERSAL
<BR>);
postorder(ptr);
getch();
}



void insert(struct node **p,int num)
{


if((*p)==NULL)
{ printf("
Leaf node created.");
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{ if(num==(*p)->data)
{
printf("

REPEATED ENTRY ERROR
VALUE REJECTED
<BR>);
return;
}
if(num<(*p)->data)
{
printf("
Directed to left link.");
insert(&((*p)->left),num);
}
else
{
printf("
Directed to right link.");
insert(&((*p)->right),num);
}
}
return;
}


/* Tree Traversal Functions*/
////////////// INORDER (LDR) ////////////

void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("
Data :%d",p->data);
inorder(p->right);
}
else
return;
}

////////////// PREORDER (DLR) ////////////

void preorder(struct node *p)
{
if(p!=NULL)
{
printf("
Data :%d",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}


////////////// POSTORDER (LRD) ////////////

void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("
Data :%d",p->data);
}
else
return;
}

 

 

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved copyright 123ENG