Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08a969e

Browse files
committed
convert min heap to max heap
1 parent fc1db23 commit 08a969e

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎Data-Structure/Heap/minHeapToMaxHeap.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
function maxHeapify(root, n, arr) {
3+
let largest = root;
4+
let l = (root * 2) + 1;
5+
let r = (root * 2) + 2;
6+
if (l < n && arr[l] > arr[largest]) largest = l;
7+
if (r < n && arr[r] > arr[largest]) largest = r;
8+
if (largest != root) {
9+
let temp = arr[root];
10+
arr[root] = arr[largest];
11+
arr[largest] = temp;
12+
maxHeapify(largest, n, arr);
13+
}
14+
}
15+
16+
function constructMaxHeap(arr) {
17+
for (let index = (arr.length - 2) / 2; index >= 0; index--)
18+
maxHeapify(index, arr.length, arr);
19+
}
20+
21+
let arr = [3, 5, 9, 6, 8, 20, 10, 12, 18, 9];
22+
console.log('Min Heap - ', arr.join(', '));
23+
constructMaxHeap(arr);
24+
console.log('Max Heap - ', arr.join(', '));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /