Class No.26  Data Structures http://ecomputernotes.com
Heap code in C++ <ul><li>template <class eType> </li></ul><ul><li>void Heap<eType>::deleteMin( eType & minItem ) </li></ul...
Heap code in C++ <ul><li>//  hole is the index at which the percolate begins. </li></ul><ul><li>template <class eType> </l...
Heap code in C++ <ul><li>template <class eType> </li></ul><ul><li>const eType& Heap<eType>::getMin( ) </li></ul><ul><li>{ ...
Heap code in C++ <ul><li>template <class eType> </li></ul><ul><li>bool Heap<eType>::isEmpty( ) </li></ul><ul><li>{ </li></...
BuildHeap in Linear Time <ul><li>How is buildHeap a linear time algorithm? I.e., better than Nlog 2 N? </li></ul><ul><li>W...
BuildHeap in Linear Time <ul><li>It is easy to see that this tree consists of (2 0 ) node at height  h , 2 1  nodes at hei...
Complete Binary Tree h  : 2 0  nodes H D I E J K L F M G N O h  -1: 2 1  nodes h  -2: 2 2  nodes h  -3: 2 3  nodes http://...
BuildHeap in Linear Time <ul><li>The sum of the heights of all the nodes is then </li></ul><ul><li>S =    2 i ( h – i ), ...
BuildHeap in Linear Time <ul><li>Since a complete binary tree has between   2 h  and   2 h+1   nodes </li></ul><ul><li>S =...
BuildHeap in Linear Time <ul><li>Another way to prove the theorem. </li></ul><ul><li>The  height  of a node in the tree = ...
Height 1 Nodes Marking the left edges for height 1 nodes http://ecomputernotes.com
Height 2 Nodes Marking the first left edge and the subsequent right edge for height 2 nodes http://ecomputernotes.com
Height 3 Nodes Marking the first left edge and the subsequent two right edges for height 3 nodes http://ecomputernotes.com
Height 4 Nodes Marking the first left edge and the subsequent three right edges for height 4 nodes http://ecomputernotes.com
Theorem N=31, treeEdges=30, H=4, dottedEdges=4 (H). Darkened Edges = 26 = N-H-1 (31-4-1) http://ecomputernotes.com
Upcoming SlideShare
Loading in …5
×

Computer notes - Heap code in C++

1,074 views

Published on

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,074
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • End of lecture 31, start of 32
  • End of lecture 32
  • Computer notes - Heap code in C++

    1. 1. Class No.26 Data Structures http://ecomputernotes.com
    2. 2. 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
    3. 3. Heap code in C++ <ul><li>// hole is the index at which the percolate begins. </li></ul><ul><li>template <class eType> </li></ul><ul><li>void Heap<eType>::percolateDown( int hole ) </li></ul><ul><li>{ </li></ul><ul><li>int child; </li></ul><ul><li>eType tmp = array[ hole ]; </li></ul><ul><li>for( ; hole * 2 <= currentSize; hole = child ) </li></ul><ul><li>{ </li></ul><ul><li>child = hole * 2; </li></ul><ul><li>if( child != currentSize && </li></ul><ul><li>array[child+1] < array[ child ] ) </li></ul><ul><li>child++; // right child is smaller </li></ul><ul><li>if( array[ child ] < tmp ) </li></ul><ul><li>array[ hole ] = array[ child ]; </li></ul><ul><li>else break; </li></ul><ul><li>} </li></ul><ul><li>array[ hole ] = tmp; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    4. 4. Heap code in C++ <ul><li>template <class eType> </li></ul><ul><li>const eType& Heap<eType>::getMin( ) </li></ul><ul><li>{ </li></ul><ul><li>if( !isEmpty( ) ) </li></ul><ul><li>return array[ 1 ]; </li></ul><ul><li>} </li></ul><ul><li>template <class eType> </li></ul><ul><li>void Heap<eType>::buildHeap(eType* anArray, int n ) </li></ul><ul><li>{ </li></ul><ul><li>for(int i = 1; i <= n; i++) </li></ul><ul><li>array[i] = anArray[i-1]; </li></ul><ul><li>currentSize = n; </li></ul><ul><li>for( int i = currentSize / 2; i > 0; i-- ) </li></ul><ul><li>percolateDown( i ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    5. 5. Heap code in C++ <ul><li>template <class eType> </li></ul><ul><li>bool Heap<eType>::isEmpty( ) </li></ul><ul><li>{ </li></ul><ul><li>return currentSize == 0; </li></ul><ul><li>} </li></ul><ul><li>template <class eType> </li></ul><ul><li>bool Heap<eType>::isFull( ) </li></ul><ul><li>{ </li></ul><ul><li>return currentSize == capacity; </li></ul><ul><li>} </li></ul><ul><li>template <class eType> </li></ul><ul><li>int Heap<eType>::getSize( ) </li></ul><ul><li>{ </li></ul><ul><li>return currentSize; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    6. 6. BuildHeap in Linear Time <ul><li>How is buildHeap a linear time algorithm? I.e., better than Nlog 2 N? </li></ul><ul><li>We need to show that the sum of heights is a linear function of N (number of nodes). </li></ul><ul><li>Theorem : For a perfect binary tree of height h containing 2 h + 1 – 1 nodes, the sum of the heights of nodes is 2 h + 1 – 1 – ( h +1 ), or N - h-1. </li></ul>http://ecomputernotes.com
    7. 7. BuildHeap in Linear Time <ul><li>It is easy to see that this tree consists of (2 0 ) node at height h , 2 1 nodes at height h – 1, 2 2 at h- 2 and, in general, 2 i nodes at h – i. </li></ul>http://ecomputernotes.com
    8. 8. Complete Binary Tree h : 2 0 nodes H D I E J K L F M G N O h -1: 2 1 nodes h -2: 2 2 nodes h -3: 2 3 nodes http://ecomputernotes.com A B C
    9. 9. BuildHeap in Linear Time <ul><li>The sum of the heights of all the nodes is then </li></ul><ul><li>S =  2 i ( h – i ), for i = 0 to h-1 </li></ul><ul><li>= h + 2 (h- 1 ) + 4 (h- 2 ) + 8 (h- 3 )+ ….. + 2 h-1 (1) </li></ul><ul><li>Multiplying by 2 gives the equation </li></ul><ul><li>2S = 2 h + 4 (h- 1 ) + 8 (h- 2 ) + 16 (h- 3 )+ ….. + 2 h (2) </li></ul><ul><li>Subtract the two equations to get </li></ul><ul><li>S = - h + 2 + 4 + 8 + 16 + ….. + 2 h-1 + 2 h </li></ul><ul><li>= (2 h+1 – 1) - (h+ 1 ) </li></ul><ul><li>Which proves the theorem. </li></ul>http://ecomputernotes.com
    10. 10. BuildHeap in Linear Time <ul><li>Since a complete binary tree has between 2 h and 2 h+1 nodes </li></ul><ul><li>S = (2 h+1 – 1) - (h+ 1 ) </li></ul><ul><li> N - log 2 ( N +1) </li></ul><ul><li>Clearly, as N gets larger, the log 2 ( N +1) term becomes insignificant and S becomes a function of N . </li></ul>http://ecomputernotes.com
    11. 11. BuildHeap in Linear Time <ul><li>Another way to prove the theorem. </li></ul><ul><li>The height of a node in the tree = the number of edges on the longest downward path to a leaf </li></ul><ul><li>The height of a tree = the height of its root </li></ul><ul><li>For any node in the tree that has some height h , darken h tree edges </li></ul><ul><ul><li>Go down tree by traversing left edge then only right edges </li></ul></ul><ul><li>There are N – 1 tree edges, and h edges on right path, so number of darkened edges is N – 1 – h , which proves the theorem. </li></ul>http://ecomputernotes.com
    12. 12. Height 1 Nodes Marking the left edges for height 1 nodes http://ecomputernotes.com
    13. 13. Height 2 Nodes Marking the first left edge and the subsequent right edge for height 2 nodes http://ecomputernotes.com
    14. 14. Height 3 Nodes Marking the first left edge and the subsequent two right edges for height 3 nodes http://ecomputernotes.com
    15. 15. Height 4 Nodes Marking the first left edge and the subsequent three right edges for height 4 nodes http://ecomputernotes.com
    16. 16. Theorem N=31, treeEdges=30, H=4, dottedEdges=4 (H). Darkened Edges = 26 = N-H-1 (31-4-1) http://ecomputernotes.com

    ×