0

I want to draw binary tree in reverse order. For example I've 4 elements.I like to put them in leaf node then in next iteration I combine two of them until I've one root node. every parent have Identity which is used for creation of new parent. nodes contains any objects. Orders is not mandatory. and two nodes can be combined in next iteration and in last level these nodes can be in any order

Example is like this:

binary tree

This is just a example for understanding. Solution can be in any language or any algorithm for explanation.

afaq
  • 109
  • 3
  • 11
  • 3
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on his own. A good way to show this effort is to include a [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). Check the [intro tour](https://stackoverflow.com/tour) you were *supposed* to finish before posting, especially [How to Ask](http://stackoverflow.com/help/how-to-ask). – Prune Nov 07 '17 at 18:17
  • I don't need code btw, I can do it myself. I am trying to build logic with the help of other guys, if someone have idea. – afaq Nov 07 '17 at 18:20
  • I've tried and searched many ways but unable to find something relevant to my goal. if you are here to degrade someone then you should find some other place. may be I'm not the experienced person – afaq Nov 07 '17 at 18:22

1 Answers1

1

Use level order traversal and store the contents of each level in a string. After each level is complete, print the string.

This is the basics.

If you now want fancier output like the one you have above, you can look into better ways to space out the elements and maybe use a graphics library to display the nodes.

smac89
  • 39,374
  • 15
  • 132
  • 179
  • one more confusion is, I just want to combine nodes upward regardless of is it left or right. in example number have order. but my objects have no order – afaq Nov 08 '17 at 08:49
  • one way is use Tree functionality of python. https://stackoverflow.com/questions/2358045/how-can-i-implement-a-tree-in-python-are-there-any-built-in-data-structures-in this link may help you – Nafees Abbasi Nov 09 '17 at 10:02