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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Let n be the number of nodes of T, <strong>and</strong> let pM be the maximum value of p(v) over<br />

all the nodes of T. The array list S has size N = pM + 1 s<strong>in</strong>ce the element of S at<br />

<strong>in</strong>dex 0 is not associated with any node of T. Also, S will have, <strong>in</strong> general, a number<br />

of empty elements that do not refer to exist<strong>in</strong>g nodes of T. In fact, <strong>in</strong> the worst case,<br />

N = 2 n , the justification of which is left as an exercise (R-7.23). In Section 8.3, we<br />

will see a class of b<strong>in</strong>ary trees, called "heaps" for which N = n + 1. Thus, <strong>in</strong> spite of<br />

the worst-case space usage, there are applications for which the array-list<br />

representation of a b<strong>in</strong>ary tree is space efficient. Still, for general b<strong>in</strong>ary trees, the<br />

exponential worst-case space requirement of this representation is prohibitive.<br />

Table 7.3 summarizes runn<strong>in</strong>g times of the methods of a b<strong>in</strong>ary tree implemented<br />

with an array list. We do not <strong>in</strong>clude any tree update methods here.<br />

Table 7.3: Runn<strong>in</strong>g times for a b<strong>in</strong>ary tree T<br />

implemented with an array list S. We denote the<br />

number of nodes of T with n, <strong>and</strong> N denotes the size of<br />

S. The space usage is O(N), which is O(2 n ) <strong>in</strong> the worst<br />

case.<br />

Operation<br />

416

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

Saved successfully!

Ooh no, something went wrong!