public void ConsolidatedHeapDecreaseKey_CorrectCuts() { FibonacciHeap <int> heap = new FibonacciHeap <int>(); IList <FibonacciNode <int> > input = new List <FibonacciNode <int> >() { new FibonacciNode <int>(0), new FibonacciNode <int>(28), new FibonacciNode <int>(-13), new FibonacciNode <int>(80), new FibonacciNode <int>(3), new FibonacciNode <int>(7), new FibonacciNode <int>(-7), new FibonacciNode <int>(42), new FibonacciNode <int>(-11), new FibonacciNode <int>(12) }; IList <int> expectedElementOrder = new List <int>() { 7, -8, -11, 12, -42, 80, -1, -3, 0 }; foreach (FibonacciNode <int> value in input) { heap.Insert(value); } heap.ExtractMin(); // A decrease key with no structural changes heap.DecreaseKey(input[6], -8); // Normal cuts with parent marked heap.DecreaseKey(input[7], -42); heap.DecreaseKey(input[4], -1); // Double cascading cut heap.DecreaseKey(input[1], -3); IList <int> result = heap.GetAllValues(); for (int i = 0; i < expectedElementOrder.Count; ++i) { NUnit.Framework.Assert.IsTrue(expectedElementOrder[i] == result[i]); } NUnit.Framework.Assert.IsTrue(heap.GetMin() == -42); }
public void ConsolidatedHeapExtractMin_CorrectConsolidation() { // Create consolidated heap FibonacciHeap <int> heap = new FibonacciHeap <int>(); IList <int> input = new List <int>() { 0, 28, -13, 80, 3, 7, -7, 42, -11, 12 }; IList <int> expectedElementOrder = new List <int>() { 7, 42, 12, 28, 80, 3, 0, -7 }; foreach (int value in input) { heap.Insert(value); } heap.ExtractMin(); // Trigger second consolidation heap.ExtractMin(); IList <int> result = heap.GetAllValues(); for (int i = 0; i < expectedElementOrder.Count; ++i) { NUnit.Framework.Assert.IsTrue(expectedElementOrder[i] == result[i]); } NUnit.Framework.Assert.IsTrue(heap.GetMin() == -7); }
public void UnconsolidatedHeapInsertWithValue_RightOder() { FibonacciHeap <int> heap = new FibonacciHeap <int>(); IList <int> input = new List <int>() { 0, 28, -13, 80 }; foreach (int value in input) { heap.Insert(value); } IList <int> result = heap.GetAllValues(); for (int i = 0; i < input.Count; ++i) { NUnit.Framework.Assert.IsTrue(input[i] == result[i]); } }
public void UnconsolidatedHeapExtractMin_CorrectValue() { FibonacciNode <int> node = new FibonacciNode <int>(-20); FibonacciHeap <int> heap = new FibonacciHeap <int>(); IList <int> input = new List <int>() { 0, 28, -13, 80 }; heap.Insert(node); foreach (int value in input) { heap.Insert(value); } NUnit.Framework.Assert.IsTrue(heap.ExtractMin() == node); NUnit.Framework.Assert.IsTrue(heap.GetMin() == -13); NUnit.Framework.Assert.IsTrue(heap.GetAllValues().Count == input.Count); }
public void EmptyHeapGetAllValues_Empty() { FibonacciHeap <int> heap = new FibonacciHeap <int>(); NUnit.Framework.Assert.IsTrue(heap.GetAllValues().Count == 0); }