Tree testing

/* trees */
# include
# include
struct node
{ struct node *left;
int num;
struct node *right;
};
main()
{
int ch,num;
struct node *start=NULL;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Insert into tree");
printf("\n\t\t2. Display tree in preorder fashion");
printf("\n\t\t3. Display tree in inorder fashion");
printf("\n\t\t4. Display tree in postorder fashion");
printf("\n\t\t5. Exit program");
printf("\n\t\Enter your choice(1-5)");
scanf("%d",&ch);

switch(ch)
{ case 1:
printf("\nEnter number to be inserted");
scanf("%d",&num);
insnode(&start,num);
break;
case 2:
preorder(start);
break;
case 3:
inorder(start);
break;
case 4:
postorder(start);
break;
case 5:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
insnode(struct node *p, int n)
{
if(p==NULL)
{
p=(struct node *) malloc(sizeof(struct node));
p->num=n;
p->left=NULL;
p->right=NULL;
}
else
{
if(n>(p->num))
insnode(p->right,n);
else
insnode(p->left,n);
}
}

preorder(struct node *p)
{
if(p==NULL)
return;
else
{
printf("\nNode:%d",p->num);
preorder(p->left);
preorder(p->right);
}
}

inorder(struct node *p)
{
if(p==NULL)
return;
else
{

inorder(p->left);
printf("\nNode:%d",p->num);
inorder(p->right);
}
}

postorder(struct node *p)
{
if(p==NULL)
return;
else
{
postorder(p->left);
postorder(p->right);
printf("\nNode:%d",p->num);
}
}

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!!!