0
votes

I would appreciate a hand with this exercise. Exercise 310. Design the function inorder. It consumes a binary tree and produces the sequence of all the ssn numbers in the tree as they show up from left to right when looking at a tree drawing.Binary Tree

My solution simply reverses each left node with what is below the node and then access the right node. Although the sequence is correct the answer is not in the correct format. This is my code:

(define-struct no-info [])
(define NONE (make-no-info))

(define-struct node [ssn name left right])
; A BinaryTree (short: BT) is one of:
; – NONE
; – (make-node Number Symbol BT BT)

(define nine9 (make-node 99 "nine9" NONE NONE))
(define one0 (make-node 10 "one0" NONE NONE))
(define two4 (make-node 24 "two4" NONE NONE))
(define one5 (make-node 15 "one5" one0 two4))
(define seven7 (make-node 77 "seven7" NONE NONE))
(define nine5 (make-node 95 "nine5" NONE nine9))
(define two9 (make-node 29 "two9" one5 one8))
(define eight9 (make-node 89 "eight9" seven7 nine5))
(define six3 (make-node 63 "six3" two9 eight9))

; BT -> list
; produce the sequence of ssn numbers from left to right
(define (inorder bt)
  (cond
    [(no-info? bt) '()]
    [else (append (reverse (cons (node-ssn bt) (list (inorder (node-left bt)))))
                  (inorder (node-right bt)))]))

This is the result when I run (inorder six3) (list (list (list (list '() 10) 15 '() 24) 29) 63 (list '() 77) 89 '() 95 '() 99)

2

2 Answers

0
votes

There are many strange elements in your code. I expect inorder would return a list but still you are packing the result in a list (list (inorder (node-left bt)) before consing. It is the sole reason you get a bunch of nested lists.

Why the reverse? Every level in the tree you end up with a partial list which you reverse. The order is then determined on whether the tree has odd or even height.

Given that inorder recursions actually returns (10 15 24 29) and (77 89 95 99) you do (append (reverse (cons 63 (list '(10 15 24 29)))) '(10 15 24 29)) which would result in ((10 15 24 29) 63 77 89 95 99) but of course the recursions don't really return those lists since it also will make a nested list etc.

If you have two lists and one element and you need that one element to be in the middle you do (append (10 15 24 29) (list 63) (77 89 95 99))

Now there are a much better way to do this:

(define (tree->list tree (acc '()))
  (if (tree-null? tree)
      acc
      (tree->list (node-left tree)
                  (cons (node-ssn tree)
                        (tree->list (node-right tree) acc)))))

Before a procedure can be applied it's arguments need to be calculated thus (tree->list (node-right tree) acc) is done first. It will have (77 89 95 99) for your tree and it will be '() for a node with one ssn with two null nodes. Then the ssn is consed onto the right side. Then it will act as an accumulator for the left side recursion.

Since a list is made by using cons from the end to the beginning that is the order in which the procedure is doing it so there is no need for append``and certainly no need forreverse`.

0
votes

After doing some reading I came up with this:

; BT -> list
; produce the sequence of ssn numbers from left to right
(define (inorder bt)
  (cond
    [(no-info? bt) '()]
    [else (append (inorder (node-left bt)) (list (node-ssn bt))
                  (inorder (node-right bt)))]))

Not so elegant with the one term list. However it works well.

Or this:

; BT -> list
; produce the sequence of ssn numbers from left to right
(define (inorder bt)
  (cond
    [(no-info? bt) '()]
    [else (append (add-to-end (inorder (node-left bt)) (node-ssn bt))
                  (inorder (node-right bt)))]))

(define (add-to-end ls ssn)
  (cond
    [(empty? ls) (cons ssn '())]
    [else (cons (first ls) (add-to-end (rest ls) ssn))]))