/// <summary> /// Gets the median based on the current elements in the stream /// </summary> /// <returns>median of the stream</returns> public double GetMedian() { if ((MinHeapInst.HeapSize + MaxHeapInst.HeapSize) % 2 != 0) { // odd number of elements in the stream return(MaxHeapInst.PeekMax()); } // even number of elements in the stream int mid1 = MaxHeapInst.PeekMax(); int mid2 = MinHeapInst.PeekMin(); return(mid1 + ((mid2 - mid1) / 2.0)); // This will prevent overflow }