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

    Constructors
    Constructor
    Description
    Constructs a new MaxHeap with the default capacity.
    MaxHeap(int initialCapacity)
    Constructs a new MaxHeap with the specified capacity.
    MaxHeap(T[] entries)
    Constructs a new MaxHeap with the specified entries.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T newEntry)
    Adds a new entry to the heap.
    void
    Removes all elements from the heap.
    getElement(int index)
    Returns the element at the specified index.
    Returns the maximum element in the heap.
    int
    Returns the number of elements in the heap.
    int
    Returns the number of swaps performed during heap construction and removals.
    boolean
    Returns true if the heap is empty and false otherwise.
    Removes and returns the maximum element in the heap.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public MaxHeap(T[] entries)
      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

      public void add(T newEntry)
      Adds a new entry to the heap.
      Specified by:
      add in interface MaxHeapInterface<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

      public T getElement(int index)
      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

      public T removeMax()
      Removes and returns the maximum element in the heap.
      Specified by:
      removeMax in interface MaxHeapInterface<T extends Comparable<? super T>>
      Returns:
      the maximum element in the heap, or null if the heap is empty
    • getMax

      public T getMax()
      Returns the maximum element in the heap.
      Specified by:
      getMax in interface MaxHeapInterface<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 interface MaxHeapInterface<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 interface MaxHeapInterface<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 interface MaxHeapInterface<T extends Comparable<? super T>>