mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-25 09:50:59 -04:00
abstracted structures fixed
This commit is contained in:
parent
3fdbc2a605
commit
01703751f1
98 changed files with 305 additions and 856 deletions
|
@ -1,13 +1,13 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
#!/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)
|
||||
|
||||
|
@ -19,12 +19,12 @@ class Heapify(object):
|
|||
|
||||
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)
|
||||
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:
|
||||
|
@ -44,7 +44,6 @@ def test_Heapify():
|
|||
l1 = [3, 2, 5, 1, 7, 8, 2]
|
||||
h = Heapify(l1)
|
||||
assert(h.extract_max() == 8)
|
||||
print ("Tests Passed!")
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_Heapify()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue