/// <summary>
        /// Union two heaps together, returns a new min-heap of both heaps' elements,
        /// ... and then destroys the original ones.
        /// </summary>
        public BinaryMinHeap <T> Union(ref BinaryMinHeap <T> firstMinHeap, ref BinaryMinHeap <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 BinaryMinHeap <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);
        }
        /// <summary>
        /// Returns a new min heap that contains all elements of this heap.
        /// </summary>
        public IMinHeap <T> ToMinHeap()
        {
            BinaryMinHeap <T> newMinHeap = new BinaryMinHeap <T>(this.Count, this._heapComparer);

            newMinHeap.Initialize(this._collection.ToArray());
            return(newMinHeap);
        }
Ejemplo n.º 3
0
        public MinPriorityQueue(uint capacity, Comparer <PriorityQueueNode <TKey, TPriority> > priorityComparer)
        {
            // Make sure the TPriority is elegible for a priority
            if (!_validPriorityType())
            {
                throw new NotSupportedException("The priority type is not supported.");
            }

            // Initialize comparer
            if (priorityComparer == null)
            {
                _priorityComparer = Comparer <PriorityQueueNode <TKey, TPriority> > .Default;
            }
            else
            {
                _priorityComparer = priorityComparer;
            }

            // Initialize.
            _keys = new Dictionary <TKey, long>();
            _heap = new BinaryMinHeap <PriorityQueueNode <TKey, TPriority> >((int)capacity, this._priorityComparer);
        }