Add previously written methods to challenge seed in "Remove an Element from a Max Heap" (#38834)
* Add previous methods to challenge seed * fix: add suggested changes Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
This commit is contained in:
parent
77bf5630f0
commit
b45e6ecae0
@ -70,12 +70,12 @@ var MaxHeap = function() {
|
||||
```js
|
||||
var MaxHeap = function() {
|
||||
// change code below this line
|
||||
this.heap = [undefined];
|
||||
this.heap = [null];
|
||||
this.insert = (ele) => {
|
||||
var index = this.heap.length;
|
||||
var arr = [...this.heap];
|
||||
arr.push(ele);
|
||||
while (ele > arr[Math.floor(index / 2)]) {
|
||||
while (ele > arr[Math.floor(index / 2)] && index > 1) {
|
||||
arr[index] = arr[Math.floor(index / 2)];
|
||||
arr[Math.floor(index / 2)] = ele;
|
||||
index = arr[Math.floor(index / 2)];
|
||||
@ -83,7 +83,7 @@ var MaxHeap = function() {
|
||||
this.heap = arr;
|
||||
}
|
||||
this.print = () => {
|
||||
return this.heap
|
||||
return this.heap.slice(1);
|
||||
}
|
||||
// change code above this line
|
||||
};
|
||||
|
@ -18,7 +18,7 @@ Now that we can add elements to our heap let's see how we can remove elements. R
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Instructions: Add a method to our max heap called <code>remove</code>. This method should return the greatest value that has been added to our max heap and remove it from the heap. It should also reorder the heap so the heap property is maintained. After removing an element, the next greatest element remaining in the heap should become the root. Add your <code>insert</code> method again here as well.
|
||||
Instructions: Add a method to our max heap called <code>remove</code>. This method should return the greatest value that has been added to our max heap and remove it from the heap. It should also reorder the heap so the heap property is maintained. After removing an element, the next greatest element remaining in the heap should become the root.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
@ -47,7 +47,23 @@ tests:
|
||||
|
||||
```js
|
||||
var MaxHeap = function() {
|
||||
this.heap = [null];
|
||||
this.insert = (ele) => {
|
||||
var index = this.heap.length;
|
||||
var arr = [...this.heap];
|
||||
arr.push(ele);
|
||||
while (ele > arr[Math.floor(index / 2)] && index > 1) {
|
||||
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.slice(1);
|
||||
}
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
};
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user