-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_2_part9.rkt
291 lines (210 loc) · 6.57 KB
/
chapter_2_part9.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#lang sicp
;集合为二叉树
(define (entry tree)
(car tree));顶端数据项
(define (left-branch tree)
(cadr tree));小于顶端的左边分支
(define (right-branch tree)
(caddr tree));大于顶端的右边分支
(define (make-tree tree left right)
(list tree left right))
(define (element-of-set? x set)
(cond ((null? set)
'())
((= x (entry set))
true)
((< x (entry set))
(element-of-set? x (left-branch set)))
((> x (entry set))
(element-of-set? x (right-branch set)))))
(define (adjoin-set x set)
(cond ((null? set)
(make-tree x '() '()))
((= x (entry set))
set)
((< x (entry set))
(make-tree (entry set)
(adjoin-set x
(left-branch set));比较左侧小于顶端的分支
(right-branch set)))
((> x (entry set))
(make-tree (entry set)
(left-branch set)
(adjoin-set x
(right-branch set))))));比较右侧大于顶端的分支
;exercise-2.63
(define (tree->list-1 tree)
(if (null? tree)
'()
(append (tree->list-1 (left-branch tree))
(cons (entry tree)
(tree->list-1 (right-branch tree))))))
(define (tree->list-2 tree)
(define (copy-to-list tree result-list)
(if (null? tree)
result-list
(copy-to-list (left-branch tree)
(cons (entry tree)
(copy-to-list (right-branch tree)
result-list)))))
(copy-to-list tree '()))
(define a (make-tree 1 nil nil))
(define b (make-tree 5 nil nil))
(define e (make-tree 11 nil nil))
(define f (make-tree 9 nil e))
(define c (make-tree 3 a b))
(define trees (make-tree 7 c f))
(tree->list-1 trees)
(tree->list-2 trees)
;a)
;两个树产生的结果是一样的
;b)
;不是一样的增长速度,tree->list-1是递归,而tree->list->2是迭代.因此后面的增长速度慢一些
;exercise-2.64
(define (list->tree elements)
(car (partial-tree elements (length elements))))
(define (partial-tree elts n)
(if (= n 0)
(cons '() elts)
(let ((left-size (quotient (- n 1) 2)));quotient 实现N1除以N2,结果值为商
(let ((left-result (partial-tree elts left-size)))
(let ((left-tree (car left-result))
(non-left-elts (cdr left-result))
(right-size (- n (+ left-size 1))))
(let ((this-entry (car non-left-elts))
(right-result (partial-tree (cdr non-left-elts)
right-size)))
(let ((right-tree (car right-result))
(remianing-elts (cdr right-result)))
(cons (make-tree this-entry left-tree right-tree)
remianing-elts))))))))
#|
(1 3)(5 7 9 11) ; 分割左右子树
(5 7 9 11) ; 创建 1 节点
/
/
1(3)
(5 7 9 11) ; 创建 1 的左子树(空)
/
/
1(3)
/
/
'()
(5 7 9 11) ; 创建 1 的右子树(包含 3)
/
/
1
/ \
/ \
'() 3
5 (7 9 11) ; 创建树根 5
/
/
1
/ \
/ \
'() 3
5 ; 创建 9 节点
/ \
/ \
1 9 (7 11)
/ \
/ \
'() 3
5 ; 创建 9 的左子树(包含 7)
/\
/ \
/ \
/ \
1 9 (11)
/ \ /
/ \ /
'() 3 7
5 ; 创建 9 的右子树(包含 11)
/ \
/ \
/ \
/ \
1 9
/ \ / \
/ \ / \
'() 3 7 11
|#
;b)
;对于列表中的每个节点, list->tree 都要执行一次 make-tree (复杂度为 Θ(1) ),将这个节点和它的左右子树组合起来,因此对于长度为 n 的列表来说, list->tree 的复杂度为 对于列表中的每个节点, list->tree 都要执行一次 make-tree (复杂度为 Θ(1) ),将这个节点和它的左右子树组合起来,因此对于长度为 n 的列表来说, list->tree 的复杂度为 Θ(n) 。
;exercise-2.65
(define (union-set-ol set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
(else
(let ((x1 (car set1))
(x2 (car set2)))
(cond ((= x1 x2)
(cons x1 (union-set-ol (cdr set1) (cdr set2))))
((< x1 x2)
(cons x1 (union-set-ol (cdr set1) set2)))
((> x1 x2)
(cons x2 (union-set-ol set1 (cdr set2)))))))))
(define (union-set set1 set2)
(list->tree
(union-set-ol
(tree->list-2 set1)
(tree->list-2 set2))))
(define one (list->tree '(1 2 3 4 5)))
(define two (list->tree '(1 3 5 7 9)))
(union-set one two)
(tree->list-2 (union-set one two))
(define (intersection-set-ol set1 set2)
(if (or (null? set1) (null? set2))
nil
(let ((x1 (car set1))
(x2 (car set2)))
(cond ((= x1 x2)
(cons x1 (intersection-set-ol (cdr set1)
(cdr set2))))
((< x1 x2)
(intersection-set-ol (cdr set1) set2))
((> x1 x2)
(intersection-set-ol set1 (cdr set2)))))))
(define (intersection-set set1 set2)
(list->tree
(intersection-set-ol
(tree->list-2 set1)
(tree->list-2 set2))))
(intersection-set one two)
(tree->list-2 (intersection-set one two))
;;集合和信息检索
(define (equal? s1 s2)
(if (and (pair? s1) (pair? s2))
(and (equal? (car s1) (car s2))
(equal? (cdr s1) (cdr s2)))
(eq? s1 s2)))
(define (key records)
(car records))
(define (lookup given-key set-of-records)
(cond ((null? set-of-records)
false)
((equal? given-key (key (car set-of-records)))
(car set-of-records))
(else (lookup given-key (cdr set-of-records)))))
(define (lookup-better given-key records)
(if (null? records)
false
(let ((entry (key (car records))))
(cond ((= given-key key)
(car records))
((< given-key key)
(lookup-better given-key
(left-branch records)))
((> given-key key)
(lookup-better given-key
(right-branch records)))))))
#|
(7 "John")
/ \
/ \
(3 "Mary") (19 "Tom")
/ \
(1 "Peter") (5 "Jack")
|#