Package com.javaholics
Class MaxHeap<T extends Comparable<? super T>>
java.lang.Object
com.javaholics.MaxHeap<T>
- Type Parameters:
T
- the type of the elements in the heap
- All Implemented Interfaces:
MaxHeapInterface<T>
public final class MaxHeap<T extends Comparable<? super T>>
extends Object
implements MaxHeapInterface<T>
A class that implements the ADT maxheap using an array.
The heap is a complete binary tree.
The root is always the maximum value in the heap.
The root is stored at index 1.
- Version:
- 1.0
- Author:
- Lea Wiranatha, Lindsay Kislingbury
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a new entry to the heap.void
clear()
Removes all elements from the heap.getElement
(int index) Returns the element at the specified index.getMax()
Returns the maximum element in the heap.int
getSize()
Returns the number of elements in the heap.int
getSwaps()
Returns the number of swaps performed during heap construction and removals.boolean
isEmpty()
Returns true if the heap is empty and false otherwise.Removes and returns the maximum element in the heap.
-
Constructor Details
-
MaxHeap
public MaxHeap()Constructs a new MaxHeap with the default capacity. -
MaxHeap
public MaxHeap(int initialCapacity) Constructs a new MaxHeap with the specified capacity.- Parameters:
initialCapacity
- the initial capacity of the heap
-
MaxHeap
Constructs a new MaxHeap with the specified entries. The heap is built using the optimal method.- Parameters:
entries
- the entries to add to the heap
-
-
Method Details
-
add
Adds a new entry to the heap.- Specified by:
add
in interfaceMaxHeapInterface<T extends Comparable<? super T>>
- Parameters:
newEntry
- the entry to add- Throws:
IllegalArgumentException
- if newEntry is null
-
getSwaps
public int getSwaps()Returns the number of swaps performed during heap construction and removals.- Returns:
- the number of swaps
-
getElement
Returns the element at the specified index.- Parameters:
index
- the index of the element to return- Returns:
- the element at the specified index
- Throws:
IndexOutOfBoundsException
- if the index is out of bounds
-
removeMax
Removes and returns the maximum element in the heap.- Specified by:
removeMax
in interfaceMaxHeapInterface<T extends Comparable<? super T>>
- Returns:
- the maximum element in the heap, or null if the heap is empty
-
getMax
Returns the maximum element in the heap.- Specified by:
getMax
in interfaceMaxHeapInterface<T extends Comparable<? super T>>
- Returns:
- the maximum element in the heap, or null if the heap is empty
-
isEmpty
public boolean isEmpty()Returns true if the heap is empty and false otherwise.- Specified by:
isEmpty
in interfaceMaxHeapInterface<T extends Comparable<? super T>>
- Returns:
- true if the heap is empty, false otherwise
-
getSize
public int getSize()Returns the number of elements in the heap.- Specified by:
getSize
in interfaceMaxHeapInterface<T extends Comparable<? super T>>
- Returns:
- the number of elements in the heap
-
clear
public void clear()Removes all elements from the heap.- Specified by:
clear
in interfaceMaxHeapInterface<T extends Comparable<? super T>>
-