With a Heap that implements a Priority Queue, we can balance both space and time in our search. In this lab, we will implement this Heap, organized to always have the minimum element available in constant time. If we assign priority in a certain way, we will be implementing A* Search, a classic pathfinding algorithm.
parent()
, left()
, right()
,
and legal()
methods. Each method has a corresponding test case
which will pass with a correct solution.
isHeap()
isHeap()
method so that it will check that all of the properties of a Heap are
present. Namely, you must verify that each element is smaller than its children elements.
Because of our use of an ArrayList to track the elements, we will always assume that
the elements form a Heap that is as compact as possible.
If your implementation is correct, it should pass all four testIsHeap
test cases.
swap()
method, and make sure testSwap()
passes.
peek
. This should return the
root element, which will always be stored in the first position of the ArrayList.
Then, you should implement the add
method. New elements are added to the
end of the ArrayList, and then filtered up repeatedly when the element is found
to be less than its parent. Use the Comparator object to calculate
if an element is greater or less than another element. It is recommended to create a helper method to determine the index of the parent of a heap element. This will help in creating a cleaner implementation of add
.
Once these methods are implemented, the testAdd1()
and testAdd2()
test cases should pass.
Once this method is implemented the three testLowestInFamily
tests should all pass.
Once this method is implemented, the testRemove()
test case should pass. Using the indexOfLowestInFamily()
method makes it
much easier to write this method correctly!
compareTo
method in sorting algorithms, but
a Trail cannot be properly compared to another trail without the context of a puzzle.
Java lets us compare in another way for this circumstance. In the
TrailEstimator
class, you will find two methods. This class is a
Comparator for the Trail objects, which will have the proper Puzzle object available.
The estimateFor
method should return the length of the given Trail, plus
an estimate for the remaining length of the trail. This estimate should be calculated
using the manhattanDistanceTo
method of the Position class.
The compare
method will then be able to compare two Trail objects, using
using the estimate for each Trail calculated in the above method. Return -1 if the
first Trail estimate is less than the second, 0 if they are equal, and 1 if the first
Trail estimate is greater than the second.
maze.searchers
package.
Create 10 random mazes of size 50x50. For each maze, use the new Shotgun button
to remove some of the walls. Now, for each maze and strategy (Stack, Queue, and Heap),
record the number of visited nodes as a percentage of the total number of open spaces
in the initial maze. Discuss the differences you see between the Stack, Queue, and Heap.
Cumulative Progress | Points Earned |
---|---|
Step 1 | 4 |
Step 2 | 8 |
Step 3 | 10 |
Step 4 | 12 |
Step 5 | 14 |
Step 6 | 16 |
Step 7 | 18 |
Step 8 | 20 |