23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Array List Representation of a Complete B<strong>in</strong>ary Tree<br />

The array-list b<strong>in</strong>ary tree representation (Section 7.3.5) is especially suitable for a<br />

complete b<strong>in</strong>ary tree T. We recall that <strong>in</strong> this implementation, the nodes of T are<br />

stored <strong>in</strong> an array list A such that node v <strong>in</strong> T is the element of A with <strong>in</strong>dex equal<br />

to the level number p(v) of v, def<strong>in</strong>ed as follows:<br />

• If v is the root of T, then p(v) = 1.<br />

• If v is the left child of node u, then p(v) = 2p(u).<br />

• If v is the right child of node u, then p(v) = 2p(u) + 1.<br />

With this implementation, the nodes of T have contiguous <strong>in</strong>dices <strong>in</strong> the range<br />

[1,n] <strong>and</strong> the last node of T is always at <strong>in</strong>dex n, where n is the number of nodes<br />

of T. Figure 8.5 shows two examples illustrat<strong>in</strong>g this property of the last node.<br />

Figure 8.5: Two examples show<strong>in</strong>g that the last<br />

node w of a heap with n nodes has level number n: (a)<br />

heap T 1 with more than one node on the bottom level;<br />

(b) heap T 2 with one node on the bottom level; (c)<br />

array-list representation of T 1 ; (d) array-list<br />

representation of T 2 .<br />

473

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!