views:

257

answers:

1

An n-ary tree is memorised in the following way:

(node (list-subtree-1) (list-subtree-2) ...)

As an example, the tree

 A
/ \
B  C
  / \
 D   E

is represented as follows: (A (B) (C (D) (E)))

Return the number of levels of a tree

The problem is that I am only allowed to use the following functions: null, car, cdr, equal, atom, numberp, cons, cadr, caddr, cond and arithmtic functions. Could anyone give me a function to return the levels of that kind of tree?

+1  A: 

I'll just give some hints:

  • The number of levels below and including the root node is dependent on the highest number of levels below and including any direct sub node of the root node.

  • The arity of the tree seems to be unknown/arbitrary, therefore you will need to find a way to store the depth of the currently deepest found subtree while reducing the number of sub nodes to be examined.

Svante