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.

10.3.2 When to Splay<br />

The rules that dictate when splay<strong>in</strong>g is performed are as follows:<br />

• When search<strong>in</strong>g for key k, if k is found at a node x, we splay x, else we<br />

splay the parent of the external node at which the search term<strong>in</strong>ates<br />

unsuccessfully. For example, the splay<strong>in</strong>g <strong>in</strong> Figures 10.15 <strong>and</strong> 10.16 would be<br />

performed after search<strong>in</strong>g successfully for key 14 or unsuccessfully for key 14.5.<br />

• When <strong>in</strong>sert<strong>in</strong>g key k, we splay the newly created <strong>in</strong>ternal node where k<br />

gets <strong>in</strong>serted. For example, the splay<strong>in</strong>g <strong>in</strong> Figures 10.15 <strong>and</strong> 10.16 would be<br />

performed if 14 were the newly <strong>in</strong>serted key. We show a sequence of <strong>in</strong>sertions <strong>in</strong><br />

a splay tree <strong>in</strong> Figure 10.17.<br />

Figure 10.17: A sequence of <strong>in</strong>sertions <strong>in</strong> a splay tree:<br />

(a) <strong>in</strong>itial tree; (b) after <strong>in</strong>sert<strong>in</strong>g 2; (c) after splay<strong>in</strong>g; (d)<br />

after <strong>in</strong>sert<strong>in</strong>g 3; (e) after splay<strong>in</strong>g; (f) after <strong>in</strong>sert<strong>in</strong>g 4;<br />

(g) after splay<strong>in</strong>g.<br />

619

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

Saved successfully!

Ooh no, something went wrong!