pls explain the input format
Structurally Identical (Binary Tree)(explain input)
Input consists of a data value and the boolean variables true and false, true here means that there are children whereas false means that there are no children… try with this approach.
and how do we know we’ve to stop taking input for one tree?
If you will carefully analyse,
10 true 20 true 40 false false true 50 false false true 30 true 60 false false true 73 false false
Here 10 means it is root, true after it means that you have left child of 10 , data of tht is 20,
true again means you have left child of 20, which is 40, false means 40 doesnt have left child, again false means that 40 doesnt have right child, true means that 50 is the right child of 20, and like this your tree will be builded
ok i got that but how do we know that we’ve to stop taking input of one tree? do we have to maintain that root’s both child are now builded so there’e no input after that or there’s any other way?
You can maintain a string input, and check
if(str==“true”)
{
cin>>d;
node *temp=new node(d);
temp->left=buildtree(temp->left);
temp->right=buildtree(temp->right);
return temp;
}
else
if(str==“false”)
{
return root;
}
}