public class Heap
extends java.lang.Object
Elements of this data structure should either implement Comparable, or a Comparator should be given as argument to the constructor.
| Modifier and Type | Field and Description |
|---|---|
private java.util.Comparator |
m_comparator |
private int |
m_count |
private java.lang.Object[] |
m_nodes |
| Constructor and Description |
|---|
Heap()
Creates a new Heap whose elements inserted implement the
Comparable interface. |
Heap(java.util.Comparator comparator)
Creates a new Heap whose elements are compared using the given
Comparator. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Empties this heap
|
protected int |
compare(java.lang.Object o1,
java.lang.Object o2) |
java.lang.Object |
extract()
Removes and returns the least element of this heap.
|
void |
insert(java.lang.Object obj)
Inserts the given element in this heap.
|
protected int |
left(int index) |
protected int |
parent(int index) |
java.lang.Object |
peek() |
protected int |
right(int index) |
private java.util.Comparator m_comparator
private int m_count
private java.lang.Object[] m_nodes
public Heap()
Comparable interface.public Heap(java.util.Comparator comparator)
Comparator.comparator - public void insert(java.lang.Object obj)
obj - extract()public java.lang.Object extract()
insert(java.lang.Object),
peek()public java.lang.Object peek()
extract()public void clear()
protected int compare(java.lang.Object o1,
java.lang.Object o2)
protected int parent(int index)
index - index.protected int left(int index)
index - index.protected int right(int index)
index - index.