We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1d8ea5b commit fc1db23Copy full SHA for fc1db23
Data-Structure/Tree/Heap/checkHeap.js
@@ -0,0 +1,19 @@
1
+'use strict';
2
+function checkHeap(arr, root, n) {
3
+ if (root > (parseInt((n - 2) / 2))) return true;
4
+ let left = (root * 2) + 1;
5
+ let right = (root * 2) + 2;
6
+ if (arr[left] < arr[root] && arr[right] < arr[root])
7
+ return checkHeap(arr, left, n) && checkHeap(arr, right, n);
8
+
9
+ let result = false;
10
+ if ((left < n && arr[left] > arr[root])) result = false;
11
+ else if (left < n && arr[left] < arr[root]) result = true;
12
13
+ if (right < n && arr[right] > arr[root]) result = result & false;
14
+ else if (right < n && arr[right] < arr[root]) result = result & false;
15
16
+ return result;
17
+}
18
+let array = [90, 15, 10, 7, 12, 2, 9, 1, 6];
19
+console.log('Given Array is', checkHeap(array, 0, array.length) == true ? 'Heap' : 'not Heap');
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments