Beispiel #1
0
        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <returns>
        /// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
        public bool Remove(T item)
        {
            for (int i = 0; i < childNodes.Count; i++)
            {
                if (childNodes[i].Data.Equals(item))
                {
                    childNodes[i].parent = null;
                    childNodes.RemoveAt(i);
                    return(true);
                }
            }

            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// Removes the smallest item in the heap (located at the root).
        /// </summary>
        /// <returns>The value contained in the root of the Heap.</returns>
        public T RemoveRoot()
        {
            if (this.Count == 0)
            {
                throw new InvalidOperationException(Resources.HeapIsEmpty);
            }

            // The minimum item to return.
            T min = data[1];

            // The last item in the heap
            T last = data[this.Count];

            data.RemoveAt(this.Count);

            // If there's still items left in this heap, reheapify it.
            if (this.Count > 0)
            {
                // Re-heapify the binary tree to conform to the heap property
                int counter = 1;

                while ((counter * 2) < (data.Count))
                {
                    int child = counter * 2;

                    if (((child + 1) < (data.Count)) &&
                        (comparerToUse.Compare(data[child + 1], data[child]) < 0))
                    {
                        child++;
                    }

                    if (comparerToUse.Compare(last, data[child]) <= 0)
                    {
                        break;
                    }

                    data[counter] = data[child];
                    counter       = child;
                }

                data[counter] = last;
            }

            return(min);
        }
Beispiel #3
0
 /// <summary>
 /// Removes the item at the specified index.
 /// </summary>
 /// <param name="index">The index of the item</param>
 public void RemoveAt(int index)
 {
     CheckIndexValid(index);
     data.RemoveAt(index);
 }