Computer notes - Build Heap

1,193 views

Published on

The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,193
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
36
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Star of lecture 31
  • End of lecture 30
  • End of lecture 31, start of 32
  • Computer notes - Build Heap

    1. 1. Class No.25 Data Structures http://ecomputernotes.com
    2. 2. BuildHeap <ul><li>The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. </li></ul><ul><li>The following algorithm will build a heap out of N keys. </li></ul><ul><ul><li>for( i = N/2; i > 0; i-- ) </li></ul></ul><ul><ul><li>percolateDown(i); </li></ul></ul>http://ecomputernotes.com
    3. 3. BuildHeap <ul><li>i = 15/2 = 7 </li></ul>65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 11 32 12 15 4 13 14 15 5 70 12 i  i Why I=n/2? http://ecomputernotes.com 65 19 21 14 24 26 13 15 68 16 31 32 5 70 12
    4. 4. BuildHeap <ul><li>i = 15/2 = 7 </li></ul>65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 11 32 12 15 4 13 14 15 5 70 68 i  i http://ecomputernotes.com 65 19 21 14 24 26 13 15 12 16 31 32 5 70 68
    5. 5. BuildHeap <ul><li>i = 6 </li></ul>65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 11 32 12 15 4 13 14 15 5 70 68 i  i http://ecomputernotes.com 65 19 21 14 24 26 13 15 12 16 31 32 5 70 68
    6. 6. BuildHeap <ul><li>i = 5 </li></ul>65 21 5 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 11 32 12 15 4 13 14 15 19 70 68 i  i http://ecomputernotes.com 65 5 21 14 24 26 13 15 12 16 31 32 19 70 68
    7. 7. BuildHeap <ul><li>i = 4 </li></ul>65 14 5 26 21 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 11 32 12 15 4 13 14 15 19 70 68 i  i http://ecomputernotes.com 65 5 14 21 24 26 13 15 12 16 31 32 19 70 68
    8. 8. BuildHeap <ul><li>i = 3 </li></ul>65 14 5 13 21 16 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 11 32 12 15 4 13 14 15 19 70 68 i  i http://ecomputernotes.com 65 5 14 21 24 13 26 15 12 16 31 32 19 70 68
    9. 9. BuildHeap <ul><li>i = 2 </li></ul>65 14 16 13 21 32 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 11 5 12 15 4 13 14 15 19 70 68 i  i http://ecomputernotes.com 65 16 14 21 24 13 26 15 12 32 31 5 19 70 68
    10. 10. BuildHeap <ul><li>i = 1 </li></ul>65 14 16 24 21 32 12 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 11 5 12 15 4 13 14 15 19 70 68 i  i http://ecomputernotes.com 65 16 14 21 31 24 26 15 12 32 13 5 19 70 68
    11. 11. BuildHeap <ul><li>Min heap </li></ul>5 14 16 24 21 32 65 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 11 12 12 15 4 13 14 15 19 70 68 http://ecomputernotes.com 5 16 14 21 31 24 26 15 65 32 13 12 19 70 68
    12. 12. Other Heap Operations <ul><li>decreaseKey(p, delta) : </li></ul><ul><ul><li>lowers the value of the key at position ‘p’ by the amount ‘delta’. Since this might violate the heap order, the heap must be reorganized with percolate up (in min heap) or down (in max heap). </li></ul></ul><ul><li>increaseKey(p, delta) : </li></ul><ul><ul><li>opposite of decreaseKey. </li></ul></ul><ul><li>remove(p) : </li></ul><ul><ul><li>removes the node at position p from the heap. This is done by first decreaseKey(p,  ) and then performing deleteMin(). </li></ul></ul>http://ecomputernotes.com
    13. 13. Heap code in C++ <ul><li>template <class eType> </li></ul><ul><li>class Heap </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>Heap( int capacity = 100 ); </li></ul><ul><li>void insert( const eType & x ); </li></ul><ul><li>void deleteMin( eType & minItem ); </li></ul><ul><li>const eType & getMin( ); </li></ul><ul><li>bool isEmpty( ); </li></ul><ul><li>bool isFull( ); </li></ul><ul><li>int Heap<eType>::getSize( ); </li></ul>http://ecomputernotes.com
    14. 14. Heap code in C++ <ul><li>private: </li></ul><ul><li>int currentSize; // Number of elements in heap </li></ul><ul><li>eType* array; // The heap array </li></ul><ul><li>int capacity; </li></ul><ul><li>void percolateDown( int hole ); </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    15. 15. Heap code in C++ <ul><li>#include &quot;Heap.h“ </li></ul><ul><li>template <class eType> </li></ul><ul><li>Heap<eType>::Heap( int capacity ) </li></ul><ul><li>{ </li></ul><ul><li>array = new etype[capacity + 1]; </li></ul><ul><li>currentSize=0; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    16. 16. Heap code in C++ <ul><li>// Insert item x into the heap, maintaining heap </li></ul><ul><li>// order. Duplicates are allowed. </li></ul><ul><li>template <class eType> </li></ul><ul><li>bool Heap<eType>::insert( const eType & x ) </li></ul><ul><li>{ </li></ul><ul><li>if( isFull( ) ) { </li></ul><ul><li>cout << &quot;insert - Heap is full.&quot; << endl; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>// Percolate up </li></ul><ul><li>int hole = ++currentSize; </li></ul><ul><li>for(; hole > 1 && x < array[hole/2 ]; hole /= 2) </li></ul><ul><li>array[ hole ] = array[ hole / 2 ]; </li></ul><ul><li>array[hole] = x; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    17. 17. Heap code in C++ <ul><li>template <class eType> </li></ul><ul><li>void Heap<eType>::deleteMin( eType & minItem ) </li></ul><ul><li>{ </li></ul><ul><li>if( isEmpty( ) ) { </li></ul><ul><li>cout << &quot; heap is empty. “ << endl ; </li></ul><ul><li> return; </li></ul><ul><li>} </li></ul><ul><li>minItem = array[ 1 ]; </li></ul><ul><li>array[ 1 ] = array[ currentSize-- ]; </li></ul><ul><li>percolateDown( 1 ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com

    ×