3
3
4
4
class MaxHeap (object ):
5
5
6
- HEAP_SIZE = 10
7
-
8
- def __init__ (self ):
9
- self .heap = [0 ]* MaxHeap .HEAP_SIZE
10
- self .currentPosition = - 1
6
+ def __init__ (self , maxSize = None ):
7
+ self .heap = [float ("inf" )] # heap starting from index 1
8
+ self .HEAP_SIZE = maxSize
11
9
12
10
def _swap (self ,i ,j ):
13
11
self .heap [i ], self .heap [j ] = self .heap [j ], self .heap [i ]
14
12
15
13
def _heapIsFull (self ):
16
- if self .currentPosition == MaxHeap .HEAP_SIZE :
17
- return True
18
- return False
14
+ return (self .HEAP_SIZE != None and len (self .heap ) >= self .HEAP_SIZE )
19
15
20
16
def insert (self , item ):
21
17
if self ._heapIsFull ():
22
18
"Heap is full..."
23
- return
24
- self .currentPosition += 1
25
- self .heap [self .currentPosition ] = item
26
- # adjust parent node item
27
- self ._bubbleUp (self .currentPosition )
19
+ else :
20
+ self .heap .append (item )
21
+ # adjust parent node item
22
+ self ._bubbleUp (len (self .heap )- 1 )
28
23
29
24
def _bubbleUp (self , currentPosition ):
30
- index = currentPosition
31
- parrentIndex = index // 2
25
+ if currentPosition >= 2 : # no need to do bubbleUp for 1 element
26
+ index = currentPosition
27
+ parrentIndex = index // 2
32
28
33
- if parrentIndex >= 0 and self .heap [parrentIndex ] < self .heap [index ]:
34
- self ._swap (parrentIndex , index )
35
- # index = parrentIndex
36
- self ._bubbleUp (parrentIndex )
29
+ if parrentIndex >= 0 and self .heap [parrentIndex ] < self .heap [index ]:
30
+ self ._swap (parrentIndex , index )
31
+ self ._bubbleUp (parrentIndex )
37
32
38
-
39
- def getMax (self ):
40
- if self .heap [0 ]:
41
- return self .heap [0 ]
42
- else :
43
- return "Empty Heap"
33
+ def peek (self ):
34
+ return self .heap [1 ] if len (self .heap ) > 1 else False
44
35
45
36
def pop (self ):
46
- if self .currentPosition > 1 :
47
- # swap first element with last element
48
- self ._swap (0 ,self .currentPosition )
49
- del self .heap [self .currentPosition ]
50
- # adjust parent node item
51
- self ._bubbleDown (0 )
52
- elif self .currentPosition == 1 :
53
- del self .heap [0 ]
54
- else :
55
- print "No deletion operation for empty heap"
56
- return "False"
37
+ element = self .peek ()
38
+ if element :
39
+ self ._swap (1 , len (self .heap ) - 1 )
40
+ self .heap .pop ()
41
+ self ._bubbleDown (1 )
42
+ return element
57
43
58
44
def _bubbleDown (self , index ):
59
- left = 2 * index + 1
60
- right = 2 * index + 2
45
+ leftChildIndex = 2 * index
46
+ rightChildIndex = 2 * index + 1
61
47
largest = index
62
- if len (self .heap ) > left and self .heap [largest ] < self .heap [left ]:
63
- largest = left
64
- if len (self .heap ) > right and self .heap [largest ] < self .heap [right ]:
65
- largest = right
48
+ if len (self .heap ) > leftChildIndex and self .heap [largest ] < self .heap [leftChildIndex ]:
49
+ largest = leftChildIndex
50
+ if len (self .heap ) > rightChildIndex and self .heap [largest ] < self .heap [rightChildIndex ]:
51
+ largest = rightChildIndex
66
52
if largest != index :
67
53
self ._swap (index , largest )
68
54
self ._bubbleDown (largest )
69
-
70
- def printHeap (self ):
71
- all_hp_element = []
72
- for i in self .heap :
73
- if i != 0 :
74
- all_hp_element .append (i )
75
- return all_hp_element
76
-
77
-
0 commit comments