-
Notifications
You must be signed in to change notification settings - Fork 4
/
07-2-mark-depth.rkt
executable file
·121 lines (98 loc) · 3.17 KB
/
07-2-mark-depth.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname 07-2-mark-depth) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
;; bintree-depth
(require rackunit)
(require "extras.rkt")
(define-struct bintree (left data right))
;; A BinTreeOfX is either
;; -- empty
;; -- (make-bintree BinTreeOfX X BinTreeOfX)
;; This is just data, so there's no interpretation
;; Template:
;; bintree-fn : BinTreeofX -> ??
;; (define (bintree-fn tree)
;; (cond
;; [(empty? tree) ...]
;; [else (...
;; (bintree-fn (bintree-left tree))
;; (bintree-data tree)
;; (bintree-fn (bintree-right tree)))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Version 1: Using an invariant
;; mark-subtree : BinTreeOfX NonNegInt-> BinTreeOfNumber
;; GIVEN: a subtree stree of some tree, and a non-neg int n
;; WHERE: the subtree occurs at depth n in the tree
;; RETURNS: a tree the same shape as stree, but in which
;; each node is marked with its distance from the top of the tree
;; STRATEGY: Use template for BinTreeOfX on stree
(define (mark-subtree stree n)
(cond
[(empty? stree) empty]
[else (make-bintree
(mark-subtree (bintree-left stree) (+ n 1))
n
(mark-subtree (bintree-right stree) (+ n 1)))]))
;; mark-depth : BinTreeOfX -> BinTreeNumber
;; RETURNS: a bintree like the given one, except that each node is
;; replaced by its depth.
;; EXAMPLES: see below
;; STRATEGY: call a more general function
(define (mark-depth tree)
(mark-subtree tree 0))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Version 2: Using Generalization
;; mark-subtree : BinTreeOfX Number -> BinTreeOfNumber
;; RETURNS: a bintree like the given one, except that each node is
;; replaced by its depth STARTING FROM n
;; EXAMPLES: see below
;; STRATEGY: Use template for BinTreeOfX on stree
;; the code is exactly the same, but the purpose statement is different!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; tests
(begin-for-test
(check-equal?
(mark-depth
(make-bintree
(make-bintree
(make-bintree empty "foo" empty)
"bar"
empty)
"baz"
(make-bintree
(make-bintree empty "quux" empty)
"frob"
empty)))
(make-bintree
(make-bintree
(make-bintree empty 2 empty)
1
empty)
0
(make-bintree
(make-bintree empty 2 empty)
1
empty)))
(check-equal?
(mark-subtree
(make-bintree
(make-bintree
(make-bintree empty "foo" empty)
"bar"
empty)
"baz"
(make-bintree
(make-bintree empty "quux" empty)
"frob"
empty))
10)
(make-bintree
(make-bintree
(make-bintree empty 12 empty)
11
empty)
10
(make-bintree
(make-bintree empty 12 empty)
11
empty))))