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.

10.2 AVL Trees<br />

In the previous section, we discussed what should be an efficient dictionary data<br />

structure, but the worst-case performance it achieves for the various operations is<br />

l<strong>in</strong>ear time, which is no better than the performance of list- <strong>and</strong> array-based dictionary<br />

implementations (such as unordered lists <strong>and</strong> search tables discussed <strong>in</strong> Chapter 9). In<br />

this section, we describe a simple way of correct<strong>in</strong>g this problem so as to achieve<br />

logarithmic time for all the fundamental dictionary operations.<br />

Def<strong>in</strong>ition of an AVL Tree<br />

The simple correction is to add a rule to the b<strong>in</strong>ary search tree def<strong>in</strong>ition that will<br />

ma<strong>in</strong>ta<strong>in</strong> a logarithmic height for the tree. The rule we consider <strong>in</strong> this section is the<br />

follow<strong>in</strong>g height-balance property, which characterizes the structure of a b<strong>in</strong>ary<br />

search tree T <strong>in</strong> terms of the heights of its <strong>in</strong>ternal nodes (recall from Section 7.2.1<br />

that the height of a node v <strong>in</strong> a tree is the length of a longest path from v to an<br />

external node):<br />

Height-Balance Property: For every <strong>in</strong>ternal node v of T, the heights of the<br />

children<br />

of v differ by at most 1.<br />

Any b<strong>in</strong>ary search tree T that satisfies the height-balance property is said to be an<br />

AVL tree, named after the <strong>in</strong>itials of its <strong>in</strong>ventors: Adel'son-Vel'skii <strong>and</strong> L<strong>and</strong>is. An<br />

example of an AVL tree is shown <strong>in</strong> Figure 10.7.<br />

Figure 10.7: An example of an AVL tree. The keys of<br />

the entries are shown <strong>in</strong>side the nodes, <strong>and</strong> the heights<br />

of the nodes are shown next to the nodes.<br />

599

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

Saved successfully!

Ooh no, something went wrong!