#include<iostream>
using namespace std;
struct BTree{
int data;
BTree *left;
BTree *right;
};
BTree *root = NULL;
BTree *create_node(int item)
{
BTree *New = new BTree;
New->data = item;
New->left = NULL;
New->right = NULL;
return New;
}
void add_left_child(BTree *parent, BTree *leftchild)
{
parent->left = leftchild;
}
void add_right_child(BTree *parent, BTree *rightchild)
{
parent->right = rightchild;
}
BTree *create_tree()
{
BTree *two = create_node(2);
BTree *seven = create_node(7);
BTree *nine = create_node(9);
add_left_child(two, seven);
add_right_child(two, nine);
BTree *one = create_node(1);
BTree *six = create_node(6);
add_left_child(seven, one);
add_right_child(seven, six);
BTree *eight = create_node(8);
add_right_child(nine, eight);
BTree *five = create_node(5);
BTree *ten = create_node(10);
add_left_child(six, five);
add_right_child(six, ten);
BTree *three = create_node(3);
BTree *four = create_node(4);
add_left_child(eight, three);
add_right_child(eight, four);
return two;
}
void preorder(BTree *Node)
{
if(Node == NULL)return;
cout<<Node->data<<" ";
preorder(Node->left);
preorder(Node->right);
}
void inorder(BTree *Node)
{
if(Node == NULL)return;
inorder(Node->left);
cout<<Node->data<<" ";
inorder(Node->right);
}
void postorder(BTree *Node)
{
if(Node == NULL)return;
postorder(Node->left);
postorder(Node->right);
cout<<Node->data<<" ";
}
int main()
{
root = create_tree();
preorder(root);
cout<<endl;
inorder(root);
cout<<endl;
postorder(root);
cout<<endl;
}
I2luY2x1ZGU8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgQlRyZWV7CiAgICBpbnQgZGF0YTsKICAgIEJUcmVlICpsZWZ0OwogICAgQlRyZWUgKnJpZ2h0Owp9OwpCVHJlZSAqcm9vdCA9IE5VTEw7CgpCVHJlZSAqY3JlYXRlX25vZGUoaW50IGl0ZW0pCnsKICAgIEJUcmVlICpOZXcgPSBuZXcgQlRyZWU7CgogICAgTmV3LT5kYXRhID0gaXRlbTsKICAgIE5ldy0+bGVmdCA9IE5VTEw7CiAgICBOZXctPnJpZ2h0ID0gTl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