0

以下代码在计算有多少值小于或等于用户输入的值“z”方面是否有任何错误?特别希望有人看一下这段代码的最终功能,告诉我是否有任何错误。

(define (create-heap v H1 H2)
(list v H1 H2))
(define (h-min H) (car H))
(define (left H)  (cadr H))
(define (right H) (caddr H))

(define (insert-h x H)
  (if (null? H)
   (create-heap x '() '())
   (let ((child-value (max x (h-min H)))
         (root-value  (min x (h-min H))))
     (create-heap root-value
                  (right H)
                  (insert-h child-value (left H))))))

(define (insert-all lst H)
  (if (null? lst)
    H
    (insert-all (cdr lst) (insert-h (car lst) H))))

(define (count-less-than-or-equal-in-heap z H)
    (cond ((or (null? H) (> (h-min H) z)) 0)
        (else
        (+ 1 (count-less-than-or-equal-in-heap z (right H))
             (count-less-than-or-equal-in-heap z (left H))))
        ))
4

1 回答 1

1

没有错误:

> (define heap (insert-all '(0 5 10 15) '()))
> (count-less-than-or-equal-in-heap -1 heap)
0
> (count-less-than-or-equal-in-heap 0 heap)
1
> (count-less-than-or-equal-in-heap 15 heap)
4

> (set! heap (insert-all '(0 5 5 5) '()))
> (count-less-than-or-equal-in-heap 0 heap)
1
> (count-less-than-or-equal-in-heap 5 heap)
4
> (count-less-than-or-equal-in-heap 50 heap)
4
于 2013-10-30T21:33:40.273 回答