15.04.2018 Views

programming-for-dummies

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

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

356<br />

Understanding Hash Tables<br />

If you wanted to find the data associated with the key moron, the computer<br />

would first calculate its hash value, which is five.<br />

Next, it’d try to match this value with an existing value stored in the hash<br />

table. In this case, it finds that the hash value of 5 matches up to the key<br />

moron and the data Dick Ross, which is the data you wanted in the first<br />

place.<br />

Basically, a hash table works by searching through a sorted list of data (the<br />

hash values calculated from the key) rather than the unsorted list of data.<br />

Hash function collisions<br />

The hash function used to create a hash table can greatly determine the efficiency<br />

of that hash table. Ideally, the hash function should create a different<br />

hash value <strong>for</strong> every key. Un<strong>for</strong>tunately, that’s not always possible, which<br />

means that sometimes the hash function can create identical hash values<br />

from different keys.<br />

In the previous example, the hash function converted a key to a hash value<br />

just by counting the number of characters used in the key. So if two different<br />

keys have the same number of characters, the hash function will create the<br />

same hash value like this:<br />

blackbook := Dictionary new.<br />

blackbook at: ‘moron’ put: ‘Dick Ross’.<br />

blackbook at: ‘imbecile’ put: ‘John Adams’.<br />

Blackbook at: ‘idiot’ put: ‘Sally Evans’.<br />

Using the simple hash function to count the number of characters in each<br />

key would create a table like this:<br />

Hash table<br />

Data<br />

5 Dick Ross<br />

8 John Adams<br />

5 Sally Evans<br />

Hash tables can’t have duplicate values because every hash value must<br />

match with a single chunk of data. A collision occurs when a hash function<br />

creates duplicate values from different keys. Here are two ways to prevent<br />

collisions:

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

Saved successfully!

Ooh no, something went wrong!