{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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;
Image of page 1

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

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 32 ___________________________________________________________________ if( child != currentSize && array[child+1] < array[ child ] ) 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 ‘ for loop’, we assign the value hole * 2 to the child variable. The left child of an index i is located at the index 2i . So it is obvious that the child has the index number of the left child of hole . Then there is an if statement. In this statement, we check two conditions combined with && (logical AND) operator. This if statement returns TRUE only if both the conditions are TRUE. The first condition in this if statement checks that the child
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern