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:
Ty Mick 2020-09-03 11:40:23 -04:00 committed by GitHub
parent 77bf5630f0
commit b45e6ecae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -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
};

View File

@ -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
};
```