#include<iostream>
#include<queue>
using namespace std;
class node
{
int data;
int no_of_children;
node* left;
node* right;
public:
void set_data(int a)
{
data=a;
}
int get_data()
{
return data;
}
void set_left_node(node* anode)
{
left=anode;
}
void set_right_node(node* anode)
{
right=anode;
}
node* get_left_node()
{
return left;
}
node* get_right_node()
{
return right;
}
void set_no_of_children(int a)
{
if(a<0 || a>2)
return;
no_of_children=a;
}
int get_no_of_children()
{
return no_of_children;
}
};
class tree
{
node* root;
public:
void construct_tree()
{
queue <node*>q;
root=new node;
q.push(root);
int d;
cout<<"Enter the data of the root node: ";
cin>>d;
(q.front())->set_data(d);
while(!q.empty())
{
int no_of_children;
cout<<"Enter the number of children of "<<q.front()->get_data()<<": ";
cin>>no_of_children;
q.front()->set_no_of_children(no_of_children);
for(int i=0;i<(q.front()->get_no_of_children());i++)
{
node* temp=new node;
if(i==0)
{
int lc;
cout<<"Enter the left child of: "<<q.front()->get_data()<<": ";
cin>>lc;
temp->set_data(lc);
q.front()->set_left_node(temp);
q.push(temp);
}
else if(i==1)
{
int rc;
cout<<"Enter the right child of: "<<q.front()->get_data()<<": ";
cin>>rc;
temp->set_data(rc);
q.front()->set_right_node(temp);
q.push(temp);
}
}
if(q.front()->get_no_of_children()==0)
{
q.front()->set_left_node(0);
q.front()->set_right_node(0);
}
q.pop();
}
}
void in_order_print()
{
}
};
int main()
{
tree t;
t.construct_tree();
t.in_order_print();
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHF1ZXVlPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3Mgbm9kZQp7CiAgICBpbnQgZGF0YTsKICAgIGludCBub19vZl9jaGlsZHJlbjsKICAgIG5vZGUqIGxlZnQ7CiAgICBub2RlKiByaWdodDsKCnB1YmxpYzoKICAgIHZvaWQgc2V0X2RhdGEoaW50IGEpCiAgICB7CiAgICAgICAgICAgIGRhdGE9YTsKICAgIH0KCiAgICBpbnQgZ2V0X2RhdGEoKQogICAgewogICAgICAgIHJldHVybiBkYXRhOwogICAgfQoKICAgIHZvaWQgc2V0X2xlZnRfbm9kZShub2RlKiBhbm9kZSkKICAgIHsKICAgICAgICBsZWZ0PWFub2RlOwogICAgfQoKICAgIHZvaWQgc2V0X3JpZ2h0X25vZGUobm9kZSogYW5vZGUpCiAgICB7CiAgICAgICAgcmlnaHQ9YW5vZGU7CiAgICB9CgogICAgbm9kZSogZ2V0X2xlZnRfbm9kZSgpCiAgICB7CiAgICAgICAgcmV0dXJuIGxlZnQ7CiAgICB9CgogICAgbm9kZSogZ2V0X3JpZ2h0X25vZGUoKQogICAgewogICAgICAgIHJldHVybiByaWdodDsKICAgIH0KCiAgICB2b2lkIHNldF9ub19vZl9jaGlsZHJlbihpbnQgYSkKICAgIHsKICAgICAgICBpZihhPDAgfHwgYT4yKQogICAgICAgICAgICByZXR1cm47CgogICAgICAgIG5vX29mX2NoaWxkcmVuPWE7CiAgICB9CgogICAgaW50IGdldF9ub19vZl9jaGlsZHJlbigpCiAgICB7CiAgICAgICAgcmV0dXJuIG5vX29mX2NoaWxkcmVuOwogICAgfQp9OwoKY2xhc3MgdHJlZQp7CiAgICBub2RlKiByb290OwoKICAgIHB1YmxpYzoKICAgICAgICB2b2lkIGNvbnN0cnVjdF90cmVlKCkKICAgICAgICB7CiAgICAgICAgICAgIHF1ZXVlIDxub2RlKj5xOwogICAgICAgICAgICByb290PW5ldyBub2RlOwogICAgICAgICAgICBxLnB1c2gocm9vdCk7CiAgICAgICAgICAgIGludCBkOwogICAgICAgICAgICBjb3V0PDwiRW50ZXIgdGhlIGRhdGEgb2YgdGhlIHJvb3Qgbm9kZTogIjsKICAgICAgICAgICAgY2luPj5kOwogICAgICAgICAgICAocS5mcm9udCgpKS0+c2V0X2RhdGEoZCk7CgogICAgICAgICAgICB3aGlsZSghcS5lbXB0eSgpKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpbnQgbm9fb2ZfY2hpbGRyZW47CiAgICAgICAgICAgICAgICBjb3V0PDwiRW50ZXIgdGhlIG51bWJlciBvZiBjaGlsZHJlbiBvZiAiPDxxLmZyb250KCktPmdldF9kYXRhKCk8PCI6ICI7CiAgICAgICAgICAgICAgICBjaW4+Pm5vX29mX2NoaWxkcmVuOwogICAgICAgICAgICAgICAgcS5mcm9udCgpLT5zZXRfbm9fb2ZfY2hpbGRyZW4obm9fb2ZfY2hpbGRyZW4pOwoKICAgICAgICAgICAgICAgIGZvcihpbnQgaT0wO2k8KHEuZnJvbnQoKS0+Z2V0X25vX29mX2NoaWxkcmVuKCkpO2krKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBub2RlKiB0ZW1wPW5ldyBub2RlOwogICAgICAgICAgICAgICAgICAgIGlmKGk9PTApCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpbnQgbGM7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PCJFbnRlciB0aGUgbGVmdCBjaGlsZCBvZjogIjw8cS5mcm9udCgpLT5nZXRfZGF0YSgpPDwiOiAiOwogICAgICAgICAgICAgICAgICAgICAgICBjaW4+PmxjOwogICAgICAgICAgICAgICAgICAgICAgICB0ZW1wLT5zZXRfZGF0YShsYyk7CiAgICAgICAgICAgICAgICAgICAgICAgIHEuZnJvbnQoKS0+c2V0X2xlZnRfbm9kZSh0ZW1wKTsKICAgICAgICAgICAgICAgICAgICAgICAgcS5wdXNoKHRlbXApOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKGk9PTEpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpbnQgcmM7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PCJFbnRlciB0aGUgcmlnaHQgY2hpbGQgb2Y6ICI8PHEuZnJvbnQoKS0+Z2V0X2RhdGEoKTw8IjogIjsKICAgICAgICAgICAgICAgICAgICAgICAgY2luPj5yYzsKICAgICAgICAgICAgICAgICAgICAgICAgdGVtcC0+c2V0X2RhdGEocmMpOwogICAgICAgICAgICAgICAgICAgICAgICBxLmZyb250KCktPnNldF9yaWdodF9ub2RlKHRlbXApOwogICAgICAgICAgICAgICAgICAgICAgICBxLnB1c2godGVtcCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIGlmKHEuZnJvbnQoKS0+Z2V0X25vX29mX2NoaWxkcmVuKCk9PTApCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgcS5mcm9udCgpLT5zZXRfbGVmdF9ub2RlKDApOwogICAgICAgICAgICAgICAgICAgIHEuZnJvbnQoKS0+c2V0X3JpZ2h0X25vZGUoMCk7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgcS5wb3AoKTsKICAgICAgICAgICAgfQoKICAgICAgICB9CgogICAgICAgIHZvaWQgaW5fb3JkZXJfcHJpbnQoKQogICAgICAgIHsKICAgICAgICAgICAgCiAgICAgICAgfQp9OwoKaW50IG1haW4oKQp7CiAgICB0cmVlIHQ7CiAgICB0LmNvbnN0cnVjdF90cmVlKCk7CiAgICB0LmluX29yZGVyX3ByaW50KCk7Cn0K