示例#1
0
        /// <summary>
        /// Merges two heaps into first one.
        /// Both heaps will be modified.
        ///
        /// Time complexity: O(N * lon(M + N)).
        ///
        /// Space complexity: O(N).
        ///
        /// M and N are sizes of both heaps.
        /// </summary>
        /// <param name="other">Another heap to merge from.</param>
        /// <returns>Returns modified heap with elements from both heaps.</returns>
        public BinaryHeap <T> Meld(BinaryHeap <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            while (!other.IsEmpty)
            {
                this.Add(other.ExtractMin());
            }

            return(this);
        }
示例#2
0
        /// <summary>
        /// Merges two heaps and returns new one.
        /// Initial heaps will not be modified.
        ///
        /// Time complexity: O(M + N).
        ///
        /// Space complexity: O(M + N).
        ///
        /// M and N are sizes of both heaps.
        /// </summary>
        /// <param name="other">Another heap to merge from.</param>
        /// <returns>Returns new binary heap containing elements from both heaps.</returns>
        public BinaryHeap <T> Merge(BinaryHeap <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            List <T> elements = new List <T>(this.Count + other.Count);

            foreach (var item in this.Items)
            {
                elements.Add(item);
            }

            foreach (var item in other.Items)
            {
                elements.Add(item);
            }

            return(new BinaryHeap <T>(elements, this.Comparer));
        }