#include<stdio.h>
struct BTree
{
int Data;
struct BTree *Left;
struct BTree *Right;
}*Root,*Point;
main()
{
int a;
Menu:
printf("\n 1...Make Tree ");
printf("\n 2...In Order Traversel");
printf("\n 3...Pre Order Traversel");
printf("\n 4...Post Order Traversel");
printf("\n 5... Exit");
printf("\n\n\n Enter Your Choice ");
scanf("%d",&a);
switch(a)
{
case 1:
Make();
break;
case 2:
InOrder(Root);
break;
case 3:
PreOrder(Root);
break;
case 4:
PostOrder(Root);
break;
case 5:
exit(0);
break;
default:
printf("\n Invalid Choice ");
break;
}
goto Menu;
}
Make()
{
int a;
struct BTree *Node=(struct BTree*)malloc(sizeof(struct BTree));
struct BTree *Parent=(struct BTree*)malloc(sizeof(struct BTree));
Point=(struct BTree*)malloc(sizeof(struct BTree));
printf("\n Enter Data For Node :");
scanf("%d",&a);
if(Root==NULL)
{
Root=(struct BTree*)malloc(sizeof(struct BTree));
Root->Data=a;
Root->Left=NULL;
Root->Right=NULL;
}
else
{
Node->Data=a;
Node->Right=NULL;
Node->Left=NULL;
Point=Root;
while(Point!=NULL)
{
if(Point->Data > Node->Data )
{
Parent=Point;
Point=Point->Left;
}
else
{
Parent=Point;
Point=Point->Right;
}
}
if(Parent->Data>Node->Data)
Parent->Left=Node;
else
Parent->Right=Node;
}
}
InOrder(struct BTree *q)
{
if(q!=NULL)
{
InOrder(q->Left);
printf("\n Disp Data :%d",q->Data);
InOrder(q->Right);
}
}
PostOrder(struct BTree *q)
{
if(q!=NULL)
{
PostOrder(q->Left);
PostOrder(q->Right);
printf("\n Disp Data :%d",q->Data);
}
}
PreOrder(struct BTree *q)
{
if(q!=NULL)
{
printf("\n Disp Data :%d",q->Data);
PreOrder(q->Left);
PreOrder(q->Right);
}
}
No comments:
Post a Comment