/// <summary> /// Adds a new item on the Heap /// </summary> /// <param name="item">Heap item</param> public virtual void Add(HeapItem item) { EnsureCapacity(); _heapItems[_size] = item; _size++; HeapifyUp(); }
/// <summary> /// Returns object associated with first item and removes it from the Heap /// </summary> /// <returns>Object of first item</returns> public virtual TObject Take() { CheckHeap(); HeapItem first = _heapItems[0]; _heapItems[0] = _heapItems[_size - 1]; _size--; HeapifyDown(); return((TObject)first.Item); }
public void UpdateValue(TObject item, TValue newValue) { if (!heapIndices.ContainsKey(item)) { throw new ArgumentException("Element not existing in Priority Queue"); } int heapIndex = heapIndices[item]; HeapItem heapItem = _heapItems[heapIndex]; IComparable currentValue = heapItem.Value; heapItem.SetValue(newValue); int comparison = newValue.CompareTo(currentValue); if ((HeapType == BinaryHeapType.MinHeap && comparison < 0) || (HeapType == BinaryHeapType.MaxHeap && comparison > 0)) { HeapifyUp(heapIndex); } else { HeapifyDown(heapIndex); } }