#include <stdio.h>
#include <stdlib.h> //without this calloc can not be used
typedef struct node //tree structure definition
{
int data;
struct node *left;
struct node *right;
}branch; // here we have set it as branch
branch *createnode(int value) //createnode is the function to create a node of the tree and return it
{
branch *stick;
stick
=(branch
*)calloc(1,sizeof(branch
)); stick->data=value;
stick->left=NULL;
stick->right=NULL;
return stick;
}
void pre_order(branch *root)
{
if(root==NULL)
{return;}
pre_order(root->left);
pre_order(root->right);
}
void post_order(branch *root)
{
if(root==NULL)
{return;}
post_order(root->left);
post_order(root->right);
}
void in_order(branch *root)
{
if(root==NULL)
{return;}
in_order(root->left);
in_order(root->right);
}
//driver program
int main(void)
{
branch *root=createnode(2);
root->left=createnode(5);
root->right=createnode(1);
root->left->left=createnode(4);
root->left->right=createnode(9);
root->right->left=createnode(0);
root->right->right=createnode(6);
printf("the preorder representation is:"); pre_order(root);
printf("\n the in-order representation is:"); in_order(root);
printf("\n the post-order representation is:"); post_order(root);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4gLy93aXRob3V0IHRoaXMgY2FsbG9jIGNhbiBub3QgYmUgdXNlZAp0eXBlZGVmIHN0cnVjdCBub2RlIC8vdHJlZSBzdHJ1Y3R1cmUgZGVmaW5pdGlvbgp7CiBpbnQgZGF0YTsKIHN0cnVjdCBub2RlICpsZWZ0Owogc3RydWN0IG5vZGUgKnJpZ2h0Owp9YnJhbmNoOyAvLyBoZXJlIHdlIGhhdmUgc2V0IGl0IGFzIGJyYW5jaAoKYnJhbmNoICpjcmVhdGVub2RlKGludCB2YWx1ZSkgLy9jcmVhdGVub2RlIGlzIHRoZSBmdW5jdGlvbiB0byBjcmVhdGUgYSBub2RlIG9mIHRoZSB0cmVlIGFuZCByZXR1cm4gaXQKeyAgCiBicmFuY2ggKnN0aWNrOwogc3RpY2s9KGJyYW5jaCAqKWNhbGxvYygxLHNpemVvZihicmFuY2gpKTsKIHN0aWNrLT5kYXRhPXZhbHVlOwogc3Rp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