Update insert-an-element-into-a-max-heap.english.md (#34989)
Added solution to problem
This commit is contained in:
@ -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>
|
||||
|
Reference in New Issue
Block a user