42D. Inserting into a Heap

There are two steps to inserting something into a min-heap.

  1. Add a new node at the leftmost unoccupied position in the bottom level and put the new priority and item in that node. (If the bottom level is full, add the new node at the leftmost position in a new bottom level.)

  2. After inserting the new node, the ordering requirement might not be met. Perform a reheapUp operation to move the inserted item up the tree as high as it needs to go. (If the new value is smaller than its parent's value, swap with the parent and continue to move it upwards. It cannot move above the root.)

For example, here is the process of inserting priority 4 into a min-heap. The reheapUp operations includes all of the swap steps.

A reheapUp operations does not always move a value all the way to the root. For example, here is a similar insertion that inserts 6 instead of 4.


Implementation of insert (optional)

Implementation of reheapUp and insert are fairly simple.

  //===========================================
  //                 swap
  //===========================================
  // swap(x,y) swaps variables x and y.
  //===========================================

  void swap(ItemAndPriority& x, ItemAndPriority& y)
  {
    ItemAndPriority t = x;
    x = y;
    y = t;
  }

  //===========================================
  //              reheapUp
  //===========================================
  // reheapUp(i,A) does a reheapUp operation on
  // the heap represented by array A,
  // starting at index i.
  //===========================================

  void reheapUp(const int i, ItemAndPriority A[])
  {
    int j = i;
    while(j > 0)
    {
      int p = parent(j);
      if(A[p].priority > A[j].priority)
      {
        swap(A[p], A[j]);
        j = p;
      }
      else
      {
        break;
      }
    }
  }

  //===========================================
  //            enlarge
  //===========================================
  // Move q into a larger array.
  //===========================================

  void enlarge(PriorityQueue& q)
  {
    int              n    = q.physicalSize;
    ItemAndPriority* newA = new ItemAndPriority[2*n];

    for(int i = 0; i < q.load; i++)
    {
      newA[i] = q.A[i];
    }
    delete[] q.A;
    q.A            = newA;
    q.physicalSize = 2*n;
  }

  //===========================================
  //            insert
  //===========================================
  // Insert item x with priority p into
  // priority queue q.
  //===========================================

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

    if(n == q.physicalSize)
    {
      enlarge(q);
    }

    q.A[n].priority = p;
    q.A[n].item     = x;
    q.load          = n+1;
    reheapUp(n, q.A);
  }

Exercises

  1. What min-heap do you get if you insert 9 into the following min-heap?

    Answer

  2. What min-heap do you get if you insert 5 into the following min-heap?

    Answer