forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_heap.rb
More file actions
56 lines (51 loc) · 1.35 KB
/
Copy pathmax_heap.rb
File metadata and controls
56 lines (51 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require_relative './monkey_patch'
using MonkeyPatch
module Heap
module MaxHeap
# Public: Ensures the max-heap property is being maintained from the index provided
# Recursive, Max-Heap property A[parent] >= A[left] as well as A[right]
#
# ARGS:
# arr - Input array
# i - Index at which the Max-Heap property is to be applied
#
# RETURN: nil
#
# COMPLEXITY: Θ(lgn)
#
# Examples
# arr = [5, 3, 8, 7, 9, 6, 2, 4, 1]
# max_heapify(arr, 5)
#
# Modifies the provided array.
def self.max_heapify(arr, i)
arr.heap_size ||= arr.length
l = i.left
r = i.right
largest = (l <= arr.heap_size-1 && arr[l] > arr[i]) ? l : i
largest = r if (r <= arr.heap_size-1 && arr[r] > arr[largest])
if largest != i
arr[i], arr[largest] = arr[largest], arr[i]
max_heapify(arr, largest)
end
end
# Public: Re-order the input array to adhere to the Max-Heap property at all indices
#
# ARGS:
# arr - Input array
#
# RETURN: nil
#
# Examples
# arr = [5, 3, 8, 7, 9, 6, 2, 4, 1]
# build_max_heap(arr)
#
# Modifies the provided array.
def self.build_max_heap(arr)
arr.heap_size = arr.length
(0..arr.length.half).reverse_each do |i|
max_heapify(arr, i)
end
end
end
end