It could help you if you looked at the answers given to a similar question; they contain links to software doing exactly the kind of tree visualization that you want.
Aesthetics is highly subjective, so this is just my opinion. I think my guidelines (not an algorithm) would be the following. I am assuming that the order of children is important (as these are binary search trees).
Only
x
coordinates are interesting;y
coordinates should only be determined by the node's level. (I would find it rather ugly if this was violated but, as I said, tastes differ. However, the rest is based on this assumption.)No nodes in the same level should be closer than some fixed minimum distance (say
D
).If a node has two children at
x1
andx2
, I would prefer it to be placed at(x1+x2)/2
. In some cases, it would be preferable to select some other coordinate in[x1..x2]
(possibly one of its ends). I guess there could be unusual cases where a coordinate outside[x1..x2]
would be preferable.If a node has one child at
x1
and its parent is atxp
, I would prefer it to be placed at(x1+xp)/2
(so that it lies on the line connecting its parent to its child). In some cases, it would be preferable to deviate from this and select some other coordinate in[xp..x1]
(or even outside).Let's call width of a level the distance between the leftmost and the rightmost node. The width of the widest level should be minimal.
These guidelines impose constraints that cannot be satisfied all at the same time. Therefore, you should prioritize them, and this is again subjective. E.g., what's more important, #4 or #5? Your sketch for the 5-node tree implies that #4 is more important; if #5 was more important you'd get a house-like picture (vertical lines); if both were important, then your current result would be fine.
One way to tackle this is by assigning weights to the guidelines and define penalties if these are not followed. E.g., in guideline #3, you could and penalize with abs(x-(x1+x2)/2)
if a parent is placed at x
which is not halfway between its children; you could also assign a weight that tells you how important this is, in comparison with other guidelines. You should then try to minimize the total weighted penalty of the solution. In general, this would give you a constraint optimization problem and there are several ways to solve such problems.