This problem looks a bit homework-ish, so I will try not to reveal an answer outright, but also, I hope I don't come off too condescending.
What Kohath is talking about is the normal way we think of trees in CL. These are binary trees where the CAR is and the CDR are the two branches from an interior node. Only leaves hold values. You are looking to do something a bit different (perhaps, or perhaps you are confused).
Let me ask a question, how would you print this list: (A (B C) (D (E F)))? I am honestly not sure how this would be printed from your example. Better than hazarding guess, I will just ask and wait for a response.
- Code: Select all
(defun depth-first (start)
((null start) 0)
((listp (first start)) (setq start (first start))
(depth-first (first start)))
((not (eq nil (car start))) (print (car start))
(setq start (rest start))
(t "Go home")))
Looks like some clumsy assignments in there. There really is no reason for that on a problem like this.
My suggestion is to think this out in pseudo code first. Or, like I do, just pretend you are the computer walking along the tree. Develop a set of rules that you will follow at each point and see if it works for various inputs. Then you can sit down and start thinking about an algorithm that works for all inputs.