Class MinHeapWithUpdate

java.lang.Object
com.graphhopper.coll.MinHeapWithUpdate

public class MinHeapWithUpdate extends Object
A minimum heap implemented using a binary tree (https://en.wikipedia.org/wiki/Binary_heap). Besides the tree and the elements' values this heap also keeps track of the positions of the elements in the tree. This requires additional book-keeping when doing pushes/polls, but allows for an efficient update operation. For the same reason the heap has a fixed memory size that is determined in the constructor and the inserted element may not exceed a certain range. todo: strictly speaking the heap could automatically grow/shrink as long as the range of legal ids stays fixed, but for simplicity the heap has a fixed size for now.

This class is very similar to IntFloatBinaryHeap, but compared to this has an efficient update operation. In turn it is (much) less memory-efficient when the heap is used for a small number of elements from a large range.

  • Constructor Details

    • MinHeapWithUpdate

      public MinHeapWithUpdate(int elements)
      Parameters:
      elements - the number of elements that can be stored in this heap. Currently the heap cannot be resized or shrunk/trimmed after initial creation. elements-1 is the maximum id that can be stored in this heap
  • Method Details

    • size

      public int size()
    • isEmpty

      public boolean isEmpty()
    • push

      public void push(int id, float value)
      Adds an element to the heap, the given id must not exceed the size specified in the constructor. Its illegal to push the same id twice (unless it was polled/removed before). To update the value of an id contained in the heap use the update(int, float) method.
    • contains

      public boolean contains(int id)
      Returns:
      true if the heap contains an element with the given id
    • update

      public void update(int id, float value)
      Updates the element with the given id. The complexity of this method is O(log(N)), just like push/poll. Its illegal to update elements that are not contained in the heap. Use contains(int) to check the existence of an id.
    • peekId

      public int peekId()
      Returns:
      the id of the next element to be polled, i.e. the same as calling poll() without removing the element
    • peekValue

      public float peekValue()
      Returns:
      the value of the next element to be polled
    • poll

      public int poll()
      Extracts the element with minimum value from the heap
    • clear

      public void clear()