-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild_heap_from_array.rb
53 lines (41 loc) · 1.18 KB
/
build_heap_from_array.rb
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
# build max heap from array
def heapify_max(arr, index)
return arr if arr.empty?
largest = index
left = (index * 2) + 1
right = (index * 2) + 2
# check if left exist, and value of left is greater then root
largest = left if left < arr.length && arr[left] > arr[largest]
largest = right if right < arr.length && arr[right] > arr[largest]
# swap if largest is not root index
if largest != index
arr[largest], arr[index] = arr[index], arr[largest]
# Recursively heapify the affected sub-tree
heapify_max(arr, largest)
end
end
def heapify_min(arr, index)
smallest = index
left = 2 * index + 1
right = 2 * index + 2
length = arr.length
smallest = left if left < length && arr[left] < arr[smallest]
smallest = right if right < length && arr[right] < arr[smallest]
if smallest != index
arr[smallest], arr[index] = arr[index], arr[smallest]
heapify_min(arr, smallest)
end
end
def build_heap(arr)
return arr if arr.empty?
# Find 1st non leaf
length = arr.length
start_index = length / 2 - 1
start_index.downto(0).each do |i|
heapify_max(arr, i)
end
end
arr = [1, 3, 5, 4, 6, 13,
10, 9, 8, 15, 17]
build_heap(arr)
puts arr.to_s