mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
49
ebook_src/abstract_structures/heap/heapify.py
Normal file
49
ebook_src/abstract_structures/heap/heapify.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
class Heapify(object):
|
||||
def __init__(self, data=None):
|
||||
self.data = data or []
|
||||
for i in range(len(data)//2, -1, -1):
|
||||
self.__max_heapify__(i)
|
||||
|
||||
def __repr__(self):
|
||||
return '{}'.format(self.data)
|
||||
|
||||
def parent(self, i):
|
||||
return i >> 1
|
||||
|
||||
def left_child(self, i):
|
||||
return (i << 1) + 1
|
||||
|
||||
def right_child(self, i):
|
||||
return (i << 1) + 2 # +2 instead of +1 because it's 0-indexed.
|
||||
|
||||
def __max_heapify__(self, i):
|
||||
largest = i
|
||||
left = self.left_child(i)
|
||||
right = self.right_child(i)
|
||||
n = len(self.data)
|
||||
largest = (left < n and self.data[left] > self.data[i]) and left or i
|
||||
largest = (right < n and self.data[right] > self.data[largest]) and right or largest
|
||||
if i is not largest:
|
||||
self.data[i], self.data[largest] = self.data[largest], self.data[i]
|
||||
self.__max_heapify__(largest)
|
||||
|
||||
def extract_max(self):
|
||||
n = len(self.data)
|
||||
max_element = self.data[0]
|
||||
self.data[0] = self.data[n - 1]
|
||||
self.data = self.data[:n - 1]
|
||||
self.__max_heapify__(0)
|
||||
return max_element
|
||||
|
||||
|
||||
def test_Heapify():
|
||||
l1 = [3, 2, 5, 1, 7, 8, 2]
|
||||
h = Heapify(l1)
|
||||
assert(h.extract_max() == 8)
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_Heapify()
|
Loading…
Add table
Add a link
Reference in a new issue