CS301-Lec30 handout

# CS301-Lec30 handout - CS301 Data Structures Lecture No. 30...

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

CS301 – Data Structures Lecture No. 30 ___________________________________________________________________ Data Structures Lecture No. 30 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 6 6.3 Summary Inserting into a Min-Heap Deleting from a Min-Heap Building a Heap Inserting into a Min-Heap In the previous lecture, we discussed about the heap data structure besides touching upon the concept of storage of complete binary tree in an array. For the study of parent-child relationship, the ‘2i and 2i+1 scheme’ was used. Then we changed our focus to heap and found it best for the priority queues. In the previous lecture, we did not really discuss the uses of heap. Rather, most of the discussion remained focused on insertion method in the binary tree employed at the time of implementation with the help of an array. After inserting a new element in the array and by moving few elements, a programmer can have minimum or maximum heap. In case of minimum heap, the minimum value in the tree is always in the root of the tree. However, in case of maximum heap, the maximum value in the tree lies in the root node. When we insert a new element in the tree implemented with the help of an array, we insert element at the last position (of the array). Due to this insertion at the end, the heap order may be violated. Therefore, we start moving this element upwards. While moving upward, this element may reach at the root of the tree. It is important to note that only one branch of the tree is affected because of this movement in the upward direction. This process, being of localized nature, will not disturb the entire tree. To recap, see the figure Fig 30.1, where we are inserting an element 15. Page 1 of 13

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

View Full Document
CS301 – Data Structures Lecture No. 30 ___________________________________________________________________ 13 14 16 19 68 21 24 65 26 32 31 15 0 13 1 14 2 16 3 24 4 21 5 19 6 68 7 65 8 26 9 32 10 31 11 15 12 13 14 Insert (15) with exchange 1 2 3 4 5 6 7 8 9 10 11 12 Fig 30.1 The new element 15 is inserted at the last array position 12. Being a complete binary tree, the next new node will be the left child of node 19. As node 19 is at array position 6 (or level order traversal), its left child will be 6 * 2 = 12 th position node or the value at 12 th position in the array. Now, we see where we will have to carry out the exchange operation. As the parent of 15, the number 19 is greater than it, the first exchange will be among 19 and 15 as shown in the above figure. After exchange, the new figure is shown in Fig 30.2. 13 14 16 15 68 21 24 65 26 32 31 19 0 13 1 14 2 16 3 24 4 21 5 15 6 68 7 65 8 26 9 32 10 31 11 19 12 13 14 Insert (15) with exchange 1 2 3 4 5 6 7 8 9 10 11 12 Fig 30.2 Page 2 of 13
CS301 – Data Structures Lecture No. 30 ___________________________________________________________________ You can see that both the elements have exchanged positions i.e. 19 has come down and 15 gone up. But number 15 is still less than its parent 16, so we will have another

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

View Full Document
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 / 13

CS301-Lec30 handout - CS301 Data Structures Lecture No. 30...

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

View Full Document
Ask a homework question - tutors are online