Figure 2116 Implementation of the linear-time buildHeap method

ANSI/AIM Code 39 Drawer In JavaUsing Barcode printer for Java Control to generate, create Code 3/9 image in Java applications.

Figure 2117 (a) Initial heap; (b) after percola teDown ( 7 )

Making Barcode In JavaUsing Barcode creator for Java Control to generate, create barcode image in Java applications.

percolateDownThis process leads to an incredibly simple algorithm for buildHeap,which is shown in Figure 2116 Note that percolateDown

Barcode Reader In JavaUsing Barcode scanner for Java Control to read, scan read, scan image in Java applications.

need not be performed on a leaf Thus we start at the highest numbered nonleaf node The tree in Figure 2117(a) is the unordered tree The seven remaining trees in Figures 2 117(b) through 2 120 show the result of each of the seven percolateDown operations Each dashed line corresponds to two comparisons: one to find the smaller child and one to compare the smaller child with the node Notice that the ten dashed lines in the algorithm correspond to 20 comparisons (There could have been an eleventh line)

ANSI/AIM Code 39 Creator In Visual C#.NETUsing Barcode creation for VS .NET Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.

-- ---

Code 39 Maker In VS .NETUsing Barcode creation for ASP.NET Control to generate, create ANSI/AIM Code 39 image in ASP.NET applications.

- - --

Code-39 Creation In .NETUsing Barcode creator for Visual Studio .NET Control to generate, create Code 39 Full ASCII image in Visual Studio .NET applications.

A Pr~ority Queue: The Binary Heap

Creating Code39 In Visual Basic .NETUsing Barcode creator for .NET Control to generate, create Code 3 of 9 image in VS .NET applications.

Figure 2118 (a) After percolateDown ( 6 ) ; (b) after percolateDown ( 5 )

Paint Code39 In JavaUsing Barcode creator for Java Control to generate, create Code 39 Full ASCII image in Java applications.

Figure 2119 (a) After percolateDown ( 4 ) ; (b) after percolateDown ( 3 )

Drawing USS Code 128 In JavaUsing Barcode creation for Java Control to generate, create Code128 image in Java applications.

Figure 2120 (a) After percolateDown ( 2 ) ; (b) after percolateDown ( 1) and buildHeap terminates

Paint Bar Code In JavaUsing Barcode creation for Java Control to generate, create barcode image in Java applications.

The buildHeap Operation: Linear-T

Barcode Encoder In JavaUsing Barcode generation for Java Control to generate, create barcode image in Java applications.

To bound the running time of buildHeap,we must bound the number of dashed lines We can h o so by computing the sum of the heights of all the nodes in the heap, which is the maximum number of dashed lines We expect a small number because half the nodes are leaves and have height 0 and a quarter of the nodes have height 1 Thus only a quarter of the nodes (those not already counted in the first two cases) can contribute more than I unit of height In particular, only one node contributes the maximum height of h o g N ] To obtain a linear time bound for b u i ldHeap,we need to establish that the sum of the heights of the nodes of a complete binary tree is O(N) We do so in Theorem 2 11, proving the bound for perfect trees by using a marking argument

Draw GTIN - 13 In JavaUsing Barcode printer for Java Control to generate, create EAN13 image in Java applications.

The linear time bound Can be by computing the sum of the heights of all the nodes in the heap

Create Code11 In JavaUsing Barcode printer for Java Control to generate, create USD - 8 image in Java applications.

We prove the bound perfect trees by using a marking ar,ument~

Generating Code 128 Code Set B In Visual C#.NETUsing Barcode printer for .NET Control to generate, create Code-128 image in Visual Studio .NET applications.

For the perject binaq tree of height H containing N = 2" the sum of the heights of the nodes is N - H - 1

Barcode Scanner In JavaUsing Barcode reader for Java Control to read, scan read, scan image in Java applications.

1 nodes,

Painting Bar Code In VS .NETUsing Barcode generation for .NET Control to generate, create barcode image in .NET framework applications.

Theorem 211

GTIN - 13 Reader In VS .NETUsing Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications.

We use a tree marking argument ( A more direct brute force calculation could also be done, as in Exercise 2 110) For any node in the tree that has some height h, we darken h tree edges as follows We go down the tree by traversing the left edge and then only right edges Each edge traversed is darkened An example is a peilfect tree of height 4 Nodes that have height I have their leji edge darkened, as shown in Figure 2121 Next, nodes of height 2 have a left edge and then a right edge darkened on the path from the node to the bottom, as shown in Figure 2122 In Figure 2123, three edges are darkened for each node of height 3: the first left edge leading out of the node and then the two right edges on the path to the bottom Finally, in Figure 2 124 four edges are darkened: the leji edge leading out of the root and the three right edges on the path to the bottom Note that no edge is ever darkened twice and that every edge except those on the right path is darkened As there are (N - I ) tree edges ( e v e q node has an edge coming into it except the root) and H edges on the right path, the number of darkened edges is N - I - H This proves the theorem

Draw Barcode In .NET FrameworkUsing Barcode creation for ASP.NET Control to generate, create bar code image in ASP.NET applications.

Proof

Generating Barcode In Visual Basic .NETUsing Barcode creator for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.

y Queue: The Binary Heap

Scan Data Matrix In Visual Studio .NETUsing Barcode decoder for VS .NET Control to read, scan read, scan image in .NET applications.

Figure 2121 Marking the left edges for height 1 nodes

Figure 2122 Marking the first left edge and the subsequent right edge for height 2 nodes

Figure 2123 Marking the first left edge and the subsequent two right edges for height 3 nodes

STL priority-queue l r n p l e r n e n t a t ~

Marking the first left edge and the subsequent two right edges for the height 4 node

A complete binary tree is not a perfect binary tree, but the result we have obtained is an upper bound on the sum of the heights of the nodes in a complete binary tree A complete binary tree has between 2 H and 2H - 1 nodes, so this theorem implies that the sum is O(N) A more careful argument establishes that the sum of the heights is N - v ( N ) , where Y ( N )is the number of Is in the binary representation of N A proof of this is left for you to do as Exercise 2 1 12