Ejemplo n.º 1
0
 /// <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();
 }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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);
            }
        }