(define (val T) (car T) ) (define (left T) (car (cdr T)) ) (define (right T) (ca
ID: 3884486 • Letter: #
Question
(define (val T)
(car T)
)
(define (left T)
(car (cdr T))
)
(define (right T)
(car (cdr (cdr T)))
)
Explanation / Answer
; Problem 1
(define T
'(13
(5
(1 () ())
(8 ()
(9 () ())))
(22
(17 () ())
(25 () ()))))
; Define three auxiliary functions (left T),(right T) and (val T) which return the left sub tree, ; the right sub tree, and the value in the root of tree T, respectively.
(define (left L)
(List-ref L 1)
)
(Define (right L)
(List-ref L 2)
)
(Define (Val L)
(List-ref L 0)
)
(Define (n-nodes L)
(cond [(equal? (length L) 0) 0] [else (+ 1 (+ (n-nodes (left L)) (n-nodes (right L))))]
)
)
(Define (n-leaves L)
(cond [(equal? (length L) 0) 0]
[Else
(cond
[(equal? (+ (length (left L)) (length (right L))) 0) 1]
[else (+ (n-leaves (left L)) (n-leaves (right L)))]
)
]
)
)
(define (height L)
(cond [(equal? (length L) 0) 0]
[else (+ 1 (max (height (left L)) (height (right L))))]
)
)
(define (postorder L)
(cond [(equal? (length L) 0) ()]
[else (append (postorder (left L)) (postorder (right L))
(list (val L)))
]
)
)
; Problem 2 (define (member-bst? x L)
(cond [(equal? (length L) 0) #f]
[else
(cond [(equal? x (val L)) #t] [else (cond [(> x (val L)) (member-bst? x (right L))]
[else (member-bst? x (left L))]
)
]
)
]
)
)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.