Does the following code have any errors in terms of counting how many values are less than or equal to a value "z" input by the user? In particular, I would like someone to look at the final function of this code to tell me if there are any errors.
(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))))
))
There are no errors:
> (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