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 2d6ce03

Browse files
authored
bug fixes
1 parent 4119146 commit 2d6ce03

File tree

1 file changed

+50
-22
lines changed

1 file changed

+50
-22
lines changed

‎max-heap.js

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,61 +23,89 @@ const isRootNode = index => {
2323
return index === 0;
2424
};
2525

26-
2726
const isEmpty = (element) => {
2827
return element === undefined || element === null;
2928
}
3029

31-
3230
const heapifyUp = (array, index) => {
31+
index = index || array.length - 1;
3332
const parentIndex = getParentIndex(index);
3433
if (parentIndex >= 0 && array[index] > array[parentIndex]) {
3534
swap(array, index, parentIndex);
3635
heapifyUp(array, parentIndex);
3736
}
3837
}
3938

40-
4139
const heapifyDown = (array, index) => {
4240
const element = array[index];
4341
const leftChildIndex = getLeftChildIndex(index);
4442
const rightChildIndex = getRightChildIndex(index);
4543
const leftChildElement = array[leftChildIndex];
4644
const rightChildElement = array[rightChildIndex];
45+
let maxIndex = null;
46+
4747
if (isEmpty(leftChildElement) && isEmpty(rightChildElement)) {
4848
return;
4949
}
50+
51+
if (isEmpty(leftChildElement)) {
52+
maxIndex = rightChildIndex;
53+
}
54+
else if (isEmpty(rightChildElement)) {
55+
maxIndex = leftChildIndex;
56+
}
5057
else {
51-
if (element < leftChildElement || element < rightChildElement) {
52-
const maxIndex = getMaxIndex(array, leftChildIndex, rightChildIndex);
53-
swap(array, index, maxIndex);
54-
heapifyDown(array, maxIndex);
55-
}
58+
maxIndex = getMaxIndex(array, leftChildIndex, rightChildIndex);
5659
}
57-
}
5860

61+
if (element <= leftChildElement || element < rightChildElement) {
62+
swap(array, index, maxIndex);
63+
heapifyDown(array, maxIndex);
64+
}
65+
66+
}
5967

60-
// delete the root node
6168
const deleteNode = (array) => {
6269
swap(array, 0, array.length - 1);
6370
array.length = array.length - 1;
64-
heapifyDown(array, 0)
71+
heapifyDown(array, 0);
6572
}
6673

6774
const insertNode = (array, value) => {
6875
array.push(value);
69-
heapifyUp(array,array.length-1)
76+
heapifyUp(array)
7077
}
7178

7279
const buildHeap = (array, element) => {
73-
for (let index = array.length -1; index >= array.length /2; index--) {
80+
for (let index = Math.floor(array.length /2); index <= array.length -1; index++) {
7481
heapifyUp(array, index);
7582
}
7683

84+
// for (let index = Math.floor(array.length / 2); index >= 0; index--) {
85+
// heapifyDown(array, index);
86+
// }
7787
}
7888

79-
// const heap = [0, 1, 2, 3, 4, 5, 6];
80-
// buildHeap(heap);
89+
90+
const heapSort = (array) => {
91+
buildHeap(array);
92+
const sortedArray = [];
93+
while (array.length > 0) {
94+
swap(array, 0, array.length - 1);
95+
sortedArray[array.length - 1] = array[array.length - 1];
96+
array.length = array.length - 1;
97+
heapifyDown(array, 0);
98+
}
99+
100+
array.push(...sortedArray);
101+
sortedArray.length = 0;
102+
}
103+
104+
// Arr[ N/2+1 ] to Arr[ N ] are the leaf nodes
105+
106+
const array = [4, 1, 5, 3, 0, 2, 6];
107+
// heapSort(array);
108+
// buildHeap(array);
81109
// const heap = [6, 4, 5, 3, 1, 0, 2];
82110
// deleteNode(array);
83111
// const heap = [];
@@ -89,12 +117,12 @@ const buildHeap = (array, element) => {
89117
// insertNode(heap, 2, heap.length);
90118
// insertNode(heap, 3, heap.length);
91119
// console.log(heap);
92-
// deleteNode(heap);
93-
// deleteNode(heap);
94-
// deleteNode(heap);
95-
// console.log(heap);
96-
// insertNode(heap, 2);
97-
// insertNode(heap, 3);
98-
// console.log(heap);
120+
// deleteNode(array);
121+
// deleteNode(array);
122+
// deleteNode(array);
123+
// console.log(array);
124+
// insertNode(array, 2);
125+
// insertNode(array, 3);
126+
console.log(array);
99127

100128

0 commit comments

Comments
(0)

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