Class QuaternaryPrimitiveMinHeap
This implementation provides MinHeap contract using a quaternary tree structure. Each
node in the heap can have up to 4 children, which reduces the height of the tree compared to a
binary heap, resulting in fewer comparisons during sift operations.
The heap maintains three parallel arrays:
heap: stores node IDs in heap ordercosts: stores the corresponding costs for each nodeidToPos: provides O(1) lookup from node ID to heap position
This implementation uses 0-based indexing and automatically resizes when capacity is exceeded
(see Resizable). Implements Siftable for heap property maintenance through sift
operations with quaternary parent/child relationships.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new quaternary min-heap with the default initial capacity.QuaternaryPrimitiveMinHeap(int initialCapacity) Constructs a new quaternary min-heap with the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()Returns the current capacity of the internal storage.voidclear()Removes all elements from the heap.booleancontains(long nodeId) Checks if a specific node is currently in the heap.doublecost(long nodeId) Gets the current cost of a node in the heap.voidEnsures the internal storage has sufficient capacity, resizing if necessary.longRemoves and returns the node with the minimum cost from the heap.voidinsertOrUpdate(long nodeId, double cost) Inserts a new node or updates an existing node's cost in the heap.booleanisEmpty()Checks if the heap is empty.voidsiftDown(int index) Moves a node down the heap until the heap property is restored.voidsiftUp(int index) Moves a node up the heap until the heap property is restored.intsize()Returns the number of elements currently in the heap.
-
Constructor Details
-
QuaternaryPrimitiveMinHeap
public QuaternaryPrimitiveMinHeap()Constructs a new quaternary min-heap with the default initial capacity.- Since:
- 5.4.2
-
QuaternaryPrimitiveMinHeap
public QuaternaryPrimitiveMinHeap(int initialCapacity) Constructs a new quaternary min-heap with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity for the heap- Since:
- 5.4.1
-
-
Method Details
-
insertOrUpdate
public void insertOrUpdate(long nodeId, double cost) Description copied from interface:MinHeapInserts a new node or updates an existing node's cost in the heap.If the node is not currently in the heap, it is inserted with the given cost. If the node already exists and the new cost is lower, the node's cost is updated (decrease-key operation).
- Specified by:
insertOrUpdatein interfaceMinHeap- Parameters:
nodeId- the unique identifier of the nodecost- the cost associated with the node (typically F-cost for A*)
-
capacity
public int capacity()Description copied from interface:ResizableReturns the current capacity of the internal storage. -
ensureCapacity
public void ensureCapacity()Description copied from interface:ResizableEnsures the internal storage has sufficient capacity, resizing if necessary.- Specified by:
ensureCapacityin interfaceResizable
-
size
public int size()Description copied from interface:MinHeapReturns the number of elements currently in the heap. -
clear
public void clear()Description copied from interface:MinHeapRemoves all elements from the heap. -
contains
public boolean contains(long nodeId) Description copied from interface:MinHeapChecks if a specific node is currently in the heap. -
cost
public double cost(long nodeId) Description copied from interface:MinHeapGets the current cost of a node in the heap. -
isEmpty
public boolean isEmpty()Checks if the heap is empty. -
extractMin
public long extractMin()Description copied from interface:MinHeapRemoves and returns the node with the minimum cost from the heap.- Specified by:
extractMinin interfaceMinHeap- Returns:
- the identifier of the node with the minimum cost
-
siftUp
public void siftUp(int index) Moves a node up the heap until the heap property is restored.This method is used after inserting a new node or decreasing a node's cost. The node at the given index is compared with its parent and swapped if necessary, continuing until the node is in the correct position.
In a quaternary heap, the parent of a node at index i is at index (i-1)/4, which is computed efficiently using the unsigned right shift operator
>>> 2. -
siftDown
public void siftDown(int index) Moves a node down the heap until the heap property is restored.This method is used after removing the minimum element. The node at the given index is compared with its children and swapped with the smallest child if necessary, continuing until the node is in the correct position.
In a quaternary heap, a node at index i has up to 4 children at indices 4*i+1, 4*i+2, 4*i+3, and 4*i+4. The method finds the child with the minimum cost and swaps if needed. The multiplication by 4 is computed efficiently using left shift:
index << 2.
-