1

So, I have a tree data structure which should work fine but now I want to print the results. My problem is if the parent node has two children (right and left) which aren't null; what path should it take? If it takes lets say right then all of the left ones after that node get left out?

I want the tree to be in the correct order

Example of what it'll do if I do not order it correctly:

-----------------------------root---------------------------------------
-------------------value---------------skips this value-----------------
-------------value-------skips this value-- skips this value -- skips this value
----------value -------skips this value----- skips this value ---- skips this value

What I'd like it to do:

-----------------------------------root----------------------------------

---------------------------value-----------value-------------------------
----------------------value----value-----value---value-------------------
--------------------value--value--value-value-value-value-value

and so on(I'd continue on but it gets cluttered)

I'll need to place my nodes into a stack and order them in advance. Suggestions?

Zain
  • 1,246
  • 4
  • 15
  • 26
  • 1
    http://stackoverflow.com/questions/443695/traversing-a-tree-of-objects-in-c-sharp – David Brabant Mar 03 '14 at 11:59
  • @DavidBrabant That doesn't resolve my problem – Zain Mar 03 '14 at 12:01
  • Your problem is tree traversal problem. Have a look at http://en.wikipedia.org/wiki/Tree_traversal – anonymous Mar 03 '14 at 12:04
  • Why not @user3245390? – rene Mar 03 '14 at 12:05
  • @anonymous So should I make this method traverse like addNode method? – Zain Mar 03 '14 at 12:08
  • @rene does the op not explain my issue clearly (genuine question)? The link he posted is for just printing the childs, not printing them under the correct parent and so on – Zain Mar 03 '14 at 12:09
  • Assuming your binary tree is correctly structured, to print things in-order you need to implement an inorder traversal. The wiki page even has both recursive and iterative pseudo code on how to perform inorder traversal. – anonymous Mar 03 '14 at 12:11
  • @user3245390 For me you don't. If you add a couple of nodes as an example, curent output and expected output it would help me to understand. – rene Mar 03 '14 at 12:16
  • @rene Updated the OP, it doesn't look too great but I'm not sure how to draw it with text.. – Zain Mar 03 '14 at 12:22
  • @anonymous Thank you v much, I'll check the wiki out now! – Zain Mar 03 '14 at 12:24
  • @user3245390 you'll get an upvote from me for creativity – rene Mar 03 '14 at 12:25
  • @rene You should see my original print method. It's v similar to the example I put in the OP. (the - are replaced with tabs, and it's centered to begin with). @ everyone I read the wiki and I think recursion would be the only way to do this gonna write some pseudo code and see if I can come up with anything. Bumping this thread as pointers towards the correct direction would be nice. If OP needs updating lemme know peeps! – Zain Mar 04 '14 at 10:40

0 Answers0