4

I recently came across an interview question asked by Amazon :

Given a binary tree to be passed through a network. How to pass this tree in minimum space?


Ok, my 2 approaches to above question are:

  1. We can store the inorder of tree in an array along with preorder (or postorder or level-order) in another array and then pass the 2 arrays into the network. This consumes a lot of space. So rejecting it, I came up with other solution.

  2. We will pass each node of the tree level-wise along with some information about it's left and right child.

Method 2

Additional Information along with nodes:

if left child==NULL && right child==NULL pass 00 along with node

if left child!=NULL && right child==NULL pass 10 along with node

if left child==NULL && right child!=NULL pass 01 along with node

if left child!=NULL && right child!=NULL pass 11 along with node

Let us look at an example for 2nd method

enter image description here

Level Wise

  1. Pass node(2) and 11
  2. Pass node(7) and 11
  3. Pass node(5) and 11
  4. Pass node(2) and 00
  5. Pass node(6) and 11
  6. Pass node(9) and 10
  7. Pass node(5) and 00
  8. Pass node(11) and 00
  9. Pass node(4) and 00

If I am correct, then in this way, you can do reconstruction of the tree on the other side of the network also by checking the additional information easily and (pardon me if I am wrong then) you are using less memory because you are only passing node's value and additional information at a time instead of passing the whole tree.

Ok, now I have 2 questions:

  1. Is my Method 2 correct or are there any exceptional cases for that which I may have missed.
  2. Is there any more efficient way other than the 2 described to send the tree onto the network?
Community
  • 1
  • 1
Ayush
  • 2,608
  • 2
  • 22
  • 31
  • 3
    possible duplicate of [Efficient Array Storage for Binary Tree](http://stackoverflow.com/questions/2675756/efficient-array-storage-for-binary-tree) – JasonMArcher Jul 14 '14 at 02:28

1 Answers1

2

This is more of a kind of question you are not expected to come up with an exact solution. All u need is to reason out soundly.

A better and efficient approach for passing the binary tree to a network would be to send its preorder traversal alone. Since u might have read that using a preorer traversal alone (if we have the entire info of a tree) is sufficent to create a tree. You can pass the entire binary tree via a single preorder traversal. For eg . if ur tree is     1   /    \2      3 Then if u want to send this tree to a network do its preorder traversal as 1 2 # # 3 # #... store it an array and send it via packets preferably packet switching. what I have done is that for every null left or right child(i.e if its left or right child or both is not present), I have put a '#' sentinel.   Now the user on the other side of the network would decode it easily using a stack-based approach which u might have learnt in your basic data-structure course. The solution u gave is also correct and I dont think there is any corner case for it but it also is not very space efficient 1. First, whether u send a tree level by level or in 2 arrays as u said above , u will be passing them as packets on a network and not as entire arrays(so sending is not a big concern when size of a single packet has a limit. U can send it via multiple packets). But for getting the info about a node and its children , u would require additonal space to store ur answer which is an overhead .. 2. u would be doing a level order traversal which on an average is more space consuming than a stack based o(height) space appraoch. BTW, u seem to be preparing for amazon. All the best.
bud
  • 41
  • 4