```
class MedianFinder {
private:
priority_queue<int, vector<int>, greater<int>> min_heap;
priority_queue<int, vector<int>, less<int>> max_heap;
public:
// Adds a number into the data structure.
void addNum(int num) {
if (min_heap.empty() || (num >= min_heap.top())) {
min_heap.emplace(num);
} else {
max_heap.emplace(num);
}
if (min_heap.size() > max_heap.size() + 1) {
max_heap.emplace(min_heap.top());
min_heap.pop();
} else if (max_heap.size() > min_heap.size()) {
min_heap.emplace(max_heap.top());
max_heap.pop();
}
}
// Returns the median of current data stream
double findMedian() {
return min_heap.size() == max_heap.size()
? 0.5 * (min_heap.top() + max_heap.top())
: min_heap.top();
}
};
```