University of California, Santa Cruz

Prof. Paulo Franca - Spring 2000

Introduction

Syllabus

Guidelines for labs

Lab sections

Take quiz & check grades

Insert an element into a heap

Book procedure:

insert(A,key)
A.heapsize=A.heapsize+1
i=A.heapsize;
while (i>1 and A[Parent(i)]<key)
      A[i] =A[Parent(i)]:
       i = Parent(i);
A[i] = key;

Explanation:

bulletincrease the heap size
bulletposition i = heapsize is the place you would insert the new element
bulletbefore inserting, check if parent of this element is >=
bulletif not, move parent down and keep on searching for appropriate place to insert the new element.