private static string DumpHeap <T>(LeftistHeap <T> .Heap heap) where T : IComparable <T> { if (LeftistHeap <T> .IsEmpty(heap)) { return("\u2205"); } var results = new StringBuilder(); if (!LeftistHeap <T> .IsEmpty(heap.A)) { results.Append(DumpHeap(heap.A)); } results.Append(heap.X); //results.Append(" ["); //results.Append(heap.r); //results.Append("]"); results.Append(", "); if (!LeftistHeap <T> .IsEmpty(heap.B)) { results.Append(DumpHeap(heap.B)); } return(results.ToString()); }
public void TC_LeftistHeap() { var heap = new LeftistHeap <double>(); HeapTest(heap, GetDoubleTestList()); Assert.AreEqual("Leftist Heap", heap.GetName()); }
public void SingleElement() { var heap = LeftistHeap <int> .Empty; heap = LeftistHeap <int> .Insert(2, heap); Assert.AreEqual("2, ", DumpHeap(heap)); }
public void SingleIsEmptyTest() { var heap = LeftistHeap <int> .Empty; heap = LeftistHeap <int> .Insert(2, heap); Assert.IsFalse(LeftistHeap <int> .IsEmpty(heap)); }
public void DelMinTest() { var heap = new[] { 3, 2, 5, 1 }.Aggregate(LeftistHeap <int> .Empty, (h, x) => LeftistHeap <int> .Insert(x, h)); heap = LeftistHeap <int> .DeleteMin(heap); Assert.AreEqual("3, 2, 5, ", DumpHeap(heap)); Assert.AreEqual(2, LeftistHeap <int> .FindMin(heap)); }
public void MergeTest() { var heap1 = new[] { "How", "now," }.Aggregate(LeftistHeap <string> .Empty, (h, x) => LeftistHeap <string> .Insert(x, h)); var heap2 = new[] { "brown", "cow?" }.Aggregate(LeftistHeap <string> .Empty, (h, x) => LeftistHeap <string> .Insert(x, h)); var heap = LeftistHeap <string> .Merge(heap1, heap2); Assert.AreEqual("cow?, brown, now,, How, ", DumpHeap(heap)); Assert.AreEqual("brown", LeftistHeap <string> .FindMin(heap)); }
public void InsertZeroTest() { var heap = new[] { 3, 2, 5, 1 }.Aggregate(LeftistHeap <int> .Empty, (h, x) => LeftistHeap <int> .Insert(x, h)); heap = LeftistHeap <int> .Insert(0, heap); Assert.AreEqual("3, 2, 5, 1, 0, ", DumpHeap(heap)); Assert.AreEqual(0, LeftistHeap <int> .FindMin(heap)); }
public void SingleMinTest() { var heap = LeftistHeap <int> .Empty; heap = LeftistHeap <int> .Insert(2, heap); var x = LeftistHeap <int> .FindMin(heap); Assert.AreEqual(2, x); }
public void SingleDeleteMinTest() { var heap = LeftistHeap <int> .Empty; heap = LeftistHeap <int> .Insert(2, heap); heap = LeftistHeap <int> .DeleteMin(heap); Assert.IsTrue(LeftistHeap <int> .IsEmpty(heap)); }
public static ISearchHeap getHeapByParam(int param) { ISearchHeap heapStructure = null; switch (param) { case 1: heapStructure = new RedBlackTreeHeap(); break; case 2: heapStructure = new FibonacciHeap(); break; case 3: heapStructure = new FibonacciHeap2(); break; case 4: heapStructure = new RegularBinaryHeap(); break; case 5: heapStructure = new RegularTernaryHeap(); break; case 6: heapStructure = new BinomialHeap(); break; case 7: heapStructure = new LeftistHeap(); break; default: break; } return(heapStructure); //heapStructure = new Heaps.RedBlackTreeHeap(); //ISearchHeap heapStructure = new Heaps.FibonacciHeap1(); //ISearchHeap heapStructure = new Heaps.FibonacciHeap2(); //ISearchHeap heapStructure = new Heaps.RegularBinaryHeap(); //ISearchHeap heapStructure = new Heaps.RegularTernaryHeap(); //ISearchHeap heapStructure = new Heaps.BinomialHeap(); //ISearchHeap heapStructure = new Heaps.LeftistHeap(); }
/// <summary> /// Computes the distances to the goals for the specified pattern. /// </summary> /// <param name="pattern">Pattern (i.e. variables of the pattern) to process.</param> private PatternValuesDistances ComputePatternDistances(int[] pattern) { IHeap <double, ISimpleConditions> fringe = new LeftistHeap <ISimpleConditions>(); InsertPatternConditions(fringe, (Conditions)Problem.GoalConditions, 0, pattern); PatternValuesDistances patternValuesDistances = new PatternValuesDistances(); while (fringe.GetSize() > 0) { double distance = fringe.GetMinKey(); ISimpleConditions conditions = fringe.RemoveMin(); int[] patternValues = conditions.GetAssignedValues(); Debug.Assert(pattern.Length == conditions.GetSize()); Debug.Assert(pattern.Length == patternValues.Length); if (patternValuesDistances.ContainsKey(patternValues)) { // already processed with a lower cost continue; } patternValuesDistances.Add(patternValues, distance); foreach (var predecessor in Problem.GetPredecessors(conditions)) { IConditions predecessorConditions = (IConditions)predecessor.GetPredecessorConditions(); IOperator predecessorOperator = (IOperator)predecessor.GetAppliedOperator(); foreach (var predecessorSimpleConditions in predecessorConditions.GetSimpleConditions()) { double cost = distance + predecessorOperator.GetCost(); InsertPatternConditions(fringe, predecessorSimpleConditions, cost, pattern); } } } return(patternValuesDistances); }
public void DeleteLotsOfMinsTest() { var random = new Random(3456); var heap = LeftistHeap <int> .Empty; for (var i = 0; i < 100; i++) { heap = LeftistHeap <int> .Insert(random.Next(100), heap); } var last = 0; var count = 0; while (!LeftistHeap <int> .IsEmpty(heap)) { var next = LeftistHeap <int> .FindMin(heap); heap = LeftistHeap <int> .DeleteMin(heap); Assert.IsTrue(last <= next); last = next; count++; } Assert.AreEqual(100, count); }
public IEnumerator <T> GetEnumerator() { return(LeftistHeap <T> .CreateEnumerator(this)); }
public ILeftistHeap <T> Add(T item) { return(LeftistHeap <T> .Make(this.Comparer, item)); }
public void EmptyDeleteMinTest() { var heap = LeftistHeap <int> .Empty; AssertThrows <ArgumentNullException>(() => LeftistHeap <int> .DeleteMin(heap)); }
public void EmptyIsEmptyTest() { var heap = LeftistHeap <int> .Empty; Assert.IsTrue(LeftistHeap <int> .IsEmpty(heap)); }
IEnumerator IEnumerable.GetEnumerator() { return(LeftistHeap <T> .CreateEnumerator(this)); }
public void MinTreeTest() { var heap = new[] { 3, 2, 5, 1 }.Aggregate(LeftistHeap <int> .Empty, (h, x) => LeftistHeap <int> .Insert(x, h)); Assert.AreEqual(1, LeftistHeap <int> .FindMin(heap)); }
public void DumpTreeTest() { var heap = new[] { 3, 2, 5, 1 }.Aggregate(LeftistHeap <int> .Empty, (h, x) => LeftistHeap <int> .Insert(x, h)); Assert.AreEqual("3, 2, 5, 1, ", DumpHeap(heap)); }