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.

generic merg<strong>in</strong>g takes time proportional to the number of elements. Thus, we<br />

have the follow<strong>in</strong>g:<br />

Proposition 11.8: The set ADT can be implemented with an ordered<br />

sequence <strong>and</strong> a generic merge scheme that supports operationsunion,<br />

<strong>in</strong>tersect, <strong>and</strong>subtract <strong>in</strong> O(n) time, where n denotes the sum of sizes of<br />

the sets <strong>in</strong>volved.<br />

Generic Merg<strong>in</strong>g as a Template Method Pattern<br />

The generic merge algorithm is based on the template method pattern (see<br />

Section 7.3.7). The template method pattern is a software eng<strong>in</strong>eer<strong>in</strong>g design<br />

pattern describ<strong>in</strong>g a generic computation mechanism that can be specialized by<br />

redef<strong>in</strong><strong>in</strong>g certa<strong>in</strong> steps. In this case, we describe a method that merges two<br />

sequences <strong>in</strong>to one <strong>and</strong> can be specialized by the behavior of three abstract<br />

methods.<br />

Code Fragment 11.9 shows the class Merge provid<strong>in</strong>g a <strong>Java</strong> implementation of<br />

the generic merge algorithm.<br />

Code Fragment 11.9: Class Merge for generic<br />

merg<strong>in</strong>g.<br />

717

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

Saved successfully!

Ooh no, something went wrong!