Esempio n. 1
0
 /// <summary>
 /// Insert a new element to the heap, and maintain the heap oreder.
 /// <para>BinaryHeapException:</para>
 /// Thrown if the heap is full.
 /// </summary>
 /// <exception cref="BinaryHeapException"></exception>
 /// </summary>
 public void insert(int element)
 {
     if (heapSize == heapArray.Length)
     {
         throw new BinaryHeapException("Failed to insert because the heap is full");
     }
     heapArray[heapSize] = element;
     heapSize++;
     BinaryHeapHelper.SiftUp(heapSize - 1, heapArray, heapSize);
 }
Esempio n. 2
0
        /// <summary>
        /// Removes and returns the element with the highest priority in the queue.
        /// <para>BinaryHeapException:</para>
        /// Thrown if the heap is empty.
        /// </summary>
        /// <exception cref="BinaryHeapException"></exception>
        public int Pop()
        {
            if (heapSize == 0)
            {
                throw new BinaryHeapException("Failed to perform the Pop operation since the queue is empty");
            }
            int result = heapArray[0];

            heapArray[0] = heapArray[heapSize - 1];
            heapSize--;
            BinaryHeapHelper.SiftDown(0, heapArray, heapSize);
            return(result);
        }
Esempio n. 3
0
 /// <summary>
 /// Constructs a binary heap containing the given elements.
 /// </summary>
 public BinaryHeap(int[] heapArray)
 {
     this.heapArray = heapArray;
     heapSize       = heapArray.Length;
     BinaryHeapHelper.MakeHeap(this.heapArray, heapSize);
 }