26 Data Structures
http://ecomputernotes.com
http://ecomputernotes.com
http://ecomputernotes.com
template <class eType> void Heap<eType>::buildHeap(eType* anArray, int n ) { for(int i = 1; i <= n; i++) array[i] = anArray[i-1]; currentSize = n; for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); }
http://ecomputernotes.com
http://ecomputernotes.com
Theorem: For a perfect binary tree of height h containing 2h +1 1 nodes, the sum of the heights of nodes is 2h +1 1 (h +1), or N-h-1.
http://ecomputernotes.com
It is easy to see that this tree consists of (20) node at height h, 21 nodes at height h 1, 22 at h-2 and, in general, 2i nodes at h i.
http://ecomputernotes.com
A
B C
h -2: 22 nodes
h -3: 23 nodes
http://ecomputernotes.com
http://ecomputernotes.com
http://ecomputernotes.com
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
http://ecomputernotes.com