练习2.66
这道题相当于2叉树在实际工程中的1个应用,我们照旧要用到前面所学到的3个进程:entry, left-branch, right-branch。这3者的作用分别是取出结点、左分支、右分支。而根据题目要求,这里还需要1个获得键值的key。固然了,就像书中上文所展现的那样,key其实不需要我们写出来,这里就用到了按欲望思惟。
(define (lookup given-key tree-of-records)
(if (null? tree-of-records)
#f
(let ((entry-key (key (entry tree-of-records))))
(cond ((= given-key entry-key)
(entry tree-of-records))
((> given-key entry-key)
(lookup given-key (right-branch tree-of-records)))
((< given-key entry-key)
(lookup given-key (left-branch tree-of-records)))))))