/** initialize data structure */ public MedianFinder() { lowHeap = new MaxHeap(new List <int>()); lowHeap.BuildMaxHeap(); highHeap = new MinHeap(new List <int>()); highHeap.BuildMinHeap(); even = true; }
public void BuildMaxHeap() { var array = new int[] { 15, 8, 65, 1, 52, 24 }; var expected = new int[] { 65, 52, 24, 1, 8, 15 }; var heap = new MaxHeap(); heap.BuildMaxHeap(array); CollectionAssert.AreEqual(expected, array); }
public void DeleteRoot() { var array = new int[] { 15, 8, 65, 1, 52, 24 }; var expected = new int[] { 52, 15, 24, 1, 8, int.MinValue }; var heap = new MaxHeap(); heap.BuildMaxHeap(array); heap.Delete(array, 0); CollectionAssert.AreEqual(expected, array); }
public void Sort(int[] array) { var heap = new MaxHeap(); //O(n); heap.BuildMaxHeap(array); for (int i = 0; i < array.Length; i++) { Swap(array, 0, array.Length - 1 - i); heap.MaxHeapify(array, 0, (array.Length - 1 - i)); } }
public void BuildMaxHeapTest() { //Array in ascending order. int[] elements = { 3, 5, 7, 8, 9, 10, 11 }; MaxHeap maxHeap = new MaxHeap(); maxHeap.BuildMaxHeap(elements); Console.Write("Max Heap: "); for (int index = 0; index < elements.Length - 1; index++) { Console.Write(elements[index].ToString() + '\t'); } }
public void BuildMaxHeapGeneratesCorrectMaxHeap() { heap.BuildMaxHeap(); var validData = new int[6] { 6, 4, 5, 1, 0, 2 }; for (int i = 0; i < validData.Length; i++) { if (validData[i] != heap.Data[i]) { Assert.Fail("Heap has not generated correct Max Heap order"); } } }
public void DecreaseKeyTest() { //Array in ascending order. int[] elements = { 3, 5, 7, 8, 9, 10, 11 }; MaxHeap maxHeap = new MaxHeap(); maxHeap.BuildMaxHeap(elements); Console.Write("Max Heap: "); for (int index = 0; index <= elements.Length - 1; index++) { Console.Write(elements[index].ToString() + '\t'); } // Attempts to increase the prority from 5 to 15. maxHeap.Heap_Decrease_Key(elements, 2, 2); Console.WriteLine(); Console.Write("New Max Heap post decreasing the Key: "); for (int index = 0; index <= elements.Length - 1; index++) { Console.Write(elements[index].ToString() + '\t'); } }
private void button24_Click(object sender, EventArgs e) { Form1 f1 = (Form1)Application.OpenForms["Form1"]; f1.GB_Status_AppendText_Nextline("--------------------------------", Color.Black); //MinHeap mh = new MinHeap(15); MaxHeap <int> mh = new MaxHeap <int>(15); mh.Insert(77); mh.Insert(7); mh.Insert(64); mh.Insert(21); mh.Insert(89); mh.Insert(1); mh.Insert(92); mh.Insert(17); mh.Insert(3); mh.Insert(30); mh.Insert(42); mh.Insert(50); mh.Insert(2); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("1) mh.getMax() : " + mh.getMax(), Color.Red); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("--------------------------------", Color.Black); int[] arr = { 77, 7, 64, 21, 89, 1, 92, 17, 3, 30, 42, 50, 2 }; mh.BuildMaxHeap(arr); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); f1.GB_Status_AppendText_Nextline("2) mh.getMax() : " + mh.getMax(), Color.Green); mh.RemoveMax(); mh.displayHeap(); }
public void Init() { heap = new MaxHeap <int>(heapData); heap.BuildMaxHeap(); }