#include<stdio.h>
#include<stdlib.h>
struct btree
{
int data;
struct btree * right;
struct btree * left;
};
struct btree * head = NULL;
struct btree * getNode(int data)
{
struct btree * temp =NULL;
temp = (struct btree*)malloc( sizeof(struct btree) );
temp -> left = NULL;
temp -> right = NULL;
temp -> data = data ;
printf("\n getNode :: temp -> data = %d", temp -> data );
return temp;
}
struct btree * insert(struct btree * root, int data)
{
if ( root == NULL )
{
head = getNode(data);
return head ;
}
else
{
printf("\n insert:: root is not null");
if ( data <= ( (root) -> data ) )
{
printf("\n LESS THAN ");
root -> left =
insert( ( (root) -> left ), data ) ;
}
if ( data > ( (root) -> data ) )
{
printf("\n MORE THAN ");
root -> right = insert( ( (root) ->right ) , data ) ;
}
return root ;
}
}
void preorder( struct btree * r )
{
if ( r != NULL )
{
printf("\t %d ", r-> data );
preorder( r -> left );
preorder( r -> right );
}
}
void postorder( struct btree * root )
{
if ( root != NULL )
{
postorder(root -> left) ;
postorder(root -> right);
printf("\t %d", root -> data );
}
}
void inorder(struct btree * root)
{
if ( root != NULL )
{
postorder( root -> left );
printf("\t %d", root -> data );
postorder( root -> right ) ;
}
}
int main()
{
struct btree * root = NULL;
printf("\n In main ");
printf("\n main: Adding nodes ");
root =
insert(head, 9) ;
root =
insert(root, 4);
root =
insert(root, 15);
root =
insert(root, 6);
root =
insert(root, 12);
root =
insert(root, 17);
root =
insert(root, 2);
printf("\n main:: nodes added ");
printf("\n main:: root -> data = %d", root -> data);
printf("\n main:: Pre order display");
printf("\n main:: head -> data = %d", head -> data);
printf("\n ");
preorder( root );
printf("\n");
printf("\n Inorder display");
inorder( root );
printf("\n");
printf("\n post order display");
postorder( root );
return 0;
}