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.

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

O(n).<br />

Operation<br />

Time<br />

size, isEmpty<br />

O(1)<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 />

Thus, (2,4) trees provide for fast dictionary search <strong>and</strong> update operations. (2,4)<br />

trees also have an <strong>in</strong>terest<strong>in</strong>g relationship to the data structure we discuss next.<br />

10.5 Red-Black Trees<br />

Although AVL trees <strong>and</strong> (2,4) trees have a number of nice properties, there are some<br />

dictionary applications for which they are not well suited. For <strong>in</strong>stance, AVL trees<br />

may require many restructure operations (rotations) to be performed after a removal,<br />

<strong>and</strong> (2,4) trees may require many fus<strong>in</strong>g or split operations to be performed after<br />

either an <strong>in</strong>sertion or removal. The data structure we discuss <strong>in</strong> this section, the redblack<br />

tree, does not have these drawbacks, however, as it requires that only O(1)<br />

structural changes be made after an update <strong>in</strong> order to stay balanced.<br />

A red-black tree is a b<strong>in</strong>ary search tree (see Section 10.1) with nodes colored red <strong>and</strong><br />

black <strong>in</strong> a way that satisfies the follow<strong>in</strong>g properties:<br />

Root Property: The root is black.<br />

External Property: Every external node is black.<br />

Internal Property: The children of a red node are black.<br />

Depth Property: All the external nodes have the same black depth, def<strong>in</strong>ed as the<br />

number of black ancestors m<strong>in</strong>us one. (Recall that a node is an ancestor of itself.)<br />

An example of a red-black tree is shown <strong>in</strong> Figure 10.26.<br />

641

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

Saved successfully!

Ooh no, something went wrong!