CS301-Lec31 handout

# CS301-Lec31 handout - CS301 Data Structures Lecture No. 31...

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

CS301 – Data Structures Lecture No. 31 ___________________________________________________________________ Data Structures Lecture No. 31 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 6 6.3.3, 6.3.4 Summary BuildHeap Other Heap methods C++ Code BuildHeap In the previous lecture, we discussed about the BuildHeap method of heap abstract data structure. In this handout, you are going to know why a programmer adopts this method. Suppose we have some data that can be numbers or characters or in some other form and want to build a min-Heap or max-Heap out of it. One way is to use the insert() method to build the heap by inserting elements one by one. In this method, the heap property will be maintained. However, the analysis shows that it is NlogN algorithm i.e. the time required for this will be proportional to NlogN . Secondly, if we have all the data ready, then it will be better to build a heap at once as it is a better option than NlogN . In the delete procedure, when we delete the root, a new element takes the place of root. In case of min-heap , the minimum value will come up and the larger elements will move downwards. In this regard, percolate procedures may be of great help. The percolateDown procedure will move the smaller value up and bigger value down. This way, we will create the heap at once, without calling the insert() method internally. Let’s revisit it again: The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a heap out of N keys. for ( i = N/2; i > 0; i-- ) percolateDown(i); Suppose, there are N data elements (also called as N Keys). We will put this data in an array and call it as a binary tree. As discussed earlier, a complete binary tree can be stored in an array. We have a tree in an array but it is not a heap yet. It is not necessary that the heap property is satisfied. In the next step, we apply the algorithm. Why did we start the i from N/2 ? Let’s apply this algorithm on the actual data. We will use the diagram to understand the BuildHeap . Consider the diagram given below:

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

View Full Document
CS301 – Data Structures Lecture No. 31 ___________________________________________________________________ In the above diagram, there is an array containing the elements as 65, 31, 32, 26, 21, 19, 68, 13, 24, 15, 14, 16, 5, 70, 12. We have all the data in this array. The zeroth element of the array is empty. We kept it empty intentionally to apply the 2i , 2i+1 scheme. We will take this array as a complete binary tree. Let’s take the first element that is 65, applying the 2i and 2i+1 formula. This element has 31 and 32 as its children. Take the second element i.e. 32 and use the formula 2i and 2i+1 . Its children are 26 and 21. We can take the remaining elements one by one to build the tree. The tree is shown in the above figure. This tree is not a min-heap as it has 65 as root element while there are smaller elements like 13, 15, 5 as its children. So, this being a
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 / 10

CS301-Lec31 handout - CS301 Data Structures Lecture No. 31...

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

View Full Document
Ask a homework question - tutors are online