Skip to content

Commit c977f2f

Browse files
committed
#70 max_heap_decrease_key
1 parent 73f5e03 commit c977f2f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from book.chapter6.section2 import max_heapify
2+
from book.data_structures import KeyObject
3+
from book.data_structures import PriorityQueue
4+
from solutions.chapter2.section1.exercise4 import linear_search
5+
6+
7+
def max_heap_decrease_key(A: PriorityQueue, x: KeyObject, k: int) -> None:
8+
"""Increases the value of the element's key to the new value.
9+
10+
Implements:
11+
Max-Heap-Decrease-Key
12+
13+
Args:
14+
A: a max-priority queue implemented by a max-heap containing elements of the dynamic set
15+
x: an element in A whose key to decrease
16+
k: a new value of the key, at most as large as x.key
17+
"""
18+
if k > x.key:
19+
raise ValueError('new key is larger than current key')
20+
x.key = k
21+
i = __find_index_of_object(A, x)
22+
max_heapify(A, i)
23+
24+
25+
def __find_index_of_object(A: PriorityQueue, x: KeyObject) -> int:
26+
return linear_search(A, A.heap_size, x)

test/test_solutions/test_chapter6.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from solutions.chapter6.section5.exercise3 import min_heap_extract_min
1919
from solutions.chapter6.section5.exercise3 import min_heap_insert
2020
from solutions.chapter6.section5.exercise3 import min_heap_minimum
21+
from solutions.chapter6.section5.exercise4 import max_heap_decrease_key
2122
from test_case import ClrsTestCase
2223
from test_util import create_heap
2324
from util import range_of
@@ -192,3 +193,42 @@ def test_min_heap_insert_overflow(self, data):
192193
self.assertEqual(A.heap_size, n)
193194
self.assertArrayPermuted(A, key_objects, end=n)
194195
self.assertPriorityQueueMappingConsistent(A)
196+
197+
@given(st.data())
198+
def test_max_heap_decrease_key(self, data):
199+
keys = data.draw(lists(integers(), min_size=1))
200+
key_objects = [KeyObject(key, data.draw(text())) for key in keys]
201+
heap = create_heap(key_objects)
202+
n = len(key_objects)
203+
build_max_heap(heap, n)
204+
A = PriorityQueue(heap, n)
205+
x = random.choice(key_objects)
206+
k = x.key - data.draw(integers(min_value=0))
207+
208+
max_heap_decrease_key(A, x, k)
209+
210+
self.assertEqual(x.key, k)
211+
self.assertEqual(A.heap_size, n)
212+
self.assertMaxHeap(A)
213+
self.assertArrayPermuted(A, key_objects, end=n)
214+
self.assertPriorityQueueMappingConsistent(A)
215+
216+
@given(st.data())
217+
def test_max_heap_decrease_key_invalid_key(self, data):
218+
keys = data.draw(lists(integers(), min_size=1))
219+
key_objects = [KeyObject(key, data.draw(text())) for key in keys]
220+
heap = create_heap(key_objects)
221+
n = len(key_objects)
222+
build_max_heap(heap, n)
223+
A = PriorityQueue(heap, n)
224+
x = random.choice(key_objects)
225+
k = x.key + data.draw(integers(min_value=1))
226+
227+
with self.assertRaisesRegex(ValueError, "new key is larger than current key"):
228+
max_heap_decrease_key(A, x, k)
229+
230+
self.assertNotEquals(x.key, k)
231+
self.assertMaxHeap(A)
232+
self.assertEqual(A.heap_size, n)
233+
self.assertArrayPermuted(A, key_objects, end=n)
234+
self.assertPriorityQueueMappingConsistent(A)

0 commit comments

Comments
 (0)