-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-2.65.scm
109 lines (100 loc) · 3.77 KB
/
ex-2.65.scm
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
(load "./util.scm")
(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) #f)
((= x (entry set)) #t)
((< 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))))))
;; 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 '()))
;; 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))))))))
;; ex-2.65
(define (union-set-l s1 s2)
(cond ((null? s1) s2)
((null? s2) s1)
(else (let ((x1 (car s1)) (x2 (car s2)))
(cond ((= x1 x2)
(cons x1
(union-set-l (cdr s1)
(cdr s2))))
((< x1 x2)
(cons x1
(union-set-l (cdr s1) s2)))
((< x2 x1)
(cons x2
(union-set-l s1 (cdr s2)))))))))
(define (union-set t1 t2)
(cond ((null? t1) t2)
((null? t2) t1)
(else (list->tree (union-set-l (tree->list-2 t1)
(tree->list-2 t2))))))
(print (union-set (list->tree '(1 3 5 7 9 10)) (list->tree '(2 5 7 10 13))))
(define (intersection-set-l set1 set2)
(if (or (null? set1) (null? set2))
'()
(let ((x1 (car set1)) (x2 (car set2)))
(cond ((= x1 x2)
(cons x1
(intersection-set-l (cdr set1)
(cdr set2))))
((< x1 x2)
(intersection-set-l (cdr set1) set2))
((< x2 x1)
(intersection-set-l set1 (cdr set2)))))))
(define (intersection-set t1 t2)
(cond ((null? t1) t2)
((null? t2) t1)
(else (list->tree (intersection-set-l (tree->list-2 t1)
(tree->list-2 t2))))))
(print (intersection-set (list->tree '(1 3 5 7 9 10)) (list->tree '(2 5 7 10 13))))