|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectscranton.tree.HeapViaArray
Heap Class: Manages a heap in an array applying a comparator. The class includes several utilities fall into two categories 1. Methods to support the heap-to-array relationship and the corresponding index calculations 2. The code for the siftUp and siftDown methods
| Field Summary | |
protected java.util.Comparator |
c
c.compare(a,b)<0 implies a precedes b |
protected java.lang.Object[] |
heap
The array that holds the heap |
protected int |
heapSize
The size of the heap |
| Constructor Summary | |
HeapViaArray(java.util.Comparator comp)
Method Heap construct a heap with Comparator c and initially of size max. |
|
HeapViaArray(java.util.Comparator comp,
int max)
Method Heap construct a heap with Comparator c and initially of size max. |
|
HeapViaArray(java.lang.Object[] data,
int actual,
java.util.Comparator comp)
Method Heap construct a heap from an exisitng array of data with. |
|
| Method Summary | |
java.lang.Object[] |
close()
Method close terminates the heap and returns the current array. |
private java.lang.Object[] |
doubler(java.lang.Object[] oldHeap)
Method doubler doubles the size of the array instead of reporting an overflow error. |
void |
insert(java.lang.Object val)
Method insert a new object and reorganize the heap by applying the siftUp process. |
private boolean |
isLeaf(int currentNode)
Method isLeaf. |
private int |
leftchild(int currentNode)
Method leftchild. |
private int |
parent(int currentNode)
Method parent. |
java.lang.Object |
remove()
Method remove removes the root and applies the siftDown process to maintain the heap correctly. |
private int |
rightchild(int currentNode)
Method rightchild. |
private void |
siftDown(int currentNode)
Method siftDown implements the siftDown process. |
private void |
siftUp(int curr)
Method siftUp apply the siftUp method starting at curr. |
int |
size()
Method size reports the current size of the heap. |
private void |
swap(int i,
int j)
Method swap interchanges two objects in the array. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected java.util.Comparator c
protected java.lang.Object[] heap
protected int heapSize
| Constructor Detail |
public HeapViaArray(java.util.Comparator comp)
comp - The comparator that applies the heap order
public HeapViaArray(java.util.Comparator comp,
int max)
max - comp -
public HeapViaArray(java.lang.Object[] data,
int actual,
java.util.Comparator comp)
data - - an array of Objectsactual - - the number of Objectscomp - - the heap order| Method Detail |
public int size()
size in interface Heappublic void insert(java.lang.Object val)
insert in interface Heapval - public java.lang.Object remove()
remove in interface Heappublic java.lang.Object[] close()
private boolean isLeaf(int currentNode)
currentNode - - index of the currentNode
private int leftchild(int currentNode)
currentNode - - array index of currentNode
private int rightchild(int currentNode)
currentNode - - array index of current node
private int parent(int currentNode)
currentNode - - array index of current node
private void swap(int i,
int j)
i - - array index of one Objectj - - array index of other Objectprivate java.lang.Object[] doubler(java.lang.Object[] oldHeap)
oldHeap - current heap
private void siftUp(int curr)
curr - - start position of siftUp methodprivate void siftDown(int currentNode)
currentNode - - start position of the siftDown process
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||