Update insert-an-element-into-a-max-heap.english.md (#34989)

Added solution to problem
This commit is contained in:
Dan B
2019-03-05 06:41:56 -08:00
committed by Tom
parent 28aecb41f8
commit 8bb76a4eb6

View File

@ -68,6 +68,24 @@ var MaxHeap = function() {
<section id='solution'>
```js
// solution required
var MaxHeap = function() {
// change code below this line
this.heap = [undefined];
this.insert = (ele) => {
var index = this.heap.length;
var arr = [...this.heap];
arr.push(ele);
while (ele > arr[Math.floor(index / 2)]) {
arr[index] = arr[Math.floor(index / 2)];
arr[Math.floor(index / 2)] = ele;
index = arr[Math.floor(index / 2)];
}
this.heap = arr;
}
this.print = () => {
return this.heap
}
// change code above this line
};
```
</section>