New updates.
This commit is contained in:
@@ -18,14 +18,13 @@ import java.util.ArrayList;
|
||||
* @author Mark Allen Weiss
|
||||
* @author DLB
|
||||
*/
|
||||
public class BinaryHeap<E extends Comparable<E>> {
|
||||
public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||
|
||||
// Number of elements in heap.
|
||||
private int currentSize;
|
||||
|
||||
// The heap array. Java genericity does not work with arrays so we have to use
|
||||
// an ArrayList.
|
||||
private ArrayList<E> array;
|
||||
// The heap array.
|
||||
private final ArrayList<E> array;
|
||||
|
||||
/**
|
||||
* Construct a new empty binary heap.
|
||||
@@ -126,62 +125,37 @@ public class BinaryHeap<E extends Comparable<E>> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the heap is empty, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.currentSize == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Current size (number of elements) of this heap.
|
||||
*/
|
||||
@Override
|
||||
public int size() {
|
||||
return this.currentSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the given element into the heap.
|
||||
*
|
||||
* @param x Item to insert.
|
||||
*/
|
||||
public void add(E x) {
|
||||
@Override
|
||||
public void insert(E x) {
|
||||
int index = this.currentSize++;
|
||||
this.arraySet(index, x);
|
||||
this.percolateUp(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell the binary heap that the given element has been modified and should be
|
||||
* re-positioned inside the heap.
|
||||
*
|
||||
* @param x Item to update.
|
||||
*/
|
||||
public void update(E x) {
|
||||
@Override
|
||||
public void remove(E x) throws ElementNotFoundException {
|
||||
// TODO:
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the smallest item in the heap.
|
||||
*
|
||||
* @return The smallest item in the heap.
|
||||
*
|
||||
* @throws RuntimeException if this heap is empty.
|
||||
*/
|
||||
public E findMin() throws RuntimeException {
|
||||
@Override
|
||||
public E findMin() throws EmptyPriorityQueueException {
|
||||
if (isEmpty())
|
||||
throw new RuntimeException("Empty binary heap.");
|
||||
return this.array.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the smallest item from the heap.
|
||||
*
|
||||
* @return The smallest item in the heap.
|
||||
*
|
||||
* @throws RuntimeException if this heap is empty.
|
||||
*/
|
||||
public E deleteMin() throws RuntimeException {
|
||||
@Override
|
||||
public E deleteMin() throws EmptyPriorityQueueException {
|
||||
E minItem = findMin();
|
||||
E lastItem = this.array.get(--this.currentSize);
|
||||
this.arraySet(0, lastItem);
|
||||
|
||||
64
src/main/org/insa/algo/utils/BinarySearchTree.java
Normal file
64
src/main/org/insa/algo/utils/BinarySearchTree.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package org.insa.algo.utils;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||
|
||||
// Underlying implementation
|
||||
private final SortedSet<E> sortedSet;
|
||||
|
||||
/**
|
||||
* Create a new empty binary search tree.
|
||||
*/
|
||||
public BinarySearchTree() {
|
||||
this.sortedSet = new TreeSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a copy of the given binary search tree.
|
||||
*
|
||||
* @param bst Binary search tree to copy.
|
||||
*/
|
||||
public BinarySearchTree(BinarySearchTree<E> bst) {
|
||||
this.sortedSet = new TreeSet<>(bst.sortedSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return sortedSet.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return sortedSet.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(E x) {
|
||||
sortedSet.add(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(E x) throws ElementNotFoundException {
|
||||
if (!sortedSet.remove(x)) {
|
||||
throw new ElementNotFoundException(x);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E findMin() throws EmptyPriorityQueueException {
|
||||
if (isEmpty()) {
|
||||
throw new EmptyPriorityQueueException();
|
||||
}
|
||||
return sortedSet.first();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E deleteMin() throws EmptyPriorityQueueException {
|
||||
E min = findMin();
|
||||
remove(min);
|
||||
return min;
|
||||
}
|
||||
|
||||
}
|
||||
32
src/main/org/insa/algo/utils/ElementNotFoundException.java
Normal file
32
src/main/org/insa/algo/utils/ElementNotFoundException.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package org.insa.algo.utils;
|
||||
|
||||
public class ElementNotFoundException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Element not found
|
||||
private final Object element;
|
||||
|
||||
/**
|
||||
* @param element Element that was not found.
|
||||
*/
|
||||
public ElementNotFoundException(Object element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The element that was not found.
|
||||
*/
|
||||
public Object getElement() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "element not found: " + element;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.insa.algo.utils;
|
||||
|
||||
public class EmptyPriorityQueueException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public EmptyPriorityQueueException() {
|
||||
}
|
||||
|
||||
}
|
||||
54
src/main/org/insa/algo/utils/PriorityQueue.java
Normal file
54
src/main/org/insa/algo/utils/PriorityQueue.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package org.insa.algo.utils;
|
||||
|
||||
/**
|
||||
* Interface representing a basic priority queue.
|
||||
*
|
||||
* @see https://en.wikipedia.org/wiki/Priority_queue
|
||||
*/
|
||||
public interface PriorityQueue<E extends Comparable<E>> {
|
||||
|
||||
/**
|
||||
* Check if the priority queue is empty.
|
||||
*
|
||||
* @return true if the queue is empty, false otherwise.
|
||||
*/
|
||||
public boolean isEmpty();
|
||||
|
||||
/**
|
||||
* @return Current size (number of elements) of this queue.
|
||||
*/
|
||||
public int size();
|
||||
|
||||
/**
|
||||
* Insert the given element into the queue.
|
||||
*
|
||||
* @param x Item to insert.
|
||||
*/
|
||||
public void insert(E x);
|
||||
|
||||
/**
|
||||
* Remove the given element from the priority queue.
|
||||
*
|
||||
* @param x Item to remove.
|
||||
*/
|
||||
public void remove(E x) throws ElementNotFoundException;
|
||||
|
||||
/**
|
||||
* Retrieve (but not remove) the smallest item in the queue.
|
||||
*
|
||||
* @return The smallest item in the queue.
|
||||
*
|
||||
* @throws EmptyPriorityQueueException if this queue is empty.
|
||||
*/
|
||||
public E findMin() throws EmptyPriorityQueueException;
|
||||
|
||||
/**
|
||||
* Remove and return the smallest item from the priority queue.
|
||||
*
|
||||
* @return The smallest item in the queue.
|
||||
*
|
||||
* @throws EmptyPriorityQueueException if this queue is empty.
|
||||
*/
|
||||
public E deleteMin() throws EmptyPriorityQueueException;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user