sicp-ex-2.66



<< Previous exercise (2.65) | Index | Next exercise (2.67) >>


meteorgan

The solution is similar to element-of-set?

 (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))) 
         (else (lookup given-key (right-branch set-of-records))))) 

 (define (lookup given-key set-of-records) 
   (if (null? set-of-records) 
       false 
       (let ((node-entry (entry set-of-records))) 
         (let ((node-key (key node-entry))) 
           (if (= given-key node-key) 
               node-entry 
               (lookup given-key 
                       ((if (< given-key node-key) left-branch right-branch) set-of-records)))))))