Task: transfer a server side binary tree to client.
I got this task in an interview. Is there any efficient way to do this?
I don’t understand the task very well myself.
This is what I came up with, but not sure about server to client trasfer. Any ideas?
void copyInOrder(TNode *orgTree, Tnode *& copyTree)
{
if(orgTree !=NULL){
//left side
TNode newLeftNode = cloneNode(orgTree->left_link);
copyTree->left_link = newLeftNode;
copyInOrder(orgTree->left_link, copyTree->left_link);
//right side
TNode newRightNode = cloneNode(orgTree->right_link);
copyTree->right_link = newRightNode;
copyInOrder(orgTree->right_link, copyTree->right_link);
}
}
Honestly, defining “efficient” would be a good first step. What is considered important? Network bandwidth? Server side computation involved? Client side computation involved?
Along the same lines, what is the data?
For example, if the data is integers and network bandwidth is important, you could do construct an array of ints from the tree, transfer that with minimal overhead, and then convert it back to a binary tree on the user’s end.
If server side load is the most important thing, you’d go a different route. If client side load, possibly yet another.
Its worth noting that, since this is an interview question, discussing with the interviewer what they find important for efficiency and how it affects the solution may be just as important as the actual solution you come up with.