/// <summary> /// Returns a new min heap that contains all elements of this heap. /// </summary> public IMinHeap <T> ToMinHeap() { MinBinaryHeap <T> newMinHeap = new MinBinaryHeap <T>(this.Count(), this._heapComparer); newMinHeap.Initialize(this._collection.ToArray()); return(newMinHeap); }
/// <summary> /// Union two heaps together, returns a new min-heap of both heaps' elements, /// ... and then destroys the original ones. /// </summary> public MinBinaryHeap <T> Union(ref MinBinaryHeap <T> firstMinHeap, ref MinBinaryHeap <T> secondMinHeap) { if (firstMinHeap == null || secondMinHeap == null) { throw new ArgumentNullException("Null heaps are not allowed."); } // Create a new heap with reserved size. int size = firstMinHeap.Count() + secondMinHeap.Count(); var newHeap = new MinBinaryHeap <T> (size, Comparer <T> .Default); // Insert into the new heap. while (firstMinHeap.IsEmpty() == false) { newHeap.Add(firstMinHeap.ExtractMin()); } while (secondMinHeap.IsEmpty() == false) { newHeap.Add(secondMinHeap.ExtractMin()); } // Destroy the two heaps. firstMinHeap = secondMinHeap = null; return(newHeap); }