#include
<assert.h>
#include
<stdio.h>
#include
<stdlib.h>
typedef
char DATA;
struct
node
{
DATA d;
struct
node *left;
struct
node *right;
};
typedef
struct node
NODE;
typedef
NODE *BTREE;
void
inorder(BTREE root)
{
if (root
!= NULL)
{
inorder(root->left);
printf("%c ",
root->d);
inorder(root->right);
}
}
void
preorder(BTREE root)
{
if (root
!= NULL)
{
printf("%c
", root->d);
preorder(root->left);
preorder(root->right);
}
}
void
postorder(BTREE root)
{
if (root
!= NULL)
{
postorder(root->left);
postorder(root->right);
printf("%c
", root->d);
}
}
BTREE new_node()
{
return
(malloc(sizeof(NODE)));
}
BTREE init_node(DATA
d1, BTREE p1, BTREE p2)
{
BTREE t;
t = new_node();
t->d = d1;
t->left = p1;
t->right = p2;
return
t;
}
BTREE create_tree(DATA
a[], int i, int
size)
{
if (i >=
size)
return
NULL;
else
return
(init_node(a[i],
create_tree(a, 2*i+1, size),
create_tree(a, 2*i+2, size)));
}
void
main()
{
int j =
0;
int i=0;
BTREE b_tree;
int n;
char arr[50];
clrscr();
printf("\n\n\t\t\tBINARY
TREE TRAVERSAL");
printf("\n\t\t\t~~~~~~
~~~~ ~~~~~~~~~");
printf("\n Enter
the no nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("\nenter
the node:");
scanf("%c",&arr[i]);
}
b_tree = (create_tree(arr,j,n));
printf("\nInorder
Traversal =>");
inorder (b_tree);
printf("\nPostorder
traversal =>");
postorder(b_tree);
printf("\nPreorder
Traversal =>");
preorder(b_tree);
putchar('\n');
getch();
}
No comments:
Post a Comment