0

I have a tree data structure which stores information. How do I figure out the correct placement of each node i.e. x,y locations of the tree on the canvas, so that nothing overlaps and all sibling nodes are on the same level? My attempts achieve neither. Thank you very much. I had something like this in mind, to be displayed on the canvas.

      A
     / \
    B   C
  / | \  \
 D  E  F  G
           \
           H
Asher
  • 811
  • 3
  • 10
  • 19

2 Answers2

2

Rather than reinventing the wheel, you could simply use a library that already implements several layout algorithms.

Zest seems the best choice here.

Here you can find a brilliant article about it and especially this section about Layout Managers should help you (TreeLayoutAlgorithm).

Baz
  • 36,440
  • 11
  • 68
  • 94
0

If your tree implementation is solid, then you should be able to know how many children a node has. Based on that, you can calculate the spacing between the siblings when drawing. To post a drawing example would be overkill for this answer.

First, try it for a small tree structure, like the one in your question. If that works, make it generic for N tree nodes and levels. You can tweak the spacings in the drawing afterwards.

Georgian
  • 8,795
  • 8
  • 46
  • 87
  • Thank you for your answer :) I tried to design an algorithm on my own, however it didn't work out because all my elements are of different lengths. Zest seemed to solve that problem for me. – Asher Jul 29 '14 at 17:06
  • @Asher I'm glad it worked out. I thought you didn't want to use external libraries. Best of luck! – Georgian Jul 29 '14 at 20:42