Example #1
0
        /// <summary>
        /// Removes the min element from the heap.
        /// </summary>
        /// <param name="keyValue">If the operation is successful, contains the minimum element in the array.</param>
        /// <param name="heapArrayLength">The length of the heap array. </param>
        /// <returns>True in case of success, and false otherwise</returns>
        public override bool TryRemoveRoot(out KeyValuePair <TKey, TValue> keyValue, int heapArrayLength)
        {
            keyValue = new KeyValuePair <TKey, TValue>((TKey)typeof(TKey).GetField("MinValue").GetValue(null), default(TValue)); // T.MinValue;

            /* If array is empty, returns false. */
            if (heapArrayLength == 0)
            {
                return(false);
            }

            /* If array has only one element left, it is the minimum value, and clear the array after removing it.*/
            if (heapArrayLength == 1)
            {
                keyValue = HeapArray[0];
                HeapArray.Clear();;
                return(true);
            }

            /* If array has more than 1 element, the next instructions are executed. */
            keyValue     = HeapArray[0];                    /* In a minHeap the minimum value is always in the root, which is at index 0.*/
            HeapArray[0] = HeapArray[heapArrayLength - 1];  /* Move the last element to the place of root, and then bubble down. */
            HeapArray.RemoveAt(heapArrayLength - 1);        /* Removing the last element, as it is now placed in the root's position, and needs to be bubbled down.*/
            BubbleDown_Recursively(0, heapArrayLength - 1); /* Call this method to bubble down the (new) root.*/ /* Also notice that the array is shorter by one value now, thus the new array length is one smaller. */
            return(true);
        }
        /// <summary>
        /// Removes the min element from the heap.
        /// </summary>
        /// <param name="keyValue">If the operation is successful, contains the minimum element in the array.</param>
        /// <param name="heapArrayLength">The length of the heap array. </param>
        /// <returns></returns>
        public override bool TryRemoveRoot(out KeyValuePair <TKey, TValue> keyValue, int heapArrayLength)
        {
            keyValue = new KeyValuePair <TKey, TValue>((TKey)typeof(TKey).GetField("MinValue").GetValue(null), default(TValue));

            if (heapArrayLength == 0)
            {
                return(false);
            }
            if (heapArrayLength == 1)
            {
                keyValue = HeapArray[0];
                HeapArray.Clear();
                return(true);
            }

            keyValue     = HeapArray[0];
            HeapArray[0] = HeapArray[heapArrayLength - 1];
            HeapArray.RemoveAt(heapArrayLength - 1);
            BubbleDownMin_Recursively(0, heapArrayLength - 1); /* Calling this method, because this is a min-max heap and 0 is expected to be on a min level.*/ /* Also notice that the array is shorter by one value now, thus the new array length is one smaller. */
            return(true);
        }
Example #3
0
        /// <summary>
        /// Removes the max element from the heap.
        /// </summary>
        /// <param name="keyValue">If the operation is successful, contains the maximum element in the array.</param>
        /// <param name="heapArrayLength">The length of the heap array. </param>
        /// <returns>True in case of success, and false otherwise</returns>
        public override bool TryRemoveRoot(out KeyValuePair <TKey, TValue> keyValue, int heapArrayLength)
        {
            keyValue = new KeyValuePair <TKey, TValue>((TKey)typeof(TKey).GetField("MaxValue").GetValue(null), default(TValue));

            if (heapArrayLength == 0)
            {
                return(false);
            }
            if (heapArrayLength == 1)
            {
                keyValue = HeapArray[0];
                HeapArray.Clear();
                return(true);
            }

            keyValue     = HeapArray[0];
            HeapArray[0] = HeapArray[heapArrayLength - 1];
            HeapArray.RemoveAt(heapArrayLength - 1);
            BubbleDown_Recursively(0, heapArrayLength - 1); /* notice that the array is shorter by one value now, thus the new array length is one smaller. */

            return(true);
        }