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.

In Code Fragments 10.7 <strong>and</strong> 10.8, we show a complete <strong>Java</strong> class, AVLTree,<br />

implement<strong>in</strong>g a dictionary us<strong>in</strong>g an AVL tree (assum<strong>in</strong>g the parent class <strong>in</strong>cludes<br />

an implementation of the restructure method). This class extends<br />

B<strong>in</strong>arySearchTree (Code Fragments 10.3–10.5) <strong>and</strong> <strong>in</strong>cludes a nested class,<br />

AVLNode, which extends the BTNode class used to represent the nodes of a b<strong>in</strong>ary<br />

tree. The AVLNode class def<strong>in</strong>es an additional <strong>in</strong>stance variable height,<br />

represent<strong>in</strong>g the height of the node. We get our b<strong>in</strong>ary tree to use this node class<br />

<strong>in</strong>stead of the BTNode class simply by overrid<strong>in</strong>g the createNode method,<br />

which is used exclusively to create new b<strong>in</strong>ary tree nodes. Class AVLTree <strong>in</strong>herits<br />

methods size, isEmpty, f<strong>in</strong>d, <strong>and</strong> f<strong>in</strong>dAll from its superclass,<br />

B<strong>in</strong>arySearchTree, but overrides methods <strong>in</strong>sert <strong>and</strong> remove to keep the<br />

search tree balanced.<br />

Method <strong>in</strong>sert (Code Fragment 10.8) beg<strong>in</strong>s by call<strong>in</strong>g the superclass's <strong>in</strong>sert<br />

method, which <strong>in</strong>serts the new entry <strong>and</strong> assigns the <strong>in</strong>sertion position (for example,<br />

the node stor<strong>in</strong>g key 54 <strong>in</strong> Figure 10.8) to the <strong>in</strong>stance variable actionPos. The<br />

auxiliary method rebalance is then used to traverse the path from the <strong>in</strong>sertion<br />

position to the root. This traversal updates the heights of all the nodes visited, <strong>and</strong><br />

performs a tr<strong>in</strong>ode restructur<strong>in</strong>g if necessary. Similarly, method remove (Code<br />

Fragment 10.8) beg<strong>in</strong>s by call<strong>in</strong>g the superclass's remove method, which performs<br />

the removal of the entry <strong>and</strong> assigns the position replac<strong>in</strong>g the deleted one to<br />

<strong>in</strong>stance variable actionPos. The auxiliary method rebalance is then used to<br />

traverse the path from the removed position to the root, perform<strong>in</strong>g any needed<br />

restructur<strong>in</strong>gs.<br />

Code Fragment 10.7: Constructor <strong>and</strong> auxiliary<br />

methods of class AVLTree.<br />

609

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

Saved successfully!

Ooh no, something went wrong!