乐正

Actions speak louder than words.

Sicp-ex2-66

问题

假设记录的集合采用二叉树实现,按照其中作为键值的数值排序。请实现相应的lookup过 程。

解答

练习2.66 (ex2-66.scm) download
1
2
3
4
5
6
7
8
(define (lookup given-key set-of-records)
  (cond ((null? set-of-records) #f)
        ((= given-key (key (entry set-of-records)))
         (entry set-of-records))
        ((< given-key (key (entry set-of-records)))
         (lookup given-key (left-branch set-of-records)))
        ((> given-key (key (entry set-of-records)))
         (lookup given-key (right-branch set-of-records)))))

draft

« sicp-ex2-65 sicp-ex2-67 »

Comments