|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectjutil.BinaryHeap
public class BinaryHeap
A binary heap is a complete binary tree except possibly for the bottom level. The element of every node is greater than the element of its parent. Thus, the smallest element is stored at the root of the tree.
| Field Summary | |
|---|---|
private int |
capacity
The capacity of the heap is a power of 2. |
private java.lang.Comparable[] |
elems
The children of elems[i] are stored in elems[2i] and elems[2i + 1], the root (= min) is in elems[1]. |
private int |
size
The number of elements stored. |
| Constructor Summary | |
|---|---|
BinaryHeap()
Initializes capacity to 2^8 = 512. |
|
BinaryHeap(int exp)
Initializes capacity to 2^exp. |
|
| Method Summary | |
|---|---|
void |
clear()
Sets elems[i] to null for each i. |
java.lang.Comparable |
deleteMin()
Removes and returns the smallest element. |
void |
insert(java.lang.Comparable x)
Inserts element according to its natural order. |
boolean |
isEmpty()
Returns true if size is 0. |
private void |
percolateDown(int hole)
A helper method that repeatedly swaps a node with its smaller child until there are no smaller children. |
int |
size()
Returns the size of the heap. |
java.lang.Comparable[] |
toArray()
Converts heap into a sorted array. |
java.lang.String |
toString()
Converts heap into a LISP-style string representation of a heap. |
private java.lang.String |
toString(int i)
Recursively converts the heap rooted at element i into a LISP style string representation of a tree. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
private java.lang.Comparable[] elems
private int size
private int capacity
| Constructor Detail |
|---|
public BinaryHeap(int exp)
public BinaryHeap()
| Method Detail |
|---|
public void clear()
clear in interface PriorityQueuepublic int size()
size in interface PriorityQueuepublic boolean isEmpty()
isEmpty in interface PriorityQueuepublic java.lang.String toString()
toString in class java.lang.Objectprivate java.lang.String toString(int i)
public java.lang.Comparable[] toArray()
public void insert(java.lang.Comparable x)
throws OverflowException
insert in interface PriorityQueuex - element to be inserted.
OverflowException - if the heap is full.public java.lang.Comparable deleteMin()
deleteMin in interface PriorityQueueprivate void percolateDown(int hole)
hole - position of the element to percolate down.
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||