The algorithm for the MaxHeap class will be exactly the same as the MinHeap class. The difference is that whenever we perform a > (more than) comparison, we will change it to use the < (less than) comparison.
The code for the MaxHeap class is presented as follows:
export class MaxHeap extends MinHeap { constructor(compareFn = defaultCompare) { super(compareFn); this.compareFn = reverseCompare(compareFn); // {1} } }
But instead of copying the code and duplicating it, we can extend the MinHeap class to inherit all the code we created in this chapter and do a reverse comparison whenever needed. To reverse the comparison, instead of comparing a with b, we can compare b with a ({1}) instead, as demonstrated in the following ...