|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.mhhe.clrs2e.Heap
Abstract base class for both binary min-heaps and binary max-heaps.
Implements methods common to both, based on Chapter 6 of
Introduction to Algorithms, Second edition. Objects in the
heap must implement the Comparable
interface.
All operations in this class maintain the invariant that the heap has the heap property.
Subclasses must implement heapify(int)
to be concrete.
Extended by MinHeap
and MaxHeap
.
Nested Class Summary | |
protected static class |
Heap.Handle
Nested class for handles within a heap. |
class |
Heap.Heapsort
Implements the Sorter interface via heapsort. |
Field Summary | |
protected java.lang.Comparable[] |
array
The array holding the heap. |
protected int |
heapSize
How many elements are actually in the heap. |
Constructor Summary | |
Heap()
Creates an empty heap. |
|
Heap(java.lang.Comparable[] array)
Makes a heap in place from the argument, and ensures that the heap property holds. |
Method Summary | |
void |
buildHeap()
Rearranges the array within the heap so that it complies with the heap property. |
protected void |
exchange(int i,
int j)
Swaps the elements at two indices. |
java.lang.Comparable |
head()
Returns the first element in the heap without removing it. |
abstract void |
heapify(int i)
Restores the heap property. |
boolean |
isEmpty()
Returns true if the heap is empty,
false otherwise. |
static int |
left(int i)
Returns the index of the left child of a node. |
com.mhhe.clrs2e.Sorter |
makeSorter()
Returns a Heap.Heapsort object that will sort this heap. |
static int |
parent(int i)
Returns the index of the parent of a node. |
static int |
right(int i)
Returns the index of the right child of a node. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected java.lang.Comparable[] array
protected int heapSize
Constructor Detail |
public Heap()
public Heap(java.lang.Comparable[] array)
array
- Array from which a heap is made.Method Detail |
public abstract void heapify(int i)
i
- Index of the position at which the heap property might
not hold.protected void exchange(int i, int j)
i
- One index.j
- The other index.public static final int parent(int i)
i
- The node whose parent's index is returned.public static final int left(int i)
i
- The node whose left child's index is returned.public static final int right(int i)
i
- The node whose right child's index is returned.public final boolean isEmpty()
true
if the heap is empty,
false
otherwise.
public java.lang.Comparable head()
public void buildHeap()
public com.mhhe.clrs2e.Sorter makeSorter()
Heap.Heapsort
object that will sort this heap.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |