1
-
2
-
3
1
class ContinuousMedianHandler :
4
2
def __init__ (self ):
5
3
self .lowers = Heap (MAX_HEAP_FUNC , [])
6
4
self .greaters = Heap (MIN_HEAP_FUNC , [])
7
5
self .median = None
8
6
9
-
10
- def insert (self , number ):
7
+ def insert (self , number ):
11
8
if not self .lowers .length or number < self .lowers .peek ():
12
9
self .lowers .insert (number )
13
10
else :
14
11
self .greaters .insert (number )
15
12
self .rebalannceHeap ()
16
13
self .updateMedian ()
17
14
18
-
19
15
def rebalannceHeap (self ):
20
16
if self .lowers .length - self .greaters .length == 2 :
21
17
self .greaters .insert (self .lowers .remove ())
22
18
elif self .greaters .length - self .lowers .length == 2 :
23
19
self .lowers .insert (self .greaters .remove ())
24
20
25
-
26
21
def updateMedian (self ):
27
22
if self .lowers .length == self .greaters .length :
28
23
self .median = (self .lowers .peek () + self .greaters .peek ()) / 2
@@ -31,32 +26,26 @@ def updateMedian(self):
31
26
else :
32
27
self .median = self .greaters .peek ()
33
28
34
-
35
29
def getMedian (self ):
36
30
return self .median
37
31
38
32
39
-
40
-
41
-
42
33
class Heap :
43
34
def __init__ (self , comparisonFunc , array ):
44
35
self .heap = self .buildHeap (array )
45
36
self .comparisonFunc = comparisonFunc
46
- self .lenght = len (self .heap )
47
-
37
+ self .length = len (self .heap )
48
38
49
39
def buildHeap (self , array ):
50
40
firstParentIdx = (len (array ) - 2 ) // 2
51
41
for currentIdx in reversed (range (firstParentIdx + 1 )):
52
42
self .shiftDown (currentIdx , len (array ) - 1 , array )
53
43
return array
54
44
55
-
56
45
def shiftDown (self , currentIdx , endIdx , heap ):
57
46
childOneIdx = currentIdx * 2 + 1
58
47
while childOneIdx <= endIdx :
59
- childTwoIdx = currentIdx * 2 + 2 if currentIdx * 2 + 2 <= endIdx else - 1
48
+ childTwoIdx = currentIdx * 2 + 2 if currentIdx * 2 + 2 <= endIdx else - 1
60
49
if childTwoIdx != - 1 :
61
50
if self .comparisonFunc (heap [childTwoIdx ], heap [childOneIdx ]):
62
51
idxToSwap = childTwoIdx
@@ -71,9 +60,8 @@ def shiftDown(self, currentIdx, endIdx, heap):
71
60
else :
72
61
return
73
62
74
-
75
63
def shiftUp (self , currentIdx , heap ):
76
- parentIdx = (currentIdx - 1 ) // 2
64
+ parentIdx = (currentIdx - 1 ) // 2
77
65
while currentIdx > 0 :
78
66
if self .comparisonFunc (heap [currentIdx ], heap [parentIdx ]):
79
67
self .swap (currentIdx , parentIdx , heap )
@@ -82,32 +70,25 @@ def shiftUp(self, currentIdx, heap):
82
70
else :
83
71
return
84
72
85
-
86
73
def peek (self ):
87
74
return self .heap [0 ]
88
75
89
-
90
76
def remove (self ):
91
- self .swap (0 , self .lenght - 1 , self .heap )
77
+ self .swap (0 , self .length - 1 , self .heap )
92
78
valueToRemove = self .heap .pop ()
93
- self .lenght -= 1
94
- self .shiftDown (0 , self .lenght - 1 , self .heap )
79
+ self .length -= 1
80
+ self .shiftDown (0 , self .length - 1 , self .heap )
95
81
return valueToRemove
96
82
97
-
98
- def innsert (self , value ):
83
+ def insert (self , value ):
99
84
self .heap .append (value )
100
- self .lenght += 1
101
- self .shiftUp (self .lenght - 1 , self .heap )
102
-
85
+ self .length += 1
86
+ self .shiftUp (self .length - 1 , self .heap )
103
87
104
88
def swap (self , i , j , array ):
105
89
array [i ], array [j ] = array [j ], array [i ]
106
90
107
91
108
-
109
-
110
-
111
92
def MAX_HEAP_FUNC (a , b ):
112
93
return a > b
113
94
0 commit comments