public void DeleteKey() { FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Increasing); int count = 0; var cells = new Dictionary <int, FibonacciHeapCell <int, string> >(); for (int i = 0; i < 10; i++) { cells.Add(i, heap.Enqueue(i, i.ToString())); count++; } int?lastValue = null; heap.Dequeue(); var DeletedCell = cells[8]; heap.Delete(DeletedCell); count -= 2; foreach (var value in heap.GetDestructiveEnumerator()) { if (lastValue == null) { lastValue = value.Key; } Assert.False(lastValue > value.Key); //TODO FIXME Assert.NotEqual(DeletedCell, value); lastValue = value.Key; count--; } Assert.Equal(0, count); }
public void DeleteKey() { FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Increasing); int count = 0; var cells = new Dictionary <int, FibonacciHeapCell <int, string> >(); for (int i = 0; i < 10; i++) { cells.Add(i, heap.Enqueue(i, i.ToString())); count++; } int?lastValue = null; heap.Dequeue(); var DeletedCell = cells[8]; heap.Delete(DeletedCell); count -= 2; foreach (var value in heap.GetDestructiveEnumerator) { if (lastValue == null) { lastValue = value.Key; } Assert.IsFalse(lastValue > value.Key, "Heap condition has been violated"); Assert.AreNotEqual(DeletedCell, value, "Found item that was deleted"); lastValue = value.Key; count--; } Assert.AreEqual(count, 0, "Not all elements enqueued were dequeued"); }
public T Dequeue() { var node = _heap.Min(); _heap.Delete(node); ////var e = m_items[0]; ////m_items[0] = m_items[--m_count]; ////// Restore heap if it was broken ////FixDown(0); ////// Once items count reaches one eighth of the queue ////// capacity it is reduced to half so that items ////// still occupy one fourth (if it is reduced when ////// count reaches one fourth after reduce items will ////// occupy half of queue capacity and next enqueued item ////// will require queue expand) ////if (m_count <= m_items.Length / 8) ////{ //// Expand(m_items.Length / 2); ////} return(node.Data); }
public void RandomTest() { FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Increasing); Random rand = new Random(10); int NumberOfRecords = 10000; int RangeMultiplier = 10; int count = 0; var cells = new List <FibonacciHeapCell <int, string> >(); for (int i = 0; i < NumberOfRecords; i++) { cells.Add(heap.Enqueue(rand.Next(0, NumberOfRecords * RangeMultiplier), i.ToString())); count++; } while (!heap.IsEmpty) { int action = rand.Next(1, 6); int i = 0; while (action == 1 && i < 2) { action = rand.Next(1, 6); i++; } int lastValue = int.MinValue; switch (action) { case 1: cells.Add(heap.Enqueue(rand.Next(0, NumberOfRecords * RangeMultiplier), "SomeValue")); count++; break; case 2: Assert.IsFalse(lastValue > heap.Top.Priority, "Heap Condition Violated"); lastValue = heap.Top.Priority; cells.Remove(heap.Top); heap.Dequeue(); count--; break; case 3: int deleteIndex = rand.Next(0, cells.Count); heap.Delete(cells[deleteIndex]); cells.RemoveAt(deleteIndex); count--; break; case 4: int decreaseIndex = rand.Next(0, cells.Count); int newValue = rand.Next(0, cells[decreaseIndex].Priority); if (newValue < lastValue) { lastValue = newValue; } heap.ChangeKey(cells[decreaseIndex], newValue); break; case 5: int increaseIndex = rand.Next(0, cells.Count); heap.ChangeKey(cells[increaseIndex], rand.Next(cells[increaseIndex].Priority, NumberOfRecords * RangeMultiplier)); break; default: break; } } Assert.AreEqual(count, 0, "Not all elements enqueued were dequeued"); }