/// <summary>
        /// Inserts a new value into the Min Heap.
        /// </summary>
        /// <param name="keyValue">The new key-value pair to be inserted in the tree.</param>
        /// <param name="heapArrayLength">The length of the heap array. </param>
        public override void Insert(KeyValuePair <TKey, TValue> keyValue, int heapArrayLength)
        {
            HeapArray.Add(keyValue);
            int index = heapArrayLength;

            BubbleUp_Recursively(index, heapArrayLength + 1);
        }
Example #2
0
        /// <summary>
        /// Inserts a new value into the Max Heap.
        /// </summary>
        /// <param name="value">The new value to be inserted in the tree.</param>
        /// <param name="heapArrayLength">The length/size of the heap array. </param>
        public override void Insert(KeyValuePair <TKey, TValue> value, int heapArrayLength)
        {
            HeapArray.Add(value);// means gets added to the end of the list.

            // Bubble up this element/node
            int nodeIndex = heapArrayLength;

            BubbleUp_Iteratively(nodeIndex, heapArrayLength + 1); // Notice that the size of the array is grown by one now.
        }
Example #3
0
        /// <summary>
        /// Inserts a new value into the Min Heap.
        /// </summary>
        /// <param name="newValue">The new key-value pair to be inserted in the tree.</param>
        /// <param name="heapArrayLength">The length of the heap array. </param>
        public override void Insert(KeyValuePair <TKey, TValue> newValue, int heapArrayLength)
        {
            /* Add the new value to the end of the array. List is a dynamic array and grows in size automatically. */
            HeapArray.Add(newValue);

            /* Bubble up the new value, and stop when the parent is no longer bigger than the new value, or when new value is bubbled up to the root's position. */
            int nodeIndex = heapArrayLength;

            BubbleUp_Iteratively(nodeIndex, heapArrayLength + 1);
        }