-
Notifications
You must be signed in to change notification settings - Fork 43
/
MinHeap.py
118 lines (109 loc) · 4.43 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#---------------------------------------------------------------
# MinHeap (heap V2)
#---------------------------------------------------------------
# https://leetcode.com/explore/learn/card/heap/643/heap/4017/
# Implementing "Min Heap" (simulate heap via array, do "tricks" via "index")
class MinHeap:
def __init__(self, heapSize):
# Create a complete binary tree using an array
# Then use the binary tree to construct a Heap
self.heapSize = heapSize
# the number of elements is needed when instantiating an array
# heapSize records the size of the array
self.minheap = [0] * (heapSize + 1)
# realSize records the number of elements in the Heap
self.realSize = 0
# Function to add an element
def add(self, element):
self.realSize += 1
# If the number of elements in the Heap exceeds the preset heapSize
# print "Added too many elements" and return
if self.realSize > self.heapSize:
print("Added too many elements!")
self.realSize -= 1
return
# Add the element into the array
self.minheap[self.realSize] = element
# Index of the newly added element
index = self.realSize
"""
NOTE this !!!
"""
# Parent node of the newly added element
# Note if we use an array to represent the complete binary tree
# and store the root node at index 1
# index of the parent node of any node is [index of the node / 2]
# index of the left child node is [index of the node * 2]
# index of the right child node is [index of the node * 2 + 1]
parent = index // 2
# If the newly added element is smaller than its parent node,
# its value will be exchanged with that of the parent node
while (self.minheap[index] < self.minheap[parent] and index > 1):
self.minheap[parent], self.minheap[index] = self.minheap[index], self.minheap[parent]
index = parent
parent = index // 2
# Get the top element of the Heap
def peek(self):
return self.minheap[1]
# Delete the top element of the Heap
def pop(self):
# If the number of elements in the current Heap is 0,
# print "Don't have any elements" and return a default value
if self.realSize < 1:
print("Don't have any element!")
return sys.maxsize
else:
"""
NOTE this !!!
"""
# When there are still elements in the Heap
# self.realSize >= 1
removeElement = self.minheap[1]
# Put the last element in the Heap to the top of Heap
self.minheap[1] = self.minheap[self.realSize]
self.realSize -= 1
index = 1
# When the deleted element is not a leaf node
while (index <= self.realSize // 2):
# the left child of the deleted element
left = index * 2
# the right child of the deleted element
right = (index * 2) + 1
# If the deleted element is larger than the left or right child
# its value needs to be exchanged with the smaller value
# of the left and right child
if (self.minheap[index] > self.minheap[left] or self.minheap[index] > self.minheap[right]):
if self.minheap[left] < self.minheap[right]:
self.minheap[left], self.minheap[index] = self.minheap[index], self.minheap[left]
index = left
else:
self.minheap[right], self.minheap[index] = self.minheap[index], self.minheap[right]
index = right
else:
break
return removeElement
# return the number of elements in the Heap
def size(self):
return self.realSize
def __str__(self):
return str(self.minheap[1 : self.realSize + 1])
if __name__ == "__main__":
# Test cases
minHeap = MinHeap(5)
minHeap.add(3)
minHeap.add(1)
minHeap.add(2)
# [1,3,2]
print(minHeap)
# 1
print(minHeap.peek())
# 1
print(minHeap.pop())
# 2
print(minHeap.pop())
# 3
print(minHeap.pop())
minHeap.add(4)
minHeap.add(5)
# [4,5]
print(minHeap)