chickadee » heap » heap-insert!

heap-insert! heap key datumprocedure

Insert a new element into the heap if the datum does not exist; otherwise, adjust its key.

heap
The heap in which to insert
element
The element to be inserted
(define (heap-insert! heap key datum)
  (if (heap-member? heap datum)
    (heap-change-key! heap datum key)
    (let ((heap-size (heap-size heap)))
      (if (= heap-size (heap-length heap))
        (heap-data-set! heap (vector-resize (heap-data heap) (* 2 heap-size))))
      (heap-size-set! heap (+ heap-size 1))
      (let ((element (make-element (heap-inf heap) datum)))
        (heap-set! heap heap-size element)
        (heap-change-key!/index heap heap-size key)))))