Skip to content

Commit

Permalink
#67 min_heapify
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtask committed Oct 1, 2024
1 parent 1005c3b commit 2d30246
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
Empty file.
Empty file.
25 changes: 25 additions & 0 deletions src/solutions/chapter6/section2/exercise3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from book.chapter6.section1 import left
from book.chapter6.section1 import right


def min_heapify(A, i: int) -> None:
"""Restores the min-heap property violated by a single node.
Implements:
Min-Heapify
Args:
A: the array representing a min-heap in which the min-heap property is violated by a single node
i: the index of the node in A that is not smaller than either of its children
"""
l = left(i)
r = right(i)
if l <= A.heap_size and A[l] < A[i]:
smallest = l
else:
smallest = i
if r <= A.heap_size and A[r] < A[smallest]:
smallest = r
if smallest != i:
A[i], A[smallest] = A[smallest], A[i]
min_heapify(A, smallest)
40 changes: 40 additions & 0 deletions test/test_solutions/test_chapter6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from hypothesis import given
from hypothesis import strategies as st
from hypothesis.strategies import integers
from hypothesis.strategies import lists

from book.chapter6.section1 import build_max_heap
from book.data_structures import Array
from solutions.chapter6.section2.exercise3 import min_heapify
from test_case import ClrsTestCase
from test_util import create_array
from util import range_of


def build_min_heap_by_inversion(A: Array, n: int) -> None:
"""Relies on the fact that when we replace each element of a max heap by the element's opposite, the resulting array
is a min heap."""
build_max_heap(A, n)
for i in range_of(1, to=n):
A[i] = -A[i]


class TestChapter6(ClrsTestCase):

@given(st.data())
def test_min_heapify(self, data):
elements = data.draw(lists(integers(), min_size=1))
n = len(elements)
A = create_array([-x for x in elements])
build_min_heap_by_inversion(A, n)
self.assertMinHeap(A)
new_root = data.draw(integers(max_value=A[1] - 1))
elements.remove(A[1])
elements.append(new_root)
A[1] = new_root # possibly violate the min-heap property at the root

min_heapify(A, 1)

self.assertEqual(A.heap_size, n)
self.assertMinHeap(A)
self.assertArrayPermuted(A, elements, end=n)

0 comments on commit 2d30246

Please sign in to comment.