You can do operation (1) in O(logN) and (2) in O(1) using two binary heaps (maybe you can do operation (3) fast too, I'm just not very fluent in probability theory).
Create a max-heap L
and a min-heap R
. Create two variables a
and b
that will store one (or two) middle elements. Heap L
will store all elements lesser than middle one(s), heap R
will store all elements greater than middle one(s). Size of L
and R
will always be the same.
Insert(x
):
- If the count of elements in data structure is odd (
a
stores middle element, b
stores nothing):
- If the count of elements in data structure is even (
a
and b
store two middle elements):
- If
x < a
:
- Put
b
into R
- Put
x
into L
b = null
- If
x > b
:
- Put
x
into R
- Put
a
into L
a = b
b = null
- If
a <= x <= b
:
- Put
a
into L
- Put
b
into R
a = x
b = null
GetMedian():
- Just return
a
(if count of elements is odd) or (a + b) / 2
(of count of elements is even)