-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex2-65-big-o-n-set-operations.scm
44 lines (34 loc) · 1.11 KB
/
ex2-65-big-o-n-set-operations.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
;; -*- geiser-scheme-implementation: mit -*-
(define (make-tree entry left right)
(list entry left right))
(define (tree? tree)
(and (list? tree)
(= 3 (length tree))))
(define (leaf? tree)
(not (tree? tree)))
(define (entry tree)
(if (tree? tree)
(car tree)
tree))
(define (left-branch tree)
(if (tree? tree)
(cadr tree)
'()))
(define (right-branch tree)
(if (tree? tree)
(caddr tree)
'()))
;; Ex 2.65, Θ(n) implementations of union-set and intersection-set for
;; sets implemented as (balanced) bi-nary trees.
;; Intersection set
(define (intersection-set-v1 set1 set2)
(cond ((or (null? set1) (null? set2)) '())
((= (car set1) (car set2)) (cons (car set1)
(intersection-set (cdr set1) (cdr set2))))
((> (car set1) (car set2)) (intersection-set set1
(cdr set2)))
(else (intersection-set (cdr set1)
set2))))
;; (intersection-set '() '(1))
;; (intersection-set '(1 2 3) '(2 3 4))
;; (intersection-set '(2 3 4) '(1 2 3))