-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path06-2-trees.rkt
executable file
·45 lines (39 loc) · 1008 Bytes
/
06-2-trees.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
;; 06-2-trees.rkt
(define-struct leaf (datum))
(define-struct node (lson rson))
;; A Tree is either
;; -- (make-leaf Number)
;; -- (make-node Tree Tree)
#|
tree-fn : Tree -> ???
(define (tree-fn t)
(cond
[(leaf? t) (... (leaf-datum t))]
[else (...
(tree-fn (node-lson t))
(tree-fn (node-rson t)))]))
|#
;; leaf-sum : Tree -> Number
;; STRATEGY: Use template for Tree on t
(define (leaf-sum t)
(cond
[(leaf? t) (leaf-datum t)]
[else (+
(leaf-sum (node-lson t))
(leaf-sum (node-rson t)))]))
;; leaf-max : Tree -> Number
;; STRATEGY: Use template for Tree on t
(define (leaf-max t)
(cond
[(leaf? t) (leaf-datum t)]
[else (max
(leaf-max (node-lson t))
(leaf-max (node-rson t)))]))
;; leaf-min : Tree -> Number
;; STRATEGY: Use template for Tree on t
(define (leaf-min t)
(cond
[(leaf? t) (leaf-datum t)]
[else (min
(leaf-min (node-lson t))
(leaf-min (node-rson t)))]))