[plt-scheme] Help: I want to travel a binary tree?

From: manicmessiah at gmail.com (manicmessiah at gmail.com)
Date: Tue Jul 31 11:18:10 EDT 2007

On 7/30/07, Zeng Fucen <zengfucen at gmail.com> wrote:
>
> I'm a scheme novice,
>
> I want to travel a binary tree, how to ? (depth first)
>
> Thanks for your tips , first!
>
> I define a tree like this:  (1 (2 4 5) (3 6 7))
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>

The syntax for defining a list of any sort is either:
(list ...)
'(...) <- What's inside will NOT be evaluated.

Hans gave a nice example:

"(1 (2 (4 () ()) (5 () ())) (3 (6 () ()) (7 () ()))"

You don't *need* it, but it makes more sense that way.

(define (travel-tree l)
   (cond ((null? l) ())
            ((list? l) (travel-tree (car l)))
            (t (travel-tree (cdr l))))

The above method _should_ work, but I can't guarantee it because I have my
own problems.
I assume that it'll be tail-recursive, considering that it will not evaluate
the rest of the condition if it succeeds.

All in all, I hope this helps.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070731/aaa918b9/attachment.html>

Posted on the users mailing list.