/// <summary> /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>. /// </summary> /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param> /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception> public void Add(T item) { GeneralTree <T> child = new GeneralTree <T>(item); childNodes.Add(child); child.parent = this; }
/// <summary> /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>. /// </summary> /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param> /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception> public void Add(T item) { // Add a dummy to the end of the list (it will be replaced) data.Add(default(T)); int counter = data.Count - 1; while ((counter > 1) && (comparerToUse.Compare(data[counter / 2], item) > 0)) { data[counter] = data[counter / 2]; counter = counter / 2; } data[counter] = item; }
/// <summary> /// Adds the specified object. /// </summary> /// <param name="item">The object to add to the collection</param> /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception> public void Add(T item) { if (data.Count == 0) { data.Add(item); } else { int index = data.BinarySearch(item, comparerToUse); // Item was found if (index >= 0) { data.Insert(index, item); } else { data.Insert(~index, item); } } }
/// <summary> /// Initializes a new instance of the <see cref="Heap<T>"/> class. /// </summary> /// <param name="type">The type of heap.</param> /// <param name="capacity">The initial capacity of the Heap.</param> /// <param name="comparer">The comparer to use.</param> public Heap(HeapType type, int capacity, IComparer <T> comparer) { if (comparer == null) { throw new ArgumentNullException("comparer"); } thisType = type; data = new VisitableList <T>(capacity); data.Add(default(T)); // Add a dummy item so our indexing starts at 1 if (type == HeapType.MinHeap) { comparerToUse = comparer; } else { comparerToUse = new ReverseComparer <T>(comparer); } }