Program to show Tree Operations like INSERTION, INORDER, PREORDER, POSTORDER,TRAVERSAL




# include
# include
# include

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("Enter the number of Nodes you want to add to the tree.");
scanf("%d",&will);

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

getch();
printf("INORDER TRAVERSAL");
inorder(ptr);
getch();
printf("PREORDER TRAVERSAL");
preorder(ptr);
getch();
printf("POSTORDER TRAVERSAL");
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");
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;
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!