fix max heap insert function (#43285)
This commit is contained in:
@ -133,20 +133,28 @@ var MaxHeap = function() {
|
|||||||
```js
|
```js
|
||||||
var MaxHeap = function() {
|
var MaxHeap = function() {
|
||||||
// Only change code below this line
|
// Only change code below this line
|
||||||
this.heap = [null];
|
this.heap = [];
|
||||||
this.insert = (ele) => {
|
this.parent = index => {
|
||||||
var index = this.heap.length;
|
return Math.floor((index - 1) / 2);
|
||||||
var arr = [...this.heap];
|
}
|
||||||
arr.push(ele);
|
this.insert = element => {
|
||||||
while (ele > arr[Math.floor(index / 2)] && index > 1) {
|
this.heap.push(element);
|
||||||
arr[index] = arr[Math.floor(index / 2)];
|
this.heapifyUp(this.heap.length - 1);
|
||||||
arr[Math.floor(index / 2)] = ele;
|
}
|
||||||
index = arr[Math.floor(index / 2)];
|
this.heapifyUp = index => {
|
||||||
}
|
let currentIndex = index,
|
||||||
this.heap = arr;
|
parentIndex = this.parent(currentIndex);
|
||||||
|
while (currentIndex > 0 && this.heap[currentIndex] > this.heap[parentIndex]) {
|
||||||
|
this.swap(currentIndex, parentIndex);
|
||||||
|
currentIndex = parentIndex;
|
||||||
|
parentIndex = this.parent(parentIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.swap = (index1, index2) => {
|
||||||
|
[this.heap[index1], this.heap[index2]] = [this.heap[index2], this.heap[index1]];
|
||||||
}
|
}
|
||||||
this.print = () => {
|
this.print = () => {
|
||||||
return this.heap.slice(1);
|
return this.heap;
|
||||||
}
|
}
|
||||||
// Only change code above this line
|
// Only change code above this line
|
||||||
};
|
};
|
||||||
|
@ -114,21 +114,29 @@ assert(
|
|||||||
## --seed-contents--
|
## --seed-contents--
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var MaxHeap = function() {
|
var MaxHeap = function () {
|
||||||
this.heap = [null];
|
this.heap = [];
|
||||||
this.insert = (ele) => {
|
this.parent = index => {
|
||||||
var index = this.heap.length;
|
return Math.floor((index - 1) / 2);
|
||||||
var arr = [...this.heap];
|
}
|
||||||
arr.push(ele);
|
this.insert = element => {
|
||||||
while (ele > arr[Math.floor(index / 2)] && index > 1) {
|
this.heap.push(element);
|
||||||
arr[index] = arr[Math.floor(index / 2)];
|
this.heapifyUp(this.heap.length - 1);
|
||||||
arr[Math.floor(index / 2)] = ele;
|
}
|
||||||
index = arr[Math.floor(index / 2)];
|
this.heapifyUp = index => {
|
||||||
|
let currentIndex = index,
|
||||||
|
parentIndex = this.parent(currentIndex);
|
||||||
|
while (currentIndex > 0 && this.heap[currentIndex] > this.heap[parentIndex]) {
|
||||||
|
this.swap(currentIndex, parentIndex);
|
||||||
|
currentIndex = parentIndex;
|
||||||
|
parentIndex = this.parent(parentIndex);
|
||||||
}
|
}
|
||||||
this.heap = arr;
|
}
|
||||||
|
this.swap = (index1, index2) => {
|
||||||
|
[this.heap[index1], this.heap[index2]] = [this.heap[index2], this.heap[index1]];
|
||||||
}
|
}
|
||||||
this.print = () => {
|
this.print = () => {
|
||||||
return this.heap.slice(1);
|
return this.heap;
|
||||||
}
|
}
|
||||||
// Only change code below this line
|
// Only change code below this line
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user