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.

get(k)<br />

put(k,v)<br />

put(k,v)<br />

remove(k)<br />

remove(k)<br />

keys()<br />

keySet()<br />

values()<br />

values()<br />

entries()<br />

entrySet()<br />

9.1.1 A Simple List-Based Map Implementation<br />

A simple way of implement<strong>in</strong>g a map is to store its n entries <strong>in</strong> a list S,<br />

implemented as a doubly l<strong>in</strong>ked list. Perform<strong>in</strong>g the fundamental methods, get(k),<br />

put(k, v), <strong>and</strong> remove(k), <strong>in</strong>volves simple scans down S look<strong>in</strong>g for an entry with<br />

key k. We give pseudo-code for perform<strong>in</strong>g these methods on a map M <strong>in</strong> Code<br />

Fragment 9.1.<br />

This list-based map implementation is simple, but it is only efficient for very small<br />

maps. Every one of the fundamental methods takes O(n) time on a map with n<br />

entries, because each method <strong>in</strong>volves search<strong>in</strong>g through the entire list <strong>in</strong> the worst<br />

case. Thus, we would like someth<strong>in</strong>g faster.<br />

Code Fragment 9.1: <strong>Algorithms</strong> for the<br />

fundamental map methods with a list S.<br />

521

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

Saved successfully!

Ooh no, something went wrong!