[plt-scheme] Excersise 14.2.3 - HtDP - Ordered list of binary tree nodes

From: Veer (diggerrrrr at gmail.com)
Date: Fri Feb 20 08:53:24 EST 2009

Hello,

See this http://en.wikipedia.org/wiki/Tree_traversal



On Thu, Feb 19, 2009 at 12:51 AM, Charlie Turner
<charlie.h.turner at googlemail.com> wrote:
> Hello,
>
> I don't understand how to attack this problem. I've made several
> attempts and it never works out.
>
> A node looks like this:
> (define-struct node (ssn name left right))
>
> The Question:
> Develop the function inorder. It consumes a binary tree and produces a
> list of all the ssn numbers in the tree. The list contains the numbers
> in the left-to-right order we have used above.
>
> The authors go on to hint at using APPEND, regardless... I still don't
> understand.
>
> Figure 38, tree A on p201 is what I'm working with, I've defined it's
> structure as:
> (a pictures of it is here->
> http://www.htdp.org/2003-09-26/icons/bst1.gif and my function should
> list the nodes as (list 10 15 24 29 63 77 89 95 99) "left-to-right"
> you see)
>
> (define a-BT   ;Figure 38 A
>  (make-node 63 'top
>    (make-node 29 't
>      (make-node 15 'd
>        (make-node 10 'e false false)
>        (make-node 24 'f false false))false)
>             (make-node 89 'g
>                        (make-node 77 'h false false)
>                        (make-node 95 'i false
>                                   (make-node 99 'j false false)))))
>
> The useless function I have which just lists nodes at the moment is as follows:
>
> (define (inorder bt)
>  (cond
>    [(false? bt) empty]
>    [else (cons (node-ssn bt) (append (inorder (node-left bt))
>                                                     (inorder
> (node-right bt))))])
>
>> (inorder a-BT)
> (list 63 29 15 10 24 89 77 95 99)
>
> As you'd expect, it's just a listing of all the nodes. I'm clueless as
> to how I can order them left-to-right.
>
> Any help on where to go from here would be greatly appriciated.
>
> I thank you for your time.
>
> (list 10 15 24 29 63 77 89 95 99)
>
> --
> Charlie Turner.
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.