Removing the maximum element

To remove the maximum element from the priority queue, we will need to use the ExtractMax() operation. After we remove the element in root, we will place the minimum value in root to replace the maximum element which was just removed. Then, we need to shift the minimum value down to the leaf of the tree. To do so, we need to also create the ShiftDown() operation, as follows:

void BinaryHeap::ShiftDown(int i){    // For non-existing index    // just do nothing    if(i > heapSize)        return;    // Initialize swapId    int swapId = i;    // Compare with left child, if exists    if (l(i) <= heapSize && vect[i] < vect[l(i)])        swapId = l(i);    // Compare with right child, if exists    if (r(i) <= heapSize && vect[swapId] < vect[r(i)]) swapId = r(i); ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.