public class Heap
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
protected java.util.Comparator |
cmp_
Ordering comparator.
|
protected int |
count_
Number of used slots.
|
protected java.lang.Object[] |
nodes_
The tree nodes, packed into an array.
|
| Constructor and Description |
|---|
Heap(int capacity)
Create a Heap with the given capacity, and relying on natural ordering.
|
Heap(int capacity,
java.util.Comparator cmp)
Create a Heap with the given initial capacity and comparator
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Remove all elements.
|
protected int |
compare(java.lang.Object a,
java.lang.Object b)
Perform element comparisons using comparator or natural ordering.
|
java.lang.Object |
extract()
Return and remove least element, or null if empty.
|
void |
insert(java.lang.Object x)
Insert an element, resize if necessary.
|
protected int |
left(int k)
Get left child.
|
protected int |
parent(int k)
Get parent index.
|
java.lang.Object |
peek()
Return least element without removing it, or null if empty.
|
protected int |
right(int k)
Get right child.
|
int |
size()
Return number of elements.
|
protected java.lang.Object[] nodes_
protected int count_
protected final java.util.Comparator cmp_
public Heap(int capacity,
java.util.Comparator cmp)
throws java.lang.IllegalArgumentException
capacity - initial capacity.cmp - comparator instance.java.lang.IllegalArgumentException - if capacity less or equal to zeropublic Heap(int capacity)
capacity - initial capacity.protected int compare(java.lang.Object a,
java.lang.Object b)
protected final int parent(int k)
protected final int left(int k)
protected final int right(int k)
public void insert(java.lang.Object x)
x - object to insert.public java.lang.Object extract()
public java.lang.Object peek()
public int size()
public void clear()