Binary Heap

Download

Download jar file or use maven. psjava requires Java 1.6 (or above)

<dependency>
    <groupId>org.psjava</groupId>
    <artifactId>psjava</artifactId>
    <version>0.1.19</version>
</dependency>

Example Code

// Let's create a small heap.

Heap<Integer> heap =BinaryHeapFactory.getInstance().create(new DefaultComparator<Integer>());
heap.insert(100);
heap.insert(300);
heap.insert(200);

// This is most basic operation. Extraction of minimum.

int extracted = heap.extractMinimum(); // must be 100

// You can only get the miminum without extraction by 'getMinimum()'.

int minimum1 = heap.getMinimum(); // must be 200

// Let's do decrease-key operation.
// when you insert, keep the node like a pointer . And decrease it with the node.

HeapNode<Integer> node = heap.insert(400);
node.decreaseKey(10); // decrease 400 -> 10

int minimum2 = heap.getMinimum(); // must be 10

Implementation


Copyright 2014 psjava team. View on GitHub