mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-25 09:50:59 -04:00
Change the dir structure slightly
This commit is contained in:
parent
6b6fe21db3
commit
2f4a9638c0
184 changed files with 0 additions and 21 deletions
|
@ -1,49 +0,0 @@
|
|||
#!/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