diff --git a/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md b/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md index 7731f012f88..ac0f7d676d6 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md +++ b/curriculum/challenges/english/10-coding-interview-prep/data-structures/remove-an-element-from-a-max-heap.md @@ -88,49 +88,56 @@ The `remove` method should remove the greatest element from the max heap while m ```js function isHeap(arr, i, n) { - if (i >= (n - 1) / 2) { - return true; - } - if ( - arr[i] >= arr[2 * i + 1] && - arr[i] >= arr[2 * i + 2] && - isHeap(arr, 2 * i + 1, n) && - isHeap(arr, 2 * i + 2, n) - ) { - return true; - } - return false; + if( arr[i] < arr[2 * i + 1] || arr[i] < arr[2 * i + 2] ){ + return false; + } + if (i > (n - 1) / 2) { + return true; + } + if (isHeap(arr, 2 * i + 1, n) && isHeap(arr, 2 * i + 2, n)) { + return true; + } + return false; } + assert( (function () { let test = false; + if (typeof MaxHeap !== 'undefined') { test = new MaxHeap(); } else { return false; } - let max = Infinity; - const [result, vals] = [[], [2, 15, 3, 7, 12, 7, 10, 90]]; - vals.forEach((val) => test.insert(val)); - for (let i = 0; i < vals.length; i++) { - const curHeap = test.print(); - const arr = curHeap[0] === null ? curHeap.slice(1) : curHeap; - if (!isHeap(arr, 0, arr.length - 1)) { - return false; + + let max = Infinity; + const [result, vals] = [[], [9, 3, 5, 2, 15, 3, 7, 12, 7, 10, 90]]; + + vals.forEach((val) => test.insert(val)); + + for (let i = 0; i < vals.length; i++) { + const curHeap = test.print(); + const arr = curHeap[0] === null ? curHeap.slice(1) : curHeap; + + if (!isHeap(arr, 0, arr.length - 1)) { + return false; + } + + const removed = test.remove(); + if (!vals.includes(removed)) return false; + if (removed > max) return false + max = removed; + result.push(removed); } - const removed = test.remove(); - if (!vals.includes(removed)) return false; - if (removed > max) return false - max = removed; - result.push(removed); - } - for (let i = 0; i < vals.length; i++) { - if (!result.includes(vals[i])) { - return false; - } - } - return true + + for (let i = 0; i < vals.length; i++) { + if (!result.includes(vals[i])) { + return false; + } + } + + return true; })() ); ```