-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from book.chapter6.section1 import left | ||
from book.chapter6.section1 import right | ||
|
||
|
||
def iterative_max_heapify(A, i: int) -> None: | ||
"""Restores the max-heap property violated by a single node (an iterative version). | ||
Implements: | ||
Iterative-Max-Heapify | ||
Args: | ||
A: the array representing a max-heap in which the max-heap property is violated by a single node | ||
i: the index of the node in A that is not larger than either of its children | ||
""" | ||
while True: | ||
l = left(i) | ||
r = right(i) | ||
if l <= A.heap_size and A[l] > A[i]: | ||
largest = l | ||
else: | ||
largest = i | ||
if r <= A.heap_size and A[r] > A[largest]: | ||
largest = r | ||
if largest == i: | ||
return | ||
A[i], A[largest] = A[largest], A[i] | ||
i = largest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters