99 questions/Solutions/66

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::Treea->Tree(a,Pos)layoutt=t'where(l,t',r)=layoutAuxx11tx1=maximuml+1layoutAux::Int->Int->Treea->([Int],Tree(a,Pos),[Int])layoutAuxxyEmpty=([],Empty,[])layoutAuxxy(Branchalr)=(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)rsep=maximum(0:zipWith(+)lrrl)`div`2+1ll'=0:overlay(map(+sep)ll)(map(subtractsep)rl)rr'=0:overlay(map(+sep)rr)(map(subtractsep)lr)-- overlay xs ys = xs padded out to at least the length of ys-- using any extra elements of ysoverlay::[a]->[a]->[a]overlay[]ys=ysoverlayxs[]=xsoverlay(x:xs)(y:ys)=x:overlayxsys

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.

Here is another solution. The helper function tries to place a node at (x,y), taking account of the guards at level y. Building up the tree from left to right.

importData.Maybetree66t=fromJust$helper[]00thelpergsxy(Branch_t0t1)=ifisGuardedgsx-- are we prevented from placing a node at (x, y)?thenNothingelsecasehelper(guardsgs)(x-1)(y+1)t0of-- no, but can we also place t0 one unit to the left?Nothing->NothingJustt0'->placeNode(t0':guardsgs)xy1t0't1-- yes, now place node and t1, trying radius of 1helpergsxyEmpty=JustEmpty-- node with subtrees symmetrically placed, as in example pictureplaceNodegsxyrt0't1=casehelpergs(x+r)(y+1)t1ofNothing->placeNodegs(x+1)y(r+1)t0't1-- could not place t1 here, trying more radiusJustt1'->Just$Branch(x,y)t0't1'-- ok---- node with right subtree packed to the left. this also places the subtree at (0, 0)--placeNode gs x y r t0' t1 =-- case helper gs (x + r) (y + 1) t1 of-- Nothing -> placeNode gs x y (r + 1) t0' t1-- Just t1' -> Just $ Branch (x, y) t0' t1'isGuarded(Branch(x',y')__:gs)x=x<=x'isGuarded__=Falseguardsgs=concatMapchildrengswherechildren(Branch_t0t1)=[t1,t0]childrenEmpty=[]