fix: test remove element from max heap failing as expected (#47959)
Co-authored-by: kravmaguy <flex4lease@gmail.com>pull/48102/head
parent
bc4edf518e
commit
52b5bba501
|
@ -87,6 +87,21 @@ assert(
|
|||
The `remove` method should remove the greatest element from the max heap while maintaining the max heap property.
|
||||
|
||||
```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;
|
||||
}
|
||||
|
||||
assert(
|
||||
(function () {
|
||||
var test = false;
|
||||
|
@ -95,16 +110,21 @@ assert(
|
|||
} else {
|
||||
return false;
|
||||
}
|
||||
test.insert(30);
|
||||
test.insert(300);
|
||||
test.insert(500);
|
||||
test.insert(10);
|
||||
let result = [];
|
||||
result.push(test.remove());
|
||||
result.push(test.remove());
|
||||
result.push(test.remove());
|
||||
result.push(test.remove());
|
||||
return result.join('') == '5003003010';
|
||||
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;
|
||||
}
|
||||
const removed = test.remove();
|
||||
if(removed > max) return false
|
||||
max = removed;
|
||||
result.push(removed);
|
||||
}
|
||||
return true
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue