CS301-Lec32 handout - CS301 Data Structures Lecture No. 32...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 32 ___________________________________________________________________ Data Structures Lecture No. 32 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 6 6.3 Summary perculateDown Method getMin Method buildHeap Method buildHeap in Linear Time Theorem In the previous lecture, we defined a heap class and discussed methods for its implementation. Besides, the insert and delete procedures were talked about. Here, we will continue with discussion on other methods of the heap class. C++ language code will be written for these procedures so that we could complete the heap class and use it to make heap objects. We are well aware of the deleteMin method. In this method, array[1] element ( a root element) is put in the minItem variable. In the min heap case, this root element is the smallest among the heap elements. Afterwards, the last element of the array is put at the first position of the array. The following two code lines perform this task. minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; perculateDown Method Then we call the perculateDown method. This is the same method earlier employed in the build heap process. We passed it the node, say i, form where it starts its function to restore the heap order. Let’s look at this perculateDown method. It takes the array index as an argument and starts its functionality from that index. In the code, we give the name hole to this array index. Following is the code of this method. // hole is the index at which the percolate begins. template <class eType> void Heap<eType>::percolateDown( int hole ) { int child; eType tmp = array[ hole ]; for( ; hole * 2 <= currentSize; hole = child ) { child = hole * 2;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 32 ___________________________________________________________________ child++; // right child is smaller if( array[ child ] < tmp ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; } In the code of this function, it declares an integer variable named child before putting the value at the index hole of the array in tmp variable. It is followed by a ‘for loop’, the core of this method. The termination condition of this for loop is hole * 2 <= currentSize; We know the 2i (or i * 2) formula that gives us index position of the left child of i. In the code given above, we are using hole instead of i. This variable i.e. hole is the loop variable. In other words, the termination condition means that the loop will continue as long as the left child (hole * 2) is less than or equal to the current size (currentSize) of the heap. Before looking at the third condition in which we change the value of loop variable, let’s have a view of the iteration of for loop. In the body of the ‘
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 9

CS301-Lec32 handout - CS301 Data Structures Lecture No. 32...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online