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.

typically <strong>in</strong>volves updat<strong>in</strong>g height values <strong>and</strong><br />

perform<strong>in</strong>g local tr<strong>in</strong>ode restructur<strong>in</strong>gs (rotations).<br />

10.2.2 <strong>Java</strong> Implementation<br />

Let us now turn to the implementation details <strong>and</strong> analysis of us<strong>in</strong>g an AVL tree T<br />

with n <strong>in</strong>ternal nodes to implement an ordered dictionary of n entries. The <strong>in</strong>sertion<br />

<strong>and</strong> removal algorithms for T require that we are able to perform tr<strong>in</strong>ode<br />

restructur<strong>in</strong>gs <strong>and</strong> determ<strong>in</strong>e the difference between the heights of two sibl<strong>in</strong>g<br />

nodes. Regard<strong>in</strong>g restructur<strong>in</strong>gs, we now need to make sure our underly<strong>in</strong>g<br />

implementation of a b<strong>in</strong>ary search tree <strong>in</strong>cludes the method restructure(x),<br />

which performs a tri-node restructur<strong>in</strong>g operation (Code Fragment 10.6). It is easy<br />

to see that a restructure operation can be performed <strong>in</strong> O(1) time if T is<br />

implemented with a l<strong>in</strong>ked structure (Section 7.3.4). In our case, we assume that the<br />

B<strong>in</strong>arySearchTree class <strong>in</strong>cludes this method.<br />

Regard<strong>in</strong>g height <strong>in</strong>formation, we can explicitly store the height of each <strong>in</strong>ternal<br />

node, v, <strong>in</strong> the node itself. Alternatively, we can store the balance factor of v at v,<br />

which is def<strong>in</strong>ed as the height of the left child of v m<strong>in</strong>us the height of the right<br />

child of v. Thus, the balance factor of v is always equal to −1, 0, or 1, except dur<strong>in</strong>g<br />

an <strong>in</strong>sertion or removal, when it may become temporarily equal to −2 or +2. Dur<strong>in</strong>g<br />

the execution of an <strong>in</strong>sertion or removal, the heights <strong>and</strong> balance factors of O(logn)<br />

nodes are affected <strong>and</strong> can be ma<strong>in</strong>ta<strong>in</strong>ed <strong>in</strong> O(logn) time.<br />

608

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

Saved successfully!

Ooh no, something went wrong!