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.

2log(n + 1). The other <strong>in</strong>equality, log(n + 1) ≤ h, follows from Proposition 7.10 <strong>and</strong><br />

the fact that T has n <strong>in</strong>ternal nodes.<br />

We assume that a red-black tree is realized with a l<strong>in</strong>ked structure for b<strong>in</strong>ary trees<br />

(Section 7.3.4), <strong>in</strong> which we store a dictionary entry <strong>and</strong> a color <strong>in</strong>dicator at each<br />

node. Thus the space requirement for stor<strong>in</strong>g n keys is O(n). The algorithm for<br />

search<strong>in</strong>g <strong>in</strong> a red-black tree T is the same as that for a st<strong>and</strong>ard b<strong>in</strong>ary search tree<br />

(Section 10.1). Thus, search<strong>in</strong>g <strong>in</strong> a red-black tree takes O(logn) time.<br />

10.5.1 Update Operations<br />

Perform<strong>in</strong>g the update operations <strong>in</strong> a red-black tree is similar to that of a b<strong>in</strong>ary<br />

search tree, except that we must additionally restore the color properties.<br />

Insertion<br />

Now consider the <strong>in</strong>sertion of an entry with key k <strong>in</strong>to a red-black tree T, keep<strong>in</strong>g<br />

<strong>in</strong> m<strong>in</strong>d the correspondence between T <strong>and</strong> its associated (2,4) tree T ′ <strong>and</strong> the<br />

<strong>in</strong>sertion algorithm for T ′. The algorithm <strong>in</strong>itially proceeds as <strong>in</strong> a b<strong>in</strong>ary search<br />

tree (Section 10.1.2). Namely, we search for k <strong>in</strong> T until we reach an external<br />

node of T, <strong>and</strong> we replace this node with an <strong>in</strong>ternal node z, stor<strong>in</strong>g (k,x) <strong>and</strong><br />

hav<strong>in</strong>g two external-node children. If z is the root of T, we color z black, else we<br />

color z red. We also color the children of z black. This action corresponds to<br />

<strong>in</strong>sert<strong>in</strong>g (k,x) <strong>in</strong>to a node of the (2,4) tree T ′ with external children. In addition,<br />

this action preserves the root, external <strong>and</strong> depth properties of T, but it may<br />

violate the <strong>in</strong>ternal property. Indeed, if z is not the root of T <strong>and</strong> the parent v of z<br />

is red, then we have a parent <strong>and</strong> a child (namely, v <strong>and</strong> z) that are both red. Note<br />

that by the root property, v cannot be the root of T, <strong>and</strong> by the <strong>in</strong>ternal property<br />

(which was previously satisfied), the parent u of v must be black. S<strong>in</strong>ce z <strong>and</strong> its<br />

parent are red, but z's gr<strong>and</strong>parent u is black, we call this violation of the <strong>in</strong>ternal<br />

property a double red at node z.<br />

To remedy a double red, we consider two cases.<br />

Case 1: The Sibl<strong>in</strong>g w of v is Black. (See Figure 10.28.) In this case, the double<br />

red denotes the fact that we have created <strong>in</strong> our red-black tree T a malformed<br />

replacement for a correspond<strong>in</strong>g 4-node of the (2,4) tree T 1 , which has as its<br />

children the four black children of u, v, <strong>and</strong> z. Our malformed replacement has<br />

one red node (v) that is the parent of another red node (z), while we want it to<br />

have the two red nodes as sibl<strong>in</strong>gs <strong>in</strong>stead. To fix this problem, we perform a<br />

tr<strong>in</strong>ode restructur<strong>in</strong>g of T. The tr<strong>in</strong>ode restructur<strong>in</strong>g is done by the operation<br />

restructure(z), which consists of the follow<strong>in</strong>g steps (see aga<strong>in</strong> Figure 10.28; this<br />

operation is also discussed <strong>in</strong> Section 10.2):<br />

644

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

Saved successfully!

Ooh no, something went wrong!