public void InsertRandomElement(int randomvalue, MaxHeap <int> maxheap, MinHeap <int> minheap) { if (maxheap.Size() == minheap.Size()) { if (minheap.Size() > 0 && randomvalue > minheap.Peek()) { //if the coming random value is greater than the minheap peek maxheap.Insert(minheap.PopRoot()); minheap.Insert(randomvalue); } else { maxheap.Insert(randomvalue); } } else { //if the Size() is not equal means the max heap will be bigger if (randomvalue < maxheap.Peek()) { minheap.Insert(maxheap.PopRoot()); maxheap.Insert(randomvalue); } else { minheap.Insert(randomvalue); } } }
public double CalculateMedian(MaxHeap <int> maxheap, MinHeap <int> minheap) { //check for empty..if maxheap is empty then minheap will also be empty if (maxheap.Size() == 0) { return(0); } if (maxheap.Size() == minheap.Size()) { //mean there are even number of item..median = (sum of middle two items)/2 return(Convert.ToDouble((maxheap.Peek() + minheap.Peek()) / 2)); } else { return(maxheap.Peek()); } }