private bool ValidMinHeap <T>(MinHeap <T> testHeap) where T : IComparable { T[] comparables = testHeap.ToArray(); for (int i = 0; i < (comparables.Length - 2) / 2; i++) { //If left child is lesser, return false if (comparables[2 * i + 1].CompareTo(comparables[i]) < 0) { return(true); } //If right child is lesser, return false if (2 * i + 2 < comparables.Length && comparables[2 * i + 2].CompareTo(comparables[i]) < 0) { return(false); } } return(true); }
/// <summary> /// Method that switfly finds the best path from start to end. Doesn't reverse outcome /// </summary> /// <returns>The end breadcrump where each .next is a step back)</returns> private static SearchNode FindPathReversed(World world, Point3D start, Point3D end) { SearchNode startNode = new SearchNode(start, 0, 0, null); MinHeap openList = new MinHeap(); openList.Add(startNode); int sizeX = world.WorldSizeX; int sizeY = world.WorldSizeY; int sizeZ = world.WorldSizeZ; bool[] brWorld = new bool[sizeX * sizeY * sizeZ]; brWorld[start.X + (start.Y + start.Z * sizeY) * sizeX] = true; while (openList.HasNext()) { SearchNode current = openList.ExtractFirst(); if (current.position.GetDistanceSquared(end) <= 3) { return(new SearchNode(end, current.pathCost + 1, current.cost + 1, current)); } for (int i = 0; i < surrounding.Length; i++) { Surr surr = surrounding[i]; Point3D tmp = new Point3D(current.position, surr.Point); int brWorldIdx = tmp.X + (tmp.Y + tmp.Z * sizeY) * sizeX; if (world.PositionIsFree(tmp) && brWorld[brWorldIdx] == false) { brWorld[brWorldIdx] = true; int pathCost = current.pathCost + surr.Cost; int cost = pathCost + tmp.GetDistanceSquared(end); SearchNode node = new SearchNode(tmp, cost, pathCost, current); openList.Add(node); } } } return(null); //no path found }
public void TestMinHeap() { var ints = new[] { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; var heap = new MinHeap <int>(ints); var actualList = new List <int>(); int minItem = int.MaxValue; while (true) { minItem = heap.ExtractMin(); if (minItem == default) { break; } actualList.Add(minItem); } int[] expectedArr = new[] { 1, 2, 3, 4, 7, 8, 9, 10, 14, 16 }; Assert.That(actualList.ToArray(), Is.EquivalentTo(expectedArr)); }
public void TestConstructor1() { MinHeap <int> intHeap = new MinHeap <int>(); Assert.IsFalse(intHeap == null); }
public void TestRemoveMinOnEmpty() { MinHeap <Cell> newHeap = new MinHeap <Cell>(); Assert.IsNull(newHeap.RemoveMin()); }