-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminheap.py
55 lines (48 loc) · 1.13 KB
/
minheap.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
# The Heap with Heap property = min
import heapq
class MinHeap:
"""
Creates an array.
"""
def __init__(self):
self.array = []
"""
True iff the item is in the array.
"""
def contains(self, key):
for x in self.array:
if x == key:
return True
return False
"""
Adds the item to the array.
"""
def add(self, key):
heapq.heappush(self.array, key)
"""
Removes the item from the array.
"""
def remove(self, key):
#stack = []
#while (True):
# val = heapq.heappop(self.array)
# stack.append(val)
# if val == key:
# break
#toReturn = stack.pop()
#for x in stack:
# self.add(x)
#return toReturn
return 0
"""
Returns the item at index index
"""
def get(self, index):
#stack = []
#for x in range(index-1):
# stack.append(heapq.heappop(self.array))
#toReturn = heapq.heappop(self.array)
#for x in stack:
# self.add(x)
#return toReturn
return 0