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

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Fri Feb 20 09:20:42 EST 2009

Charlie Turner wrote:
> 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)

> 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.

Take your example, a-BT, and plug it (mentally) into your code.  Is it 
false?  No.  So you'll do the `else' branch of the cond.  Now, assume 
the function works correctly in the recursive calls.  In other words, 
write down what

  (inorder (node-left a-BT))    and
  (inorder (node-right a-BT))

would produce assuming inorder magically works as it is supposed to.

Now you have three pieces of data, the above results and (node-ssn bt), 
to work with.

How do you put them together to get the overall expected result?

David



Posted on the users mailing list.