|
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:
 | increase the heap size |
 | position i = heapsize is the place you would insert the new element |
 | before inserting, check if parent of this element is >= |
 | if not, move parent down and keep on searching for appropriate place to insert the new
element. |
|