Commit 7b5d33f0 authored by Cody A Burchett's avatar Cody A Burchett
Browse files

-m comments for heap

parent 30954f53
...@@ -10,12 +10,15 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{ ...@@ -10,12 +10,15 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{
//add heapIndex, compareTo to tile class //add heapIndex, compareTo to tile class
//compare fCost of node then hCost //compare fCost of node then hCost
//item.compareTo(item2) should return pos if item is less than item2, neg if item is greater than item2
//create heap
public Heap(int maxSize) public Heap(int maxSize)
{ {
heapTiles = new Tile[maxSize]; heapTiles = new Tile[maxSize];
} }
//add item into heap and sort it into position
public void Add(Tile item) public void Add(Tile item)
{ {
item.heapIndex = itemCount; item.heapIndex = itemCount;
...@@ -24,6 +27,7 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{ ...@@ -24,6 +27,7 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{
itemCount++; itemCount++;
} }
//remove first tile and resort heap
public Tile RemoveFirst() public Tile RemoveFirst()
{ {
Tile first = heapTiles[0]; Tile first = heapTiles[0];
...@@ -34,11 +38,13 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{ ...@@ -34,11 +38,13 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{
return first; return first;
} }
//resort item position
public void UpdateItem(Tile item) public void UpdateItem(Tile item)
{ {
SortUp(item); SortUp(item);
} }
public int Count public int Count
{ {
get get
...@@ -47,11 +53,13 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{ ...@@ -47,11 +53,13 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{
} }
} }
//does heap contain item
public bool Contains(Tile item) public bool Contains(Tile item)
{ {
return Equals(heapTiles[item.heapIndex], item); return Equals(heapTiles[item.heapIndex], item);
} }
//sort tile lower into heap
void SortDown(Tile item) void SortDown(Tile item)
{ {
while (true) while (true)
...@@ -88,6 +96,7 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{ ...@@ -88,6 +96,7 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{
} }
} }
//sort item higher into heap
void SortUp(Tile item) void SortUp(Tile item)
{ {
int indexOfParent = (item.heapIndex - 1) / 2; int indexOfParent = (item.heapIndex - 1) / 2;
...@@ -108,6 +117,7 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{ ...@@ -108,6 +117,7 @@ public class Heap <Tile> where Tile : HeapItem<Tile>{
} }
} }
//swap tiles
void Swap(Tile item1, Tile item2) void Swap(Tile item1, Tile item2)
{ {
heapTiles[item1.heapIndex] = item2; heapTiles[item1.heapIndex] = item2;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment