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.

At first, this implementation may seem to be no better than the sequence-based data<br />

structure, but we add the follow<strong>in</strong>g simple heuristics to make it run faster:<br />

Union-by-Size: Store, with each position node p, the size of the subtree rooted<br />

at p. In a union operation, make the tree of the smaller set become a subtree<br />

of the other tree, <strong>and</strong> update the size field of the root of the result<strong>in</strong>g tree.<br />

Path Compression: In a f<strong>in</strong>d operation, for each node v that the f<strong>in</strong>d visits,<br />

reset the parent po<strong>in</strong>ter from v to po<strong>in</strong>t to the root. (See Figure 11.19.)<br />

These heuristics <strong>in</strong>crease the runn<strong>in</strong>g time of an operation by a constant factor, but<br />

as we discuss below, they significantly improve the amortized runn<strong>in</strong>g time.<br />

Figure 11.19: Path-compression heuristic: (a) path<br />

traversed by operation f<strong>in</strong>d on element 12; (b)<br />

restructured tree.<br />

724

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

Saved successfully!

Ooh no, something went wrong!