Yet another layout strategy is shown in the illustration below:
The method yields a very compact layout while maintaining a certain symmetry in every node. Find out the rules and write the corresponding Prolog predicate. Hint: Consider the horizontal distance between a node and its successor nodes. How tight can you pack together two subtrees to construct the combined binary tree?
Use the same conventions as in problem P64 and P65 and test your predicate in an appropriate way. Note: This is a difficult problem. Don't give up too early!
layout :: Tree a -> Tree (a, Pos) layout t = t' where (l, t', r) = layoutAux x1 1 t x1 = maximum l + 1 layoutAux :: Int -> Int -> Tree a -> ([Int], Tree (a, Pos), [Int]) layoutAux x y Empty = (, Empty, ) layoutAux x y (Branch a l r) = (ll', Branch (a, (x,y)) l' r', rr') where (ll, l', lr) = layoutAux (x-sep) (y+1) l (rl, r', rr) = layoutAux (x+sep) (y+1) r sep = maximum (0:zipWith (+) lr rl) `div` 2 + 1 ll' = 0 : overlay (map (+sep) ll) (map (subtract sep) rl) rr' = 0 : overlay (map (+sep) rr) (map (subtract sep) lr) -- overlay xs ys = xs padded out to at least the length of ys -- using any extra elements of ys overlay :: [a] -> [a] -> [a] overlay  ys = ys overlay xs  = xs overlay (x:xs) (y:ys) = x : overlay xs ys
The auxiliary function is passed the x- and y-coordinates for the root of the subtree and the subtree itself. It returns
- a list of distances the laid-out tree extends to the left at each level,
- the subtree annotated with positions, and
- a list of distances the laid-out tree extends to the right at each level.
These distances are usually positive, but may be 0 or negative in the case of a skewed tree. To put two subtrees side by side, we must determine the least even separation so that they do not overlap on any level. Having determined the separation, we can compute the extents of the composite tree.
The definitions of layout and its auxiliary function use local recursion to compute the x-coordinates. This works because nothing else depends on these coordinates.