-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.3.3-representing-sets.rkt
377 lines (324 loc) · 12.3 KB
/
2.3.3-representing-sets.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#lang sicp
(define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))
(define (adjoin-set x set)
(if (element-of-set? x set)
set
(cons x set)))
(define (intersection-set set1 set2)
(cond ((or (null? set1) (null? set2)) '())
((element-of-set? (car set1) set2)
(cons (car set1)
(intersection-set (cdr set1) set2)))
(else (intersection-set (cdr set1) set2))))
; exercise 2.59
(define (union-set set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
((element-of-set? (car set1) set2)
(union-set (cdr set1) set2))
(else (cons (car set1)
(union-set (cdr set1) set2)))))
(union-set '(1 2 3 4) '(3 4 5 6)) ; '(1 2 3 4 5 6)
(union-set '(1 2) '(3 4 5)) ; '(1 2 3 4 5)
(union-set '() '(1 2 3 4)) ; '(1 2 3 4)
(union-set '(1 2 3 4) '(4 5)) ; '(1 2 3 4 5)
; exercise 2.60
; element-of-set? remains the same. The entire list has to be traversed so it's linear.
; adjoin-set doesn't need to check for existence. It becomes O(1)
(define (adjoin-set x set)
(cons x set))
; intersection-set remains quadratic; it still needs to check whether each element
; belongs in the result set.
; union-set just appends lists. It's linear.
(define (union-set set1 set2)
(append set1 set2))
(adjoin-set 3 '(1 2 3)) ; '(3 1 2 3)
(union-set '(a b c d) '(c d e f)) ; '(a b c d c d e f)
; sets as ordered lists
(define (element-of-set? x set)
(cond ((null? set) false)
((= x (car set)) true)
((< x (car set)) false)
(else (element-of-set? x (cdr set)))))
(define (intersection-set set1 set2)
(if (or (null? set1) (null? set2))
'()
(let ((x1 (car set1)) (x2 (car set2)))
(cond ((= x1 x2)
(cons x1
(intersection-set (cdr set1) (cdr set2))))
((< x1 x2)
(intersection-set (cdr set1) set2))
((< x2 x1)
(intersection-set set1 (cdr set2)))))))
; exercise 2.61
(define (adjoin-set x set)
(if (null? set)
(list x)
(let ((head (car set)))
(cond ((= x head) set)
((> x head)
(cons head (adjoin-set x (cdr set))))
(else (cons x set))))))
(adjoin-set 5 '(1 2 3 8 9)) ; '(1 2 3 5 8 9)
(adjoin-set 8 '(1 2 3 8 9)) ; '(1 2 3 8 9)
(adjoin-set 3 '()) ; '(3)
(adjoin-set 10 '(1 2 3 4)) ; '(1 2 3 4 10)
(adjoin-set 1 '(5 10 15)) ; '(1 5 10 15)
; exercise 2.62
(define (union-set set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
(else
(let ((x1 (car set1)) (x2 (car set2)))
(cond ((= x1 x2)
(cons x1 (union-set (cdr set1) (cdr set2))))
((< x1 x2)
(cons x1 (union-set (cdr set1) set2)))
((> x1 x2)
(cons x2 (union-set set1 (cdr set2)))))))))
(union-set '(1 2 3 4) '(4 5 7 8)) ; '(1 2 3 4 5 7 8)
(union-set '(1 2 3) '(1 2 3)) ; '(1 2 3)
(union-set '() '(1 2 3)) ; '(1 2 3)
(union-set '(1 2 3) '(2 3 4 5 7 8)) ; '(1 2 3 4 5 7 8)
; sets as binary trees
(define (entry tree) (car tree))
(define (left-branch tree) (cadr tree))
(define (right-branch tree) (caddr tree))
(define (make-tree entry left right)
(list entry left right))
(define (element-of-set? x set)
(cond ((null? set) false)
((= 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 '()))
; tree->list-1 is slower because of the append call -- it causes a linear scan of each resulting
; sublist, over and over as longer sublists are built out of subtrees.
; tree->list-2 on the other hand does only constant-time operations.
(define tree1
(make-tree 7
(make-tree 3
(make-tree 1 '() '())
(make-tree 5 '() '()))
(make-tree 9
'()
(make-tree 11 '() '()))))
(define tree2
(make-tree 3
(make-tree 1 '() '())
(make-tree 7
(make-tree 5 '() '())
(make-tree 9
'()
(make-tree 11 '() '())))))
(define tree3
(make-tree 5
(make-tree 3
(make-tree 1 '() '())
'())
(make-tree 9
(make-tree 7 '() '())
(make-tree 11 '() '()))))
(tree->list-1 tree1) ; '(1 3 5 7 9 11)
(tree->list-1 tree2) ; '(1 3 5 7 9 11)
(tree->list-1 tree3) ; '(1 3 5 7 9 11)
(tree->list-2 tree1) ; '(1 3 5 7 9 11)
(tree->list-2 tree2) ; '(1 3 5 7 9 11)
(tree->list-2 tree3) ; '(1 3 5 7 9 11)
; They produce the same results though.
; 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)))
(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))
(remaining-elts (cdr right-result)))
(cons (make-tree this-entry left-tree right-tree)
remaining-elts))))))))
(list->tree '(1 3 5 7 9 11))
; '(5 (1 ()
; (3 () ()))
; (9 (7 () ())
; (11 () ())))
; 5
; / \
; 1 9
; / \ / \
; () 3 7 11
; / \ / \ / \
; () () () () () ()
; A call to partial tree for some number n of elements to insert uses n as counter, divides the counter
; by two (with a bias to the right) and recursively decreases said counter until the base case is reached,
; at which point it ensures the previous call, which will still have a "this-entry", inserts an empty leaf
; node as left tree.
; Once this-entry is popped from the list of elements pending insertion, a recursive call to build the
; right tree with the corresponding elements proceeds in the same way, first building the left trees with
; fewer elements, then taking each this-entry, then building the right sub-trees.
; Since the car of the remaining-elts is always pushed, the order of insertion can be seen by scanning the
; tree from left to right. Thus 1 is leftmost, followed by 3, followed by 5 (this-entry at the topmost call)
; then followed by 7, 9 and 11 in the right subtree of the topmost call.
; exercise 2.64 b
; Each call to partial-tree results in two recursive calls, but these are done with approximately half
; the number of elements each. Thus the exponential number of calls balances out with the logarithmic
; number of elements (?) (logarithmic decrease? exponential decrease?) at each step. Thus, list->tree
; is linear in the number of elements, which sort of makes sense because each element is visited once
; when it is popped as this-entry (besides the initial linear scanning of elements to compute the total
; length of the input list).
; (partial-tree (1 .. 11) 6)
; left-size = 2
; (partial-tree (1 .. 11) 2)
; left-size = 0
; (quotient 1 2) -> left-size = 0
; (partial-tree (1 .. 11) 0)
; left-result <== (cons '() (1 .. 11))
; left-tree = '(), non-left-elts (1 .. 11)
; right-size = 2 - 1 = 1
; this-entry = 1
; (partial-tree (3 .. 11) 1)
; left-size = 0
; left-result <== (cons '() (3 .. 11))
; left-tree = '(), non-left-elts = (3 .. 11)
; right-size = 1 - 1 = 0
; this-entry = 3
; right-result <== (partial-tree (5 .. 11) 0) = (() (5 .. 11))
; right-tree = (), remaining-elts = (5 .. 11)
; right-result <== (cons (make-tree 3 () () (5 .. 11)))
; == '((3 () ()) 5 7 9 11)
; right-tree = (3 () ())
; remaining-elts = (5 .. 11)
; left-result <== (cons (make-tree 1 () (3 () ())) (5 .. 11))
; == '((1 () (3 () ())) 5 7 9 11)
; left-tree = (1 () (3 () ()))
; non-left-elts = (5 .. 11)
; right-size = 6 - 3 = 3
; this-entry = 5
; (partial-tree (7 9 11) 3)
; left-size = 1
; (partial-tree (7 9 11) 1)
; left-size = 0
; left-result <== (() (7 9 11))
; left-tree = (), non-left-elts = (7 9 11)
; right-size = 1 - 1 = 0
; this-entry = 7
; right-result <== (partial-tree (9 11) 0) = (() (9 11))
; right-tree = (), remaining-elts = (9 11)
; left-result <== (cons (make-tree 7 () ()) (9 11))
; == '((7 () ()) 9 11)
; left-tree = (7 () ()), non-left-elts = (9 11)
; right-size = 3 - 2 = 1
; this-entry = 9
; (partial-tree (11) 1)
; left-size = 0
; left-result <== (() 11)
; left-tree = (), non-left-elts = (11)
; right-size = 1 - 1 = 0
; this-entry = 11
; right-result <== '(())
; right-tree = (), remaining-elts = ()
; right-result <== (cons (make-tree 11 () ()) ())
; == '((11 () ()))
; right-tree = (11 () ())
; remaining-elts = ()
; right-result <== (cons (make-tree 9 (7 () ()) (11 () ())) ())
; == '((9 (7 () ()) (11 () ())))
; right-tree = (9 (7 () ()) (11 () ()))
; remaining-elts = ()
; <== (cons (make-tree 5 (1 () (3 () ()))
; (9 (7 () ()) (11 () ())))
; ())
; == '((5 (1 () (3 () ())) (9 (7 () ()) (11 () ())))), the final tree.
; exercise 2.65
(define tree->list tree->list-2)
; Assuming tree->list-2 is linear, this is a linear operation:
(tree->list (list->tree '(1 3 5 7 9 11))) ; '(1 3 5 7 9 11)
; From 2.3.3, for a valid representation we can assume the elements in the left subtree are smaller
; and the ones in the right subtree are larger than a node's value. That is to say, tree->list will
; always output an ordered list (if given a balanced tree produced by list->tree lol).
(define tree1 (list->tree '(1 3 5 7 9))) ; (5 (1 () (3 () ())) (7 () (9 () ())))
(define tree2 (list->tree '(5 7 9 11 13))) ; (9 (5 () (7 () ())) (11 () (13 () ())))
; Merge two ordered lists. This operation is linear: it traverses each list at most once.
(define (merge xs ys)
(cond ((null? xs) ys)
((null? ys) xs)
(else (let ((x (car xs))
(y (car ys)))
(cond ((> x y) (cons y (merge xs (cdr ys))))
((> y x) (cons x (merge (cdr xs) ys)))
(else (cons x (merge (cdr xs) (cdr ys)))))))))
(merge '(1 3 5 7 9) '(1 3 5 7 9)) ; '(1 3 5 7 9)
(merge '(1 3 5 7 9) '(5 7 9 11 13)) ; '(1 3 5 7 9 11 13)
(merge '(1 3 5) '(7 9 11 13)) ; '(1 3 5 7 9 11 13)
(merge '() '(1 2 3)) ; '(1 2 3)
(define (union-set set1 set2)
(let ((list1 (tree->list set1)) ; linear
(list2 (tree->list set2))) ; linear
(list->tree (merge list1 list2)))) ; linear (linear)
(union-set tree1 tree2)
; '(7 (3 7
; (1 () ()) 3 11
; (5 () ())) 1 5 9 13
; (11
; (9 () ())
; (13 () ())))
; Remember that for binary trees, element-of-set? is a (log n) operation.
(element-of-set? 3 tree1) ; #t
(element-of-set? 4 tree1) ; #f
; Intersect two ordered lists. This operation is linear: it traverses each list at most once.
(define (intersect xs ys)
(if (or (null? xs) (null? ys))
'()
(let ((x (car xs))
(y (car ys)))
(cond ((< x y) (intersect (cdr xs) ys))
((< y x) (intersect xs (cdr ys)))
(else (cons x (intersect (cdr xs) (cdr ys))))))))
(intersect '(1 3 5 7 9) '(1 3 5 7 9)) ; '(1 3 5 7 9)
(intersect '(1 3 5 7 9) '(5 7 9 11 13)) ; '(5 7 9)
(intersect '(1 3 5) '(7 9 11)) ; '()
(define (intersection-set set1 set2)
(let ((list1 (tree->list set1))
(list2 (tree->list set2)))
(list->tree (intersect list1 list2))))
(intersection-set tree1 tree2) ; '(7 (5 () ()) (9 () ()))
; Ta-da!