0

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))))
        ))
John Friedrich
  • 343
  • 5
  • 21
  • 1
    You probably won't get great results on Stack Overflow if you don't have a programming problem (or at least, aren't sure whether you do). If you suspect that this works, then you might have better luck at http://codereview.stackexchange.com/. "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself." – Joshua Taylor Oct 30 '13 at 16:41
  • Alright, thanks for the link, I didn't know about that site. – John Friedrich Oct 31 '13 at 14:51

1 Answers1

1

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
GoZoner
  • 67,920
  • 20
  • 95
  • 145