#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *lc,*rc;
};
typedef struct tree T;
T *ROOT=NULL;
T* create(T *node,int info);
void displayInorder(T *node);
void displayPreorder(T *node);
void displayPostorder(T *node);
int main ()
{
int ch=0,x;
clrscr();
while(ch!=5)
{
printf("\n1. Enter\n2. Display-Inorder\n3. Display-Pre Order\n4. Display-Post Order\n5. Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter rhe element: ");
scanf("%d",&x);
ROOT=create(ROOT,x);
clrscr();
break;
case 2:
if(ROOT==NULL)
printf("Tree is Empty!");
else
displayInorder(ROOT);
break;
case 3:
if(ROOT==NULL)
printf("Tree is Empty!");
else
displayPreorder(ROOT);
break;
case 4:
if(ROOT==NULL)
printf("Tree is Empty!");
else
displayPostorder(ROOT);
break;
}
}
}
T* create(T *node,int info)
{
if(node==NULL)
{
node=(T*)malloc(sizeof(T));
node->lc=NULL;
node->rc=NULL;
node->data=info;
}
else if(node->data>info)
node->lc=create(node->lc,info);
else
node->rc=create(node->rc,info);
return node;
}
void displayInorder(T *node)
{
if(node!=NULL)
{
displayInorder(node->lc);
printf("%d ",node->data);
displayInorder(node->rc);
}
}
void displayPreorder(T *node)
{
if(node!=NULL)
{
printf("%d ",node->data);
displayPreorder(node->lc);
displayPreorder(node->rc);
}
}
void displayPostorder(T *node)
{
if(node!=NULL)
{
displayPostorder(node->lc);
displayPostorder(node->rc);
printf("%d ",node->data);
}
}
Related posts: