-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.26.scm
28 lines (24 loc) · 925 Bytes
/
3.26.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
(define (key entry) (car entry))
(define (value entry) (cdr entry))
(define (set-value! entry value) (set-cdr! entry value))
(define (make-entry key value)
(cons key value))
(define (empty? tree) (null? (cdr tree)))
(define (entry tree) (cadr tree))
(define (left-branch tree) (caddr tree))
(define (right-branch tree) (cadddr tree))
(define (make-tree entry left right)
(list entry left right))
(define (make-location) (list '*tree*))
(define (lookup k tree)
(if (empty? tree) #f
(let ((cur (entry tree)))
(cond ((< k (key cur)) (lookup k (left-branch tree)))
((> k (key cur)) (lookup k (right-branch tree)))
(else (value cur))))))
(define (insert! k v tree)
(cond ((empty? tree)
(set-cdr! tree (make-tree (make-entry k v) (make-location) (make-location))))
((< k (key (entry tree))) (insert! k v (left-branch tree)))
((> k (key (entry tree))) (insert! k v (right-branch tree)))
(else (set-value! (entry tree) v))))