August 27, 2024

P66 - Layout a binary tree (3).

Binary tree

Yet another layout strategy is shown in the illustration opposite.  The method yields a very compact layout while maintaining a certain symmetry in every node.  Find out the rules and write the corresponding method. 

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.  Note:  This is a difficult problem.  Don’t give up too early!

scala

scala> Node('a', Node('b', End, Node('c')), Node('d')).layoutBinaryTree3
res0: PositionedNode[Char] = T[2,1]('a T[1,2]('b . T[2,3]('c . .)) T[3,2]('d . .))

Which layout do you like most?

Be first to comment
Leave a reply