-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.65.scm
73 lines (63 loc) · 2.44 KB
/
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
(define (make-tree entry left-branch right-branch)
(list entry left-branch right-branch))
(define entry car)
(define left-branch cadr)
(define right-branch caddr)
(define (tree->list 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 (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))))))))
(define (union-set a b)
(define (merge-unique a b)
(cond
((null? a) b)
((null? b) a)
(else (let ((ha (car a))
(ta (cdr a))
(hb (car b))
(tb (cdr b)))
(cond
((= ha hb) (cons ha (merge-unique ta tb)))
((< ha hb) (cons ha (merge-unique ta b)))
((> ha hb) (cons hb (merge-unique a tb))))))))
(let ((a-list (tree->list a))
(b-list (tree->list b)))
(let ((merged (merge-unique a-list b-list)))
(list->tree merged))))
(union-set '(2 (1 () ()) (3 () ())) '(4 (1 () ()) (5 () ()))) ; (3 (1 () (2 () ())) (4 () (5 () ())))
(define (intersection-set a b)
(define (intersection-list a b)
(if (or (null? a) (null? b))
'()
(let ((a1 (car a)) (b1 (car b)))
(cond
((= a1 b1)
(cons a1 (intersection-list (cdr a) (cdr b))))
((< a1 b1)
(intersection-list (cdr a) b))
((> a1 b1)
(intersection-list a (cdr b)))))))
(let ((a-as-list (tree->list a))
(b-as-list (tree->list b)))
(list->tree (intersection-list a-as-list b-as-list))))
(intersection-set '(2 (1 () ()) (4 () ())) '(2 (1 () ()) (3 () ()))) ; (1 () (2 () ()))