If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields.

5 Problem 62B

Collect the nodes at a given level in a list
A node of a binary tree is at level N if the path from the root to the node has length N-1. The root node is at level 1. Write a predicate atlevel/3 to collect all nodes at a given level in a list.

6 Problem 63

<Problem description>

Example:
<example in lisp>
Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

7 Problem 64

Given a binary tree as the usual Prolog term t(X,L,R) (or nil). As a preparation for drawing the tree, a layout algorithm is required to determine the position of each node in a rectangular grid. Several layout methods are conceivable, one of them is shown in the illustration below:

In this layout strategy, the position of a node v is obtained by the following two rules:

x(v) is equal to the position of the node v in the inorder sequence

y(v) is equal to the depth of the node v in the tree

Write a function to annotate each node of the tree with a position, where (1,1) in the top left corner or the rectangle bounding the drawn tree.

The auxiliary function is passed the x-coordinate for the left-most node of the subtree, the y-coordinate for the root of the subtree, and the subtree itself.
It returns the subtree annotated with positions, plus the count of Branch nodes in the subtree.

8 Problem 65

An alternative layout method is depicted in the illustration below:

Find out the rules and write the corresponding function.
Hint: On a given level, the horizontal distance between neighboring nodes is constant.

Use the same conventions as in problem P64 and test your function in an appropriate way.

The auxiliary function is passed the x- and y-coordinates for the root of the subtree, the horizontal separation between the root and its child nodes, and the subtree itself.
It returns the subtree annotated with positions.

9 Problem 66

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!

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.