Solution:
#include<stdio.h>
#include<stdlib.h>
struct binary_tree
{
int info;
struct binary_tree *left;
struct binary_tree *right;
};
typedef struct binary_tree NODE;
NODE *node=NULL;
NODE* init_tree(int data)
{
NODE* root = (NODE*) malloc (sizeof(NODE));
root->left = root->right = NULL;
root->info = data;
return root;
}
NODE* create_node(int data)
{
NODE* node = (NODE*) malloc (sizeof(NODE));
node->info = data;
node->left = node->right = NULL;
return node;
}
void inorder(NODE *node)
{
if(node != NULL)
{
inorder(node->left);
printf("%d\t", node->info);
inorder(node->right);
}
}
void preorder(NODE *node)
{
if(node != NULL)
{
printf("%d\t", node->info);
preorder(node->left);
preorder(node->right);
}
}
void postorder(NODE *node)
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d\t", node->info);
}
}
void main()
{
NODE* root = init_tree(10);
root->left = create_node(20);
root->right = create_node(30);
root->left->left = create_node(40);
root->left->right = create_node(50);
root->right->left = create_node(60);
root->right->right = create_node(70);
printf("----Preorder Traversal:----\n");
preorder(root);
printf("\n\n");
printf("----Inorder Traversal:----\n");
inorder(root);
printf("\n\n");
printf("----Postorder Traversal:----\n");
postorder(root);
printf("\n\n");
}
0 Comments