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