/*PROGRAM TO TRAVERSE THE BINARY TREE*/
#include<stdio.h>
#include<malloc.h>
struct tree
{
int Info;
struct tree *l_child;
struct tree *r_child;
};
struct tree *create_tree (int , struct tree *);
void display(struct tree *, int );
void pre_order(struct tree *);
void in_order(struct tree *);
void post_order(struct tree *);
#include<malloc.h>
struct tree
{
int Info;
struct tree *l_child;
struct tree *r_child;
};
struct tree *create_tree (int , struct tree *);
void display(struct tree *, int );
void pre_order(struct tree *);
void in_order(struct tree *);
void post_order(struct tree *);
/* Function to create a tree */
struct tree * create_tree (int item, struct tree *node)
{
if (node == NULL)
{
node = (struct tree *) malloc( sizeof(struct tree ));
node->Info = item;
node->l_child = NULL;
node->r_child = NULL;
return (node);
}
if (item < node->Info)
struct tree * create_tree (int item, struct tree *node)
{
if (node == NULL)
{
node = (struct tree *) malloc( sizeof(struct tree ));
node->Info = item;
node->l_child = NULL;
node->r_child = NULL;
return (node);
}
if (item < node->Info)
/* Smaller item becomes left child */
node->l_child = create_tree (item, node->l_child);
else
node->l_child = create_tree (item, node->l_child);
else
/* Greater or equal item becomes right child */
node->r_child = create_tree (item, node->r_child);
return(node);
}
node->r_child = create_tree (item, node->r_child);
return(node);
}
/* Function to display tree */
void display(struct tree *T, int level)
{
int i;
if (T)
{
display(T->r_child, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf("\t");
printf("|----%d", T->Info);
printf("\n");
display(T->l_child, level+1);
}
}/* Function to perform Pre-order traversal */
void pre_order (struct tree *node)
{
if (node)
{
printf(" %d", node->Info);
pre_order(node->l_child);
pre_order(node->r_child);
}
}
void display(struct tree *T, int level)
{
int i;
if (T)
{
display(T->r_child, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf("\t");
printf("|----%d", T->Info);
printf("\n");
display(T->l_child, level+1);
}
}/* Function to perform Pre-order traversal */
void pre_order (struct tree *node)
{
if (node)
{
printf(" %d", node->Info);
pre_order(node->l_child);
pre_order(node->r_child);
}
}
/* Function to perform In-order traversal */
void in_order (struct tree *node)
{
if (node)
{
in_order(node->l_child);
printf(" %d", node->Info);
in_order(node->r_child);
}
}/* Function to perform Post-order traversal */
void post_order (struct tree *node)
{
if (node)
{
post_order(node->l_child);
post_order(node->r_child);
printf(" %d", node->Info);
}
}
void in_order (struct tree *node)
{
if (node)
{
in_order(node->l_child);
printf(" %d", node->Info);
in_order(node->r_child);
}
}/* Function to perform Post-order traversal */
void post_order (struct tree *node)
{
if (node)
{
post_order(node->l_child);
post_order(node->r_child);
printf(" %d", node->Info);
}
}
/* Main Function */
void main()
{
int n,item,i=0;
struct tree *T = (struct tree *) malloc(sizeof(struct tree *));
clrscr();
T = NULL;
printf("\n\n\n -------------------------------------");
printf("\n How many nodes you want to create:");
scanf("%d",&n);
while(i != n)
{
printf("\n\n ----------------------------------");
printf("\n Enter item you want to insert: ");
scanf("%d", &item);
T = create_tree (item, T);
printf("\n\n\t----------");
printf("\n\t Tree is: ");
printf("\n\t----------\n\n");
display(T, 1);
i++;
}
printf("\n\n==========================");
printf("\n\n\n\n\t -------------------");
printf("\n\t Pre-order traversal");
printf("\n\t -------------------\n\n\t");
pre_order (T);
printf("\n\n\n\t -------------------");
printf("\n\t In-order traversal");
printf("\n\t -------------------\n\n\t");
in_order (T);
printf("\n\n\n\t -------------------");
printf("\n\t Post-order traversal");
printf("\n\t -------------------\n\n\t");
post_order (T);
getch();
}
void main()
{
int n,item,i=0;
struct tree *T = (struct tree *) malloc(sizeof(struct tree *));
clrscr();
T = NULL;
printf("\n\n\n -------------------------------------");
printf("\n How many nodes you want to create:");
scanf("%d",&n);
while(i != n)
{
printf("\n\n ----------------------------------");
printf("\n Enter item you want to insert: ");
scanf("%d", &item);
T = create_tree (item, T);
printf("\n\n\t----------");
printf("\n\t Tree is: ");
printf("\n\t----------\n\n");
display(T, 1);
i++;
}
printf("\n\n==========================");
printf("\n\n\n\n\t -------------------");
printf("\n\t Pre-order traversal");
printf("\n\t -------------------\n\n\t");
pre_order (T);
printf("\n\n\n\t -------------------");
printf("\n\t In-order traversal");
printf("\n\t -------------------\n\n\t");
in_order (T);
printf("\n\n\n\t -------------------");
printf("\n\t Post-order traversal");
printf("\n\t -------------------\n\n\t");
post_order (T);
getch();
}
No comments:
Post a Comment