Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 0.45 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. HEAP-INSERT(A,key)
  2.         heapsize[A] <- heapsize[A] + 1
  3.         n <- heapsize[A]
  4.         if n = 1
  5.                 then A[n] = key
  6.         if n = 2 or n = 3
  7.                 then A[n] = key
  8.                          if A[n] > A[1]
  9.                                 then exchange A[n] <-> A[1]
  10.         if n > 3
  11.                 then A[n] = key
  12.                          i <- n
  13.                          while i > 3
  14.                                 do  if A[Parent(i)] >= A[i] >= A[Parent(Parent(i))]
  15.                                                 then done
  16.                                         if A[Parent(i)] <= A[i] <= A[Parent(Parent(i))]
  17.                                                 then done
  18.                                         exchange A[i] <-> A[Parent(Parent(i))]
  19.                                         i <- Parent(i)