Priority Queues and Heaps

You will submit your heap and priority queue implementations as a checkpoint in 1 week on November 13th! We'll grade them using the supplied tester. Submit them in your project 7 folder.

The goal of this lab period is to build a Heap structure that acts as Priority Queue.


Tasks

Testing

As always, as you are coding up this project create testing files for each class you create exercising the public methods implemented in this class.

Setup

Make a new folder for this project and copy over your Stack, Queue, and LinkedList classes from the earlier projects (your LinkedList will need to implement both the Stack and the Queue interfaces).

PriorityQueue interface

This week, we'll use this interface. You'll notice that it looks pretty similar to the Queue interface: this is intentional. To some degree, the structures are similar in that a Priority Queue is still a 'type' of Queue: while a Queue typically refers to a FIFO-structure, a Priority-Queue uses a Comparator to make sure that the item polled is the item of greatest priority.

Tasks

  1. Create a Heap class

    Create a file Heap.java and create a class with the following declaration: public class Heap<T> implements PriorityQueue<T>.

    This Heap will be a node-based Binary Heap. A binary heap is a rooted-tree structure that follows two rules:

    1. The data of every node in the tree other than the root has value no less than its parent.
    2. The shape of the tree is such that every level other than the last is fully filled and the last is filled from left to right.
    In class we used arrays to manage our structure; in this lab, we'll use Nodes to organize the structure of our Heap.

  2. Create a container class (a Node)

    As in the LinkedList class, we will use Nodes. This time, however, each Node will have 4 fields: three Node fields for the left, right, and parent references and a fourth field ot type T to store the data of the Node.

  3. Create fields for the Heap class

    The Heap will need to maintain the following four fields:

    • A Comparator<T>: this Comparator will help us sort our input. A Comparator has access to a compareTo method that allows us to compare two items of type T to help us organize our structure.
    • An int for the number of items in the Heap (the size)
    • And two Nodes: specifically the root of the Heap (the minimal item) and the last Node to have been inserted.

  4. Creating constructors for the Heap class

    A Heap should support the following constructors:

    • public Heap(Comparator<T> comparator, boolean maxHeap): this Heap should use the given comparator to determine the order of this structure. If the comparator is null, the type T will be assumed to be Comparable (and a new Comparator should be created accordingly). If maxHeap is true, then a max-heap will be constructed instead (and a new Comparator should be created accordingly).
    • public Heap(Comparator<T> comparator): equivalent to Heap(comparator, false).
    • public Heap(boolean maxHeap): equivalent to Heap(null, maxHeap).
    • public Heap(): equivalent to Heap(null, false).

  5. Creating methods for the Heap class
  6. In particular, you must implement the methods of the PriorityQueue interface. In order to do so, I suggest utilizing following private methods:

    The remaining methods to implement are just the methods of the PriorityQueue interface.

  7. Testing: Since this project is only a week long, we'll include a simple tester you could use.

Cell Class

README: In order to expedite the process of this project, we're including a fully implemented Cell class and CellType enum. However, do take a few minutes to peruse the methods described below and their implementations to make sure you understand the underlying Cell class.

In a return to familiarity, this project will be based around a 2-D Landscape consisting of individual Cells. Each Cell will have an underyling type; specifically, each Cell will either be an

  1. Declaring the enum CellType

    To implement this we'll create an additional file CellType.java with the following contents:

    public enum CellType {
        FREE, OBSTACLE;
    }
    An enum is something like a restricted class in Java, in that you're declaring a type of object that can only take on specific values. In this case, we're declaring that the enum type can either be free or obstructed.

  2. Cell's Fields

    Each Cell should have the following (private) fields:

    • boolean visited for determining whether we've visited this Cell yet
    • Cell prev for determining which Cell we came from to visit this Cell
    • int row, col for determining which row and column this Cell belongs to in the Landscape
    • CellType type for determining which type of Cell this is

  3. Cell's Methods

    All of Cell's methods are basically just getter/setters, but they'll become more obviously useful in the Landscape class.

    • public Cell (int row, int col, CellType type) constructor that sets up the row, col, and type as specified; prev should be null and visited should be false.
    • public int getRow() returns the row
    • public int getCol() returns the column
    • public CellType getType() returns the type
    • public boolean visited() returns visited
    • public Cell getPrev() returns prev
    • public void visitFrom(Cell c) sets visited to true and prev to c
    • public void reset() sets visited to false and prev to null


When you are done, you can proceed to the project.