-
Notifications
You must be signed in to change notification settings - Fork 2
/
heap.py
107 lines (89 loc) · 3.54 KB
/
heap.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
# Our Binary Heap
class BinaryHeap:
# Initialization
def __init__(self):
self.heap = []
self.size = 0
# Empty or not
def isEmpty(self):
return self.size == 0
# Get top element
def peek(self):
if self.isEmpty():
print("Heap is empty")
else:
return self.heap[0]
# Add method helper function
def __add(self, data):
# Add at last, increase size and shift up
self.heap.append(data)
self.size += 1
self.shiftUp()
# Add method to add all data in a list
def add(self, data):
if isinstance(data, int):
self.__add(data)
else:
for i in data:
self.__add(i)
# Remove the top element
def remove(self):
if self.isEmpty():
print("Heap is empty")
return
else:
# Add last data at first, reduce size and shift down
mindata = self.heap[0]
self.heap[0] = self.heap[-1]
self.size -= 1
self.shiftDown()
self.heap.pop() # Pop last element since it is added at first
return mindata # Return min data
def getLeftChildIndex(self, parentIndex):
return (2 * parentIndex) + 1
def getRightChildIndex(self, parentIndex):
return (2 * parentIndex) + 2
def getParentIndex(self, childIndex):
return (childIndex - 1) // 2
def hasLeftChild(self, index):
return self.getLeftChildIndex(index) < self.size
def hasRightChild(self, index):
return self.getRightChildIndex(index) < self.size
def hasParent(self, index):
return self.getParentIndex(index) >= 0 and index != 0
def leftChild(self, index):
return self.heap[self.getLeftChildIndex(index)]
def rightChild(self, index):
return self.heap[self.getRightChildIndex(index)]
def parent(self, index):
return self.heap[self.getParentIndex(index)]
# Swap two index elements
def swap(self, index1, index2):
temp = self.heap[index1]
self.heap[index1] = self.heap[index2]
self.heap[index2] = temp
# Shift up when adding an element
def shiftUp(self):
index = self.size - 1 # Get last index
# While it has a parent and if index data is less than its parent swap it
while(self.hasParent(index) and self.heap[index] < self.parent(index)):
self.swap(self.getParentIndex(index), index)
index = self.getParentIndex(index) # Change index
def shiftDown(self):
index = 0 # Top index
while(self.hasLeftChild(index)): # Since it is a complete binaty tree, we can traverse until there is left child
# Assume smaller child indes as left child, and if right child exists change it to right child
smallerChildIndex = self.getLeftChildIndex(index)
if self.hasRightChild(index) and self.rightChild(index) < self.leftChild(index):
smallerChildIndex = self.getRightChildIndex(index)
# Don't do anything if it is in the correc position
if self.heap[index] < self.heap[smallerChildIndex]:
break
else:
self.swap(index, smallerChildIndex) # Swap data
index = smallerChildIndex # Keep track of smaller index
if __name__ == '__main__':
heap = BinaryHeap()
heap.add([20,50,40,100,70,10])
print(heap.remove())
print(heap.heap)