-2

How can I Flatten a tree (inorder traversal) for following Tree structure: https://gist.github.com/damadamdam/7b6364220b11871f2930

My expected answer is also attached with the gist.

  • 2
    What is the question? – Reimeus Mar 15 '13 at 21:11
  • There are 2 question: 1) How is the above code working? 2) What is the code to flattening the tree? – James Matthew Mar 15 '13 at 21:13
  • 1
    That's not how it works here. Try something, and if you have a problem, show us what you try and explain the problem you're facing. We won't do your homework. – JB Nizet Mar 15 '13 at 21:32
  • I tried understanding it, but couldn't. I could get what Function.java, FlattenTree.java, Triple.java are doing and to some extend Either.java, but I couldn't understand how is Tree.java working. I'm unable to understand how to traverse the tree in Left-Middle-Right Fashion. – James Matthew Mar 15 '13 at 21:42

1 Answers1

1

An Either contains a field of type A, or a field of type B, but never both. You can pass a Function to its ifLeft() method which will only be called if it contains an A. And you can pass a Function to its ifRight() method that will be called if it contains a B. SO, if you call both methods, one and only of the functions will be called.

Function is simply an interface that you can implement, and which transforms something into something else.

A Tree is either a single element, or a Triple of three Trees (being themselves either an element, or a Triple of three trees, etc.), forming a recursive data structure.

And a Triple has a left, a middle, and a right elements.

To traverse the tree, you should check if it contains a single element or a Triple. If it's a single element, the traversal is finished. If it's a Triple, then you should traverse the its left tree, its middle tree, and its right tree.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for the quick answer! I have written the traversal and edited it in the question , can you please verify , if my code is correct from my understanding of your answer. – James Matthew Mar 15 '13 at 22:11
  • Is this a take-home exam in progress? – Mel Nicholson Mar 15 '13 at 22:20
  • Your code does not compile, it doesn't respect the Java naming conventions, and it forgets the middle element, but it seems logically correct to me. You don't really embrace the functional design of ifLeft() and ifRight(). I would have created two functions: one to apply to a leaf tree, and another to apply to a node tree. – JB Nizet Mar 15 '13 at 22:23
  • JB Nizet: I have rewritten code with 2 function and better naming convention and I think this code should compile. Is it correct now? Mel: I gave this exam, now I want to learn the solution, can you tell me a better way, how can I learn it, if I am not able to understand it myself, after trying really really hard for 2 days. – James Matthew Mar 15 '13 at 22:36
  • To check if it's correct, you should test it by yourself. Create a tree, call the method on this tree, and check that it returns the expected list. It still doesn't respect the convention. Classes start with an uppercase letter. Methods start with a lowercase letter. And what I meant is that there should be two different function implementations. One which traverse a leaf tree. And another one which traverses a node tree. You don't need an if block in the traversal method, since ifLeft() and ifRight() do the test for you. Oh, and you're still skipping the middle element. – JB Nizet Mar 15 '13 at 22:39
  • I can't get how to access the middle element, can you help me on that? – James Matthew Mar 15 '13 at 23:26
  • Somebody was trying to get a job at Atlassian... – aroth Jul 06 '13 at 08:14