0

consider I want to transfer a binary tree in a serialised way and I have only one string to pass that tree and I can use the help of only one traversal, is there any way I could do that?? - (was asked to me in Ebay)..

in other words can a binary tree be generated by using only one tree traversal string?? the interviewer also gave me a hint by saying i can use null in place where leaf is absent..

Madara
  • 150
  • 1
  • 8
  • i didnt quite understand the question. you would like to convert a binary tree object into a string? – Stian Standahl Jan 28 '13 at 07:06
  • 1
    I'm not sure I understand the question... Just go through the tree, probably depth-first, and serialize contents. Then on the other end, reconstruct the tree. – hyde Jan 28 '13 at 07:07
  • You don't get the question. This is an algorithmic, very restricted theoretic question and not a real-world problem. In theory, an arbitrary tree can only be encoded without ambiguities by two different traversal strings. – kutschkem Jan 28 '13 at 07:34
  • **Preorder.** Store it using array indexes. so it can be decoded easily at the receiving side. – Azodious Jan 28 '13 at 07:07
  • You can't uniquely decode a tree from its preorder traversal. What tree is represented by "a b c"? – templatetypedef Jan 28 '13 at 07:08
  • the string should be created using array indexes. – Azodious Jan 28 '13 at 07:16
  • nice try @ Azodious but not the way i wanted it... :) – Madara Jan 28 '13 at 07:36

3 Answers3

2

[EDIT] given that this seems to be some given task, not really a real-world problem, the answer didn't make sense. Second try:

Seeing that you can use null-values, you can indeed use preorder with null values.

a b null null c null null would be a tree that looks like (a (b) (c)), while (a (b (c)) ) would be coded as a b c null null null null (note that i gave each leaf two null children, maybe it works without that)

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • See, i can just make the tree complete by using null-leaves. A complete tree shouldn't be problematic to encode using just a single traversal. – kutschkem Jan 28 '13 at 07:32
2

http://www.seas.gwu.edu/~csci133/fall05/trees-fig4.jpg

please consider above tree in above image, if we want to pass a string using only 1 traversal we can by putting special symbol whenever there is no child for that perticular location.

now if we pass 1 2 4 7 $ $ $ 5 $ $ 3 $ 6 8 $ $ 9 $ $

                                        1
                                     /      \
                                   2        3
                                  /  \     / \
                                 4    5   $   6
                                / \  / \     / \
                               7   $ $  $    8   9
                              / \           /\  /\
                             $  $          $ $ $ $

here $ means the child is null so move to the next location of the order.

please correct me if im wrong and above is impossible....

Madara
  • 150
  • 1
  • 8
0

If you want to represent any object in string, byte[] or streams you could have a look at JSON serialization or XML serialization.

Stian Standahl
  • 2,506
  • 4
  • 33
  • 43