Finding the maximum number of child nodes in a tree

Posted by Jiminizer on Stack Overflow See other posts from Stack Overflow or by Jiminizer
Published on 2011-01-14T20:50:19Z Indexed on 2011/01/14 20:53 UTC
Read the original article Hit count: 474

Filed under:
|

First, I should make it clear that this is required for an academic project. I am trying to find the maximum number of child nodes for any node in a tree, using Common Lisp.

My current code is shown below - I'm not 100% on the logic of it, but I feel it should work, however it isn't giving me the required result.

(defun breadth (list y)
  (setf l y)
        (mapcar #'(lambda (element) 
              (when (listp element) 
         (when (> (breadth element (length element)) l) 
             (setf l (breadth element (length element)))
           ))) list)

l)

(defun max-breadth(list)
  (breadth list (length list))
  )

As an example, running

(max-breadth '(a ( (b (c d)) e) (f g (h i) j)))

should return 4.

Does anyone have any ideas where I'm going wrong? I suspect it's related to the second conditional, but I'm not sure.

© Stack Overflow or respective owner

Related posts about lisp

Related posts about common-lisp