public class Heap extends Object
| Modifier and Type | Field and Description |
|---|---|
protected Comparator |
cmp_
Ordering comparator.
|
protected int |
count_
Number of used slots.
|
protected 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,
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(Object a,
Object b)
Perform element comparisons using comparator or natural ordering.
|
Object |
extract()
Return and remove least element, or null if empty.
|
void |
insert(Object x)
Insert an element, resize if necessary.
|
protected int |
left(int k)
Get left child.
|
protected int |
parent(int k)
Get parent index.
|
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 Object[] nodes_
protected int count_
protected final Comparator cmp_
public Heap(int capacity,
Comparator cmp)
throws IllegalArgumentException
capacity - initial capacity.cmp - comparator instance.IllegalArgumentException - if capacity less or equal to zeropublic Heap(int capacity)
capacity - initial capacity.protected int compare(Object a, Object b)
protected final int parent(int k)
protected final int left(int k)
protected final int right(int k)
public void insert(Object x)
x - object to insert.public Object extract()
public Object peek()
public int size()
public void clear()
Copyright © 2004-2013 Cosylab. All Rights Reserved.