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.

Efficiency Trade-Offs with an Array-Based Sequence<br />

In this array implementation of a sequence, the addFirst, addBefore,<br />

addAfter, <strong>and</strong> remove methods take O(n) time, because we have to shift<br />

position objects to make room for the new position or to fill <strong>in</strong> the hole created by<br />

the removal of the old position (just as <strong>in</strong> the <strong>in</strong>sert <strong>and</strong> remove methods based on<br />

<strong>in</strong>dex). All the other position-based methods take O(1) time.<br />

6.5 Case Study: The Move-to-Front Heuristic<br />

Suppose we would like to ma<strong>in</strong>ta<strong>in</strong> a collection of elements while keep<strong>in</strong>g track of<br />

the number of times each element is accessed. Keep<strong>in</strong>g such access counts allows us<br />

to know which elements are among the "top ten" most popular, for <strong>in</strong>stance.<br />

Examples of such scenarios <strong>in</strong>clude a Web browser that keeps track of the most<br />

popular Web addresses (or URLs) a user visits or a photo album program that<br />

ma<strong>in</strong>ta<strong>in</strong>s a list of the most popular images a user views. In addition, a favorites list<br />

could be used <strong>in</strong> a graphical user <strong>in</strong>terface (GUI) to keep track of the most popular<br />

buttons used <strong>in</strong> a pull-down menu, <strong>and</strong> then present the user with condensed pulldowns<br />

conta<strong>in</strong><strong>in</strong>g the most popular options.<br />

Therefore, <strong>in</strong> this section, we consider how we can implement a favorite list ADT,<br />

which supports the size() <strong>and</strong> isEmpty() methods as well as the follow<strong>in</strong>g:<br />

access(e):<br />

Access the element e, <strong>in</strong>crement<strong>in</strong>g its access count, <strong>and</strong> add<strong>in</strong>g it to the<br />

favorites list if it is not already present.<br />

remove(e):<br />

Remove element e from the favorites list, provided it is already there.<br />

355

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

Saved successfully!

Ooh no, something went wrong!