File tree Expand file tree Collapse file tree 1 file changed +37
-3
lines changed Expand file tree Collapse file tree 1 file changed +37
-3
lines changed Original file line number Diff line number Diff line change 13
13
14
14
(define (look-up given-key set-of-records )
15
15
(cond ((null? set-of-records) #f )
16
- ((equal? given-key (key (entry set-of-records) ))
16
+ ((= given-key (key set-of-records))
17
17
(entry set-of-records))
18
- (else (and (look-up given-key (left-branch set-of-records))
19
- (look-up given-key (right-branch set-of-records))))))
18
+ ((< given-key (key set-of-records))
19
+ ; ; 查找左子树
20
+ (look-up given-key (left-branch set-of-records)))
21
+ (else
22
+ ; ; 查找右子树
23
+ (look-up given-key (right-branch set-of-records)))))
24
+
25
+ (module+ test
26
+ (require rackunit)
27
+ ; ; 5
28
+ ; ; 3 8
29
+ ; ; 1 4 6 9
30
+ (define tree
31
+ '(5 " data5"
32
+ (3 " data3"
33
+ (1 " data1" () ())
34
+ (4 " data4" () ()))
35
+ (8 " data8"
36
+ (6 " data6" () ())
37
+ (9 " data9" () ()))))
38
+
39
+ (test-case " Test for look-up existing key"
40
+ (check-equal? (look-up 3 tree) " data3" )
41
+ (check-equal? (entry ' (6 " data6" () ())) " data6" )
42
+ )
43
+ (test-case " Test for non-exist key"
44
+ (check-false (look-up 100 tree))
45
+ )
46
+
47
+ (test-case " Test for edge case"
48
+ (define empty-tree '())
49
+ (check-false (look-up 5 empty-tree))
50
+ (define root '(5 " root" () ()))
51
+ (check-equal? (look-up 5 root) " root" )
52
+ )
53
+ )
You can’t perform that action at this time.
0 commit comments