Computer Notes - Data Structures - 26
-
Upload
ecomputernotes -
Category
Documents
-
view
219 -
download
0
Transcript of Computer Notes - Data Structures - 26
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 1/16
Class No.26
Data Structures
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 2/16
Heap code in C++
template <class eType>
void Heap<eType>::deleteMin( eType & minItem )
{
if( isEmpty( ) ) {
cout << "heap is empty.“ << endl
;return;
}
minItem = array[ 1 ];
array[ 1 ] = array[ currentSize-- ];
percolateDown( 1 );
}
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 3/16
Heap code in C++
// hole is the index at which the percolate begins.
template <class eType> void Heap<eType>::percolateDown( int hole )
{
int child;
eType tmp = array[ hole ];
for( ; hole * 2 <= currentSize; hole = child )
{child = hole * 2;
if( child != currentSize &&
array[child+1] < array[ child ] )
child++; // right child is smaller
if( array[ child ] < tmp )
array[ hole ] = array[ child ];
else break;
}
array[ hole ] = tmp;
}
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 4/16
Heap code in C++
template <class eType>
const eType& Heap<eType>::getMin( )
{
if( !isEmpty( ) )
return array[ 1 ];
}
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
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 5/16
Heap code in C++
template <class eType>
bool Heap<eType>::isEmpty( )
{
return currentSize == 0;
}
template <class eType>
bool Heap<eType>::isFull( ){
return currentSize == capacity;
}
template <class eType>
int Heap<eType>::getSize( ){
return currentSize;
}
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 6/16
BuildHeap in Linear Time
How is buildHeap a linear time algorithm? I.e.,better than Nlog2N?
We need to show that the sum of heights is alinear function of N (number of nodes).
Theorem :For a perfect binary tree of height h containing
2h +1
–
1 nodes, the sum of the heights of nodesis 2h +1 – 1 – (h +1), or N -h-1.
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 7/16
BuildHeap in Linear Time
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
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 8/16
Complete Binary Tree
A
B
h : 20 nodes
H
D
I
E
J K
C
L
F
M
G
N O
h -1: 21
nodes
h -2: 22 nodes
h -3: 23 nodes
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 9/16
BuildHeap in Linear Time
The sum of the heights of all the nodes is then
S = 2i (h – i ), for i = 0 to h-1
= h + 2 (h- 1) + 4 (h- 2 ) + 8 (h- 3 )+ ….. + 2 h-1 (1)
Multiplying by 2 gives the equation
2S = 2h + 4 (h- 1) + 8 (h- 2 ) + 16 (h- 3 )+ ….. + 2 h (2)
Subtract the two equations to get
S = -h + 2 + 4 + 8 + 16 + ….. + 2 h-1 + 2 h
= (2 h+1 – 1) - (h+ 1)
Which proves the theorem.
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 10/16
BuildHeap in Linear Time
Since a complete binary tree has between 2 h
and 2 h+1 nodes
S = (2 h+1
– 1) - (h+ 1) N - log 2 ( N +1)
Clearly, as N gets larger, the log2(N +1) term
becomes insignificant and S becomes afunction of N .
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 11/16
BuildHeap in Linear Time
Another way to prove the theorem.
The height of a node in the tree = the number ofedges on the longest downward path to a leaf
The height of a tree = the height of its root
For any node in the tree that has some height h ,darken h tree edges
– Go down tree by traversing left edge then only
right edges 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.
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 12/16
Height 1 Nodes
Marking the left edges for height 1 nodes http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 13/16
Height 2 Nodes
Marking the first left edge and the subsequent right edge for height 2
nodes
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 14/16
Height 3 Nodes
Marking the first left edge and the subsequent two right edges for
height 3 nodes
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 15/16
Height 4 Nodes
Marking the first left edge and the subsequent three right edges for
height 4 nodes
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 26
http://slidepdf.com/reader/full/computer-notes-data-structures-26 16/16
Theorem
N=31, treeEdges=30, H=4, dottedEdges=4 (H).
Darkened Edges = 26 = N-H-1 (31-4-1)
http://ecomputernotes.com