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.

f<strong>in</strong>d another, we perform a restructure operation to restore its balance, <strong>and</strong><br />

cont<strong>in</strong>ue march<strong>in</strong>g up T look<strong>in</strong>g for more, all the way to the root. Still, s<strong>in</strong>ce the<br />

height of T is O(logn), where n is the number of entries, by Proposition 10.2,<br />

O(logn) tr<strong>in</strong>ode restructur<strong>in</strong>gs are sufficient to restore the height-balance property.<br />

Performance of AVL Trees<br />

We summarize the analysis of the performance of an AVL tree T as follows.<br />

Operations f<strong>in</strong>d, <strong>in</strong>sert, <strong>and</strong> remove visit the nodes along a root-to-leaf path<br />

of T, plus, possibly, their sibl<strong>in</strong>gs, <strong>and</strong> spend O(1) time per node. Thus, s<strong>in</strong>ce the<br />

height of T is O(logn) by Proposition 10.2, each of the above operations takes<br />

O(logn) time. We leave the implementation <strong>and</strong> analysis of an efficient version of<br />

the operation f<strong>in</strong>dAll as an <strong>in</strong>terest<strong>in</strong>g exercise. In Table 10.2, we summarize<br />

the performance of a dictionary implemented with an AVL tree. We illustrate this<br />

performance <strong>in</strong> Figure 10.11.<br />

Table 10.2: Performance of an n-entry dictionary<br />

realized by an AVL tree, where s denotes the size of<br />

the collection returned by f<strong>in</strong>dAll. The space usage<br />

is O(n).<br />

Operation<br />

Time<br />

O(1)<br />

size, isEmpty<br />

f<strong>in</strong>d, <strong>in</strong>sert, remove<br />

O(logn)<br />

f<strong>in</strong>dAll<br />

O(logn + s)<br />

Figure 10.11: Illustrat<strong>in</strong>g the runn<strong>in</strong>g time of searches<br />

<strong>and</strong> updates <strong>in</strong> an AVL tree. The time performance is<br />

O(1) per level, broken <strong>in</strong>to a down phase, which<br />

typically <strong>in</strong>volves search<strong>in</strong>g, <strong>and</strong> an up phase, which<br />

607

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

Saved successfully!

Ooh no, something went wrong!