/*Serial No.146 [swami128.cpp]*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
struct node *lc;
int info;
struct node *rc;
}node;
typedef struct pipe
{
node *info;
struct pipe *next;
}pipe;
void create_tree(node **head)
{
node *temp,*q=*head, *par;
temp=(node*)malloc(sizeof(node));
printf("\nEnter the no to insert= ");
scanf("%d",&temp->info);
temp->lc=NULL;
temp->rc=NULL;
if(q==NULL)
{
*head=temp;
return;
}
else
{
while(q!=NULL)
{
if(temp->info<q->info)
{
par=q;
q=q->lc;
}
else
{
par=q;
q=q->rc;
}
}
if(temp->info<par->info)
par->lc=temp;
else
par->rc=temp;
}
}
void postorder_traversel(node *head)
{
node *q=head;
if(head==NULL)
{
return;
}
else
{
postorder_traversel(q->lc);
postorder_traversel(q->rc);
printf("%d ",q->info);
}
}
void inorder_traversel(node *head)
{
node *q=head;
if(head==NULL)
{
return;
}
else
{
inorder_traversel(q->lc);
printf("%d ",q->info);
inorder_traversel(q->rc);
}
}
void preorder_traversel(node *head)
{
node *q=head;
if(head==NULL)
{
return;
}
else
{
printf("%d ",q->info);
preorder_traversel(q->lc);
preorder_traversel(q->rc);
}
}
void enqueue(node *p,pipe **Queue) //*used in levelorder*//
{
pipe *t=*Queue,*temp;
temp=(pipe*)malloc(sizeof(pipe));
temp->info=p;
temp->next=NULL;
if(t==NULL)
*Queue=temp;
else
{
while(t->next!=NULL)
{
t=t->next;
}
t->next=temp;
}
}
node* dequeue(pipe **Queue) //*used in levelorder*//
{
pipe *s=*Queue;
*Queue=s->next;
return(s->info);
}
void levelorder_traversel(node *head)
{
node *q=head;
pipe *Q=NULL;
if(head==NULL)
{
return;
}
else
{
enqueue(q,&Q);
while(Q!=NULL)
{
q=dequeue(&Q);
printf("%d ",q->info);
if(q->lc!=NULL)
{
enqueue(q->lc,&Q);
}
if(q->rc!=NULL)
{
enqueue(q->rc,&Q);
}
}
}
}
void search_tree(node *head)
{
int ele,flag=0;
if(head==NULL)
{
printf("\nTree is Empty.");
return;
}
printf("\nEnter the no. to search= ");
scanf("%d",&ele);
while(head!=NULL)
{
if(head->info==ele)
{
flag=1;
break;
}
if(ele<head->info)
head=head->lc;
else
head=head->rc;
}
if(flag==1)
printf("\n%d is found.",ele);
else
printf("\n%d is not found",ele);
}
void main()
{
int choice;
node *root=NULL;
clrscr();
do
{
printf("\n\t[1] Create\Insert tree element");
printf("\n\t[2] Preorder Traversal");
printf("\n\t[3] Postorder Traversal");
printf("\n\t[4] Inorder Traversal");
printf("\n\t[5] Levelorder Traversal");
printf("\n\t[6] Search");
printf("\n\t[7] Exit");
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: create_tree(&root);
break;
case 2: if(root==NULL)
printf("\nTree is Empty.");
else
preorder_traversel(root);
break;
case 3: if(root==NULL)
printf("\nTree is Empty.");
else
postorder_traversel(root);
break;
case 4: if(root==NULL)
printf("\nTree is Empty.");
else
inorder_traversel(root);
break;
case 5: if(root==NULL)
printf("\nTree is Empty.");
else
levelorder_traversel(root);
break;
case 6: if(root==NULL)
printf("\nTree is Empty.");
else
search_tree(root);
break;
}
}while(choice!=7);
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...