|
|
|
|
21
|
"""
|
21
|
"""
|
22
|
def max_heapify(lista, idx: int, heap_size: int):
|
22
|
def max_heapify(lista, idx: int, heap_size: int):
|
23
|
"""Convertir el nodo `idx` y sus descendientes en un max heap."""
|
23
|
"""Convertir el nodo `idx` y sus descendientes en un max heap."""
|
24
|
- left_idx: int = 2 * idx + 1
|
|
|
25
|
- right_idx: int = 2 * idx + 2
|
|
|
26
|
- largestval_idx: int = idx
|
|
|
|
|
24
|
+ left_idx = 2 * idx + 1
|
|
|
25
|
+ right_idx = 2 * idx + 2
|
|
|
26
|
+ largestval_idx = idx
|
27
|
if left_idx < heap_size and lista[idx] < lista[left_idx]:
|
27
|
if left_idx < heap_size and lista[idx] < lista[left_idx]:
|
28
|
largestval_idx = left_idx
|
28
|
largestval_idx = left_idx
|
29
|
if right_idx < heap_size and lista[largestval_idx] < lista[right_idx]:
|
29
|
if right_idx < heap_size and lista[largestval_idx] < lista[right_idx]:
|
|
|
|
|
32
|
lista[idx], lista[largestval_idx] = lista[largestval_idx], lista[idx]
|
32
|
lista[idx], lista[largestval_idx] = lista[largestval_idx], lista[idx]
|
33
|
max_heapify(lista, largestval_idx, heap_size)
|
33
|
max_heapify(lista, largestval_idx, heap_size)
|
34
|
|
34
|
|
35
|
- def build_max_heap(lista, heap_size: int):
|
|
|
|
|
35
|
+ def build_max_heap(lista, heap_size):
|
36
|
"""Construir un max heap the un heap dado."""
|
36
|
"""Construir un max heap the un heap dado."""
|
37
|
for idx in range((heap_size - 1) // 2, -1, -1):
|
37
|
for idx in range((heap_size - 1) // 2, -1, -1):
|
38
|
max_heapify(lista, idx, heap_size)
|
38
|
max_heapify(lista, idx, heap_size)
|