19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

oot<br />

parentOfu<br />

u<br />

Figure 47.13<br />

The nodes along the path may be<strong>com</strong>e empty as result of a transfer and<br />

fusion operation.<br />

If the node is a nonleaf node, locate the rightmost element in the left<br />

subtree of the node (lines 25–26), get the path that leads <strong>to</strong> the<br />

rightmost element from the root (line 29), replace e in the node with<br />

the rightmost element (line 31), and invoke validate <strong>to</strong> fix the<br />

rightmost node if it is empty (line 34).<br />

The validate(E e, Tree24Node u, ArrayList path)<br />

checks whether u is empty and performs a transfer or fusion operation<br />

<strong>to</strong> fix the empty node. The validate method exits when node is not empty<br />

(line 43). Otherwise, consider one of the following cases:<br />

1. If u has a left sibling with more than one element, perform a<br />

transfer on u with its left sibling (line 49).<br />

2. Otherwise, if u has a right sibling with more than one element,<br />

perform a transfer on u with its right sibling (line 52).<br />

3. Otherwise, if u has a left sibling, perform a fusion on u with<br />

its left sibling (line 55) and reset u <strong>to</strong> parentOfu (line 56).<br />

4. Otherwise, u must have a right sibling. Perform a fusion on u<br />

with its right sibling (line 59) and reset u <strong>to</strong> parentOfu (line<br />

60).<br />

Only one of the preceding cases is executed. Afterward, a new iteration<br />

starts <strong>to</strong> perform a transfer or fusion operation on a new node u if<br />

needed. Figure 47.14 shows the steps of deleting elements 20, 15, 3, 6,<br />

and 34 are deleted from a 2-4 tree in Figure 47.9(k).<br />

20<br />

16<br />

15<br />

27 34<br />

15<br />

27 34<br />

3 16 24 25 29 50<br />

3<br />

24 25<br />

29 50<br />

(a) Delete 20 (b) Replace 20 with 16<br />

12

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

Saved successfully!

Ooh no, something went wrong!