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.

Thus, a red-black tree achieves logarithmic worst-case runn<strong>in</strong>g times for both<br />

search<strong>in</strong>g <strong>and</strong> updat<strong>in</strong>g <strong>in</strong> a dictionary. The red-black tree data structure is slightly<br />

more complicated than its correspond<strong>in</strong>g (2,4) tree. Even so, a red-black tree has a<br />

conceptual advantage that only a constant number of tr<strong>in</strong>ode restructur<strong>in</strong>gs are<br />

ever needed to restore the balance <strong>in</strong> a red-black tree after an update.<br />

10.5.2 <strong>Java</strong> Implementation<br />

In Code Fragments 10.9 through 10.11, we show the major portions of a <strong>Java</strong><br />

implementation of a dictionary realized by means of a red-black tree. The ma<strong>in</strong><br />

class <strong>in</strong>cludes a nested class, RBNode, shown <strong>in</strong> Code Fragment 10.9, which<br />

extends the BTNode class used to represent a key-value entry of a b<strong>in</strong>ary search<br />

tree. It def<strong>in</strong>es an additional <strong>in</strong>stance variable isRed, represent<strong>in</strong>g the color of the<br />

node, <strong>and</strong> methods to set <strong>and</strong> return it.<br />

Code Fragment 10.9: Instance variables, nested<br />

class, <strong>and</strong> constructor for RBTree.<br />

661

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

Saved successfully!

Ooh no, something went wrong!