private void button6_Click(object sender, EventArgs e) { string[] input = this.textBox8.Text.Split(','); MinHeap <KarthicStringComparer> minheap = new MinHeap <KarthicStringComparer>(new KarthicStringComparer()); foreach (string s in input) { minheap.Insert(new KarthicStringComparer(s)); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.Length; i++) { sb.Append(minheap.PopRoot().StringOfX).Append(","); } this.textBox7.Text = sb.ToString(); }
private void button3_Click(object sender, EventArgs e) { int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox1.Text); //Assumption: //1) Store the values lesser than median in max heap and keep the root with the max value //2) Store the values greater than the median in min heap //3) Maxheap >= Minheap //* Use a max heap and a min heap to maintain the stream, where //* maxheap.size() == minheap.size() or //* maxheap.size() - 1 == minheap.size() //* always holds. // Similar to balancing BST in Method 2 above, we can use a max heap on left side to represent elements that are less than effective median, and a min heap on right side to represent elements that are greater than effective median. //After processing an incoming element, the number of elements in heaps differ utmost by 1 element. When both heaps contain same number of elements, we pick average of heaps root data as effective median. When the heaps are not balanced, we select effective median from the root of heap containing more elements. MinHeap <int> minheap = new MinHeap <int>(new KarthicMinHeapComparer2()); MaxHeap <int> maxheap = new MaxHeap <int>(new KarthicMaxHeapComparer()); foreach (int i in input) { InsertRandomElement(i, maxheap, minheap); } //Median Calcualtion //If there are odd total numbers, maxheap will be greater //if there are even total number then minheap == maxheap this.textBox2.Text = CalculateMedian(maxheap, minheap).ToString(); }