/// <summary> /// Merge two heaps and return 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>Return 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)); }
static void Main(string[] args) { #region Stack //Stack stack = new Stack(5); //stack.Push(10); //stack.Push(50); //stack.Push(80); //stack.PrintStack(); #endregion #region queue //Queue queue = new Queue(); //queue.Enqueue(10); //queue.Enqueue(30); //queue.Enqueue(40); //queue.Dequeue(); //queue.Dequeue(); #endregion #region Heap int[] arr1 = { 10, 64, 7, 52, 32, 18, 2, 48 }; int[] arr2 = { 30, 5, 6, 33 }; int n = arr1.Length; BinaryHeap binaryHeap = new BinaryHeap(); //BinaryHeap.MaxHeapHelper(); //BinaryHeap.MinHeapHelper(); binaryHeap.MergeHeap(arr1, arr2); #endregion Console.ReadLine(); }
public static void MinHeapHelper() { int[] arr = { 4, 5, 1, 6, 7, 3, 2 }; Console.Write("Input Array for the MaxHeap problem : "); ArrayHelpers.PrintIntArray(arr); BinaryHeap minHeap = new BinaryHeap(); minHeap.BuildMinHeap(arr); int?minValue = minHeap.Maximum(arr); Console.WriteLine("Minimum value in the heap is : {0}", minValue); int extractMin = minHeap.ExtractMinimum(arr); Console.WriteLine("Extract Minimum value in the heap is : {0}", extractMin); Console.Write("Array after ExtractMinimum : "); ArrayHelpers.PrintIntArray(arr); minHeap.MinHeapInsert(arr, 1); Console.Write("Array after Inserting 0 value : "); ArrayHelpers.PrintIntArray(arr); //int[] arr = { 10, 8, 9, 7, 6, 5, 4 }; //Console.Write("Input Array for the problem : "); //ArrayHelpers.PrintIntArray(arr); //BinaryHeap heap = new BinaryHeap(); //// heap.PerformHeapSort(arr); //heap.BuildMinHeap(arr); }
public PriorityQueue(bool descending = false) { _data = new BinaryHeap <ItemContainer>(descending); }