-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_median_from_data_stream.py
73 lines (53 loc) · 1.38 KB
/
find_median_from_data_stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class RedBlackBST(object):
_RED = True
_BLACK = False
def __init__(self):
self.root = None
def put(self, val):
def select(self, rank):
pass
@property
def size(self)
if self.root is None:
return 0
return self.root.size
@staticmethod
def _put_to(root, val):
if root is None:
root = RedBlackTreeNode(val)
if val > root.val:
root.right = self._put_to(root.right, val)
def _rotate_left(node):
pass
def _rotate_right(node):
pass
def _flip_colors(node):
pass
class RedBlackTreeNode(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.size = 1
# True: red, False: black
self.color = True
class MedianFinder(object):
def __init__(self):
"""
Initialize your data structure here.
"""
def addNum(self, num):
"""
Adds a num into the data structure.
:type num: int
:rtype: void
"""
def findMedian(self):
"""
Returns the median of current data stream
:rtype: float
"""
# Your MedianFinder object will be instantiated and called as such:
# mf = MedianFinder()
# mf.addNum(1)
# mf.findMedian()