August 27, 2024

P65 - Layout a binary tree (2).

An alternative layout method is depicted in the illustration opposite.  Find out the rules and write the corresponding method.  Hint: On a given level, the horizontal distance between neighboring nodes is constant.

Tree

Use the same conventions as in problem P64.

scala

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

The tree at right may be constructed with Tree.fromList(List('n', 'k', 'm', 'c', 'a', 'e', 'd', 'g', 'u', 'p', 'q')).  Use it to check your code.

Be first to comment
Leave a reply