<div>Dear Zeng,</div>
<div> </div>
<div>I am also new to Scheme/LISP but what I found was that recursive algorithms tend to make more sense in LISP than in C/C++/Java. This was my attemp at a binary tree ...</div>
<div> </div>
<div>
<p>(define null '())</p>
<p>; ---- node object ----<br>(define (make-node data)<br> (cons data (cons null null)))<br>; ---- node object setters ----<br>(define (set-data! node data) (set-car! node data))<br>(define (set-lnode! node lnode)(set-car! (cdr node) lnode))
<br>(define (set-rnode! node rnode)(set-cdr! (cdr node) rnode))<br>; ---- node object getters ----<br>(define (get-data node) (car node))<br>(define (get-lnode node)(cadr node))<br>(define (get-rnode node)(cddr node))<br>
; ---- node predicates ----<br>(define (leaf? node)<br> (if (null? (and (get-lnode node)<br> (get-rnode node)))<br> #t<br> #f))</p>
<p>(define (make-btree)<br> (let ((root null))<br> ; ---- data inserter ----<br> (define (insert! data)<br> (define (inserter cur-node)<br> (let ((new-node (make-node data)))<br> (cond ((null? cur-node)
<br> (set! root new-node))<br> ((> data (get-data cur-node))<br> (if (null? (get-rnode cur-node))<br> (set-rnode! cur-node new-node)<br> (inserter (get-rnode cur-node))))
<br> ((< data (get-data cur-node))<br> (if (null? (get-lnode cur-node)) <br> (set-lnode! cur-node new-node)<br> (inserter (get-lnode cur-node)))))))
<br> (inserter root))<br> ; ---- traversals ----<br> (define (pre-order-traversal)<br> (define (traverse node)<br> (if (not (null? node))<br> (begin<br> (display (get-data node))
<br> (traverse (get-lnode node))<br> (traverse (get-rnode node)))<br> (display "")))<br> (traverse root))</p>
<p> (define (post-order-traversal)<br> (define (traverse node)<br> (if (not (null? node))<br> (begin<br> (traverse (get-lnode node))<br> (traverse (get-rnode node))<br>
(display (get-data node)))<br> (display "")))<br> (traverse root))</p>
<p> (define (in-order-traversal)<br> (define (traverse node)<br> (if (not (null? node))<br> (begin<br> (traverse (get-lnode node))<br> (display (get-data node))<br> (traverse (get-rnode node)))
<br> (display "")))<br> (traverse root))</p>
<p> (define (dispatch op)<br> (cond ((eq? op 'insert) insert!)<br> ((eq? op 'root) root)<br> ((eq? op 'pre-order-traversal) (pre-order-traversal))<br> ((eq? op 'post-order-traversal) (post-order-traversal))
<br> ((eq? op 'in-order-traversal) (in-order-traversal))))<br> ; object interface<br> dispatch))</p>
<p>; export tree interface<br>(define (insert! tree data) ((tree 'insert) data))<br>(define (root tree) (tree 'root))<br>(define (pre-order-traversal tree) (tree 'pre-order-traversal))<br>(define (post-order-traversal tree) (tree 'post-order-traversal))
<br>(define (in-order-traversal tree) (tree 'in-order-traversal))<br><br>.... and to find the depth of a tree I use this</p>
<p>(define (depth tree)<br> (define (counter node)<br> (cond ((null? node) 0)<br> ((leaf? node) 1)<br> (else (+ 1 (max (counter (get-lnode node))<br> (counter (get-rnode node)))))))
<br> (counter (root tree)))</p>
<p>which I translated from this Haskell code</p>
<p>depth :: Tree -> Int<br>depth Empty = 0<br>depth (Leaf n) = 1<br>depth (Node l r) = 1 + max (depth l) (depth r)</p>
<p>Hope it helps</p>
<p>Joshua Ewulo</p></div>
<div><span class="gmail_quote">On 31/07/07, <b class="gmail_sendername">Marco Morazan</b> <<a href="mailto:morazanm@gmail.com">morazanm@gmail.com</a>> wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Dear Zeng,<br><br>May I suggest that you define, precisely, what you mean by a tree<br>first? This may be done by defining a BNF grammar for trees. Once you
<br>have a grammar for your trees, you can design code for performing your<br>favorite traversal of trees.<br><br>Let me get you started:<br><br><tree> --> empty<br><tree> --> ( ?????? )<br><br>Marco<br>
<br>On 7/31/07, Zeng Fucen <<a href="mailto:zengfucen@gmail.com">zengfucen@gmail.com</a>> wrote:<br>> I'm wrong!!!<br>><br>> That's a binary tree,,,should be defined like this:((4 2 5) 1 (6 3 7))<br>
><br>> 1<br>> |<br>> ---------------------<br>> 2 3<br>> | |<br>> ------------ ------------<br>> 4 5 6 7
<br>><br>> I want result of the traversal is : 1 2 3 4 5 6 7 ,,, how to?<br>><br>><br>> On 7月31日, 下午10时56分, "Hans Oesterholt-Dijkema" <<a href="mailto:hdn...@gawab.com">hdn...@gawab.com</a>>
<br>> wrote:<br>> > Why don't you define a tree like this<br>> ><br>> > (1 (2 (4 () ()) (5 () ())) (3 (6 () ()) (7 () ()))<br>> ><br>> > ?<br>> ><br>> > --Hans<br>> >
<br>> > Op 31/7/2007 schreef "Zeng Fucen" <<a href="mailto:zengfu...@gmail.com">zengfu...@gmail.com</a>>:<br>> ><br>> > >I'm a scheme novice,<br>> ><br>> > >I want to travel a binary tree, how to ? (depth first)
<br>> ><br>> > >Thanks for your tips , first!<br>> ><br>> > >I define a tree like this: (1 (2 4 5) (3 6 7))<br>> ><br>> > >_________________________________________________<br>
> > > For list-related administrative tasks:<br>> > > <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>> ><br>> > _________________________________________________
<br>> > For list-related administrative tasks:<br>> > <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>><br>> _________________________________________________
<br>> For list-related administrative tasks:<br>> <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>><br><br>_________________________________________________
<br>For list-related administrative tasks:<br><a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br><br></blockquote></div><br>