I believe a simpler and cleaner implementation of the mxheap, minheap for median of a stream is as below:
from heapq import heappush, heappop
class MedianOfAStream:
def __init__(self):
self.xh, self.nh = [], [] #max, min heaps
def insert_num(self, num):
heappush(self.xh, -num)
heappush(self.nh, -heappop(self.xh))
if len(self.xh) < len(self.nh):
heappush(self.xh, -heappop(self.nh))
def find_median(self):
if len(self.xh) > len(self.nh):
return -self.xh[0]
return (-self.xh[0] + self.nh[0])/2.