以下代码在计算用户输入的值“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))))
))
答案 0 :(得分: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