#include <stdio.h>
#include <malloc.h>
struct btree
{
int data;
struct btree * leftchild;
struct btree * rightchild;
} ;
void insert( struct btree ** node, int data) ;
void inorder( struct btree * node) ;
void preorder( struct btree * node) ;
void postorder( struct btree * node) ;
int main( )
{
int arr [ ] = { 2 , 8 , 4 , 5 , 7 , 6 , 3 } ;
int index = 0 ;
struct btree * root = NULL, * newNode = NULL;
int length = sizeof ( arr) / sizeof ( int ) ;
for ( index = 0 ; index < length; index++ )
{
insert( & root, arr[ index] ) ;
}
printf ( "This is the output of inorder traversal :- \n " ) ; inorder( root) ;
printf ( "This is the output of inorder traversal :- \n " ) ; preorder( root) ;
printf ( "This is the output of inorder traversal :- \n " ) ; postorder( root) ;
return 0 ;
}
void insert( struct btree ** node, int data)
{
if ( * node == NULL)
{
* node
= ( struct btree
* ) malloc ( sizeof ( struct btree
) ) ; ( * node) -> data = data;
( * node) -> leftchild = NULL;
( * node) -> rightchild = NULL;
}
else
{
if ( data > ( ( * node) -> data) )
insert( & ( ( ( * node) -> rightchild) ) , data) ;
else
insert( & ( ( ( * node) -> leftchild) ) , data) ;
}
}
void inorder( struct btree * node)
{
if ( node != NULL)
{
inorder( node-> leftchild) ;
inorder( node-> rightchild) ;
}
}
void preorder( struct btree * node)
{
if ( node != NULL)
{
inorder( node-> leftchild) ;
inorder( node-> rightchild) ;
}
}
void postorder( struct btree * node)
{
if ( node != NULL)
{
inorder( node-> leftchild) ;
inorder( node-> rightchild) ;
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KCnN0cnVjdCBidHJlZQp7CmludCBkYXRhOwpzdHJ1Y3QgYnRyZWUgKmxlZnRjaGlsZDsKc3RydWN0IGJ0cmVlICpyaWdodGNoaWxkOwp9OwoKCnZvaWQgaW5zZXJ0KHN0cnVjdCBidHJlZSAqKm5vZGUsIGludCBkYXRhKTsKdm9pZCBpbm9yZGVyKHN0cnVjdCBidHJlZSAqbm9kZSk7CnZvaWQgcHJlb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