42E. Removal from a Heap

Recall that we can only remove the value with the smallest priority from a priority queue. Since the smallest priority is in the root of a min-heap, it is the value in the root that we want to remove. But it is not possible to remove the root node. Instead, find the rightmost node on the bottom level. Remove that node, and move its contents into the root, replacing the former contents of the root.

But now the min-heap will not obey the ordering requirement. Push the value in the root downwards by swapping it with whichever of its children has a smaller priority. Keep doing this until this value either reaches a leaf or it reaches a node where the values beneath it are no smaller than it. Here is an example of a removal.

The swaps are called a reheapDown operation.


Implementation of remove (optional)

Implementation of reheapDown is a little tricky because there are three kinds of nodes: those with two children, those with just a left child and those with no chilren.

  //===================================================
  //                  reheapDown
  //===================================================
  // reheapDown(i, A, n) does a reheapDown operation on
  // the heap represented by array A, starting at
  // index i.  Parameter n is the number of values
  // currently in the heap.
  //===================================================

  void reheapDown(const int i, ItemAndPriority A[], const int n)
  {
    int p = A[i].priority;
    int j = i;

    while(leftchild(j) < n)  // j is not a leaf
    {
      int lc    = leftchild(j);
      int rc    = rightchild(j);
      int pleft = A[lc].priority;

      if(rc < n)  // j has two children
      {
        int pright = A[rc].priority;
        int m      = min(p, min(pleft, pright));

        if(m == p)
        {
          break;
        }
        else if(m == pleft)
        {
          swap(A[j], A[lc]);
          j = lc;
        }
        else
        {
          swap(A[j], A[rc]);
          j = rc;
        }
      }
      else  // j has only a left child.
      {
        if(p > pleft)
        {
          swap(A[j], A[lc]);
        }
        break;
      }
    }
  }

  //===================================================
  //                  remove
  //===================================================
  // remove(x, p, q) removes the pair with the smallest
  // priority from priority queue q.  It stores the
  // item and priority removed into out-parameters
  // x and p.
  //
  // If q is empty, remove sets x and p each to -1.
  //===================================================

  void remove(int& x, int& p, PriorityQueue& q)
  {
    int n = q.load;

    if(n == 0) 
    {
      x = p = -1;
    }
    else
    {
       x      = q.A[0].item;
       p      = q.A[0].priority;
       q.A[0] = q.A[n-1];
       q.load = n-1;
       reheapDown(0, q.A, q.load);
    }
  }

Exercises

  1. What min-heap do you get if you remove the smallest value from the following min-heap?

    Answer

  2. What heap do you get if you remove the smallest value from the following min-heap?

    Answer