11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

324 Chap. 9 Searching/** Insert record r with key k into HT */void hashInsert(Key k, E r) {int home;// Home position for rint pos = home = h(k);// Initial positionfor (int i=1; HT[pos] != null; i++) {pos = (home + p(k, i)) % M; // Next pobe slotassert HT[pos].key().compareTo(k) != 0 :"Duplic<strong>at</strong>es not allowed";}HT[pos] = new KVpair(k, r); // Insert R}Figure 9.6 Insertion method for a dictionary implemented by a hash table.Linear ProbingWe now turn to the most commonly used form of hashing: closed hashing with nobucketing, <strong>and</strong> a collision resolution policy th<strong>at</strong> can potentially use any slot in thehash table.During insertion, the goal of collision resolution is to find a free slot in the hashtable when the home position for the record is already occupied. We can view anycollision resolution method as gener<strong>at</strong>ing a sequence of hash table slots th<strong>at</strong> canpotentially hold the record. The first slot in the sequence will be the home positionfor the key. If the home position is occupied, then the collision resolution policygoes to the next slot in the sequence. If this is occupied as well, then another slotmust be found, <strong>and</strong> so on. This sequence of slots is known as the probe sequence,<strong>and</strong> it is gener<strong>at</strong>ed by some probe function th<strong>at</strong> we will call p. The insert functionis shown in Figure 9.6.Method hashInsert first checks to see if the home slot for the key is empty.If the home slot is occupied, then we use the probe function, p(k, i) to loc<strong>at</strong>e a freeslot in the table. Function p has two parameters, the key k <strong>and</strong> a count i for wherein the probe sequence we wish to be. Th<strong>at</strong> is, to get the first position in the probesequence after the home slot for key K, we call p(K, 1). For the next slot in theprobe sequence, call p(K, 2). Note th<strong>at</strong> the probe function returns an offset fromthe original home position, r<strong>at</strong>her than a slot in the hash table. Thus, the for loopin hashInsert is computing positions in the table <strong>at</strong> each iter<strong>at</strong>ion by addingthe value returned from the probe function to the home position. The ith call to preturns the ith offset to be used.Searching in a hash table follows the same probe sequence th<strong>at</strong> was followedwhen inserting records. In this way, a record not in its home position can be recovered.A Java implement<strong>at</strong>ion for the search procedure is shown in Figure 9.7.The insert <strong>and</strong> search routines assume th<strong>at</strong> <strong>at</strong> least one slot on the probe sequenceof every key will be empty. Otherwise, they will continue in an infiniteloop on unsuccessful searches. Thus, the dictionary should keep a count of the

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

Saved successfully!

Ooh no, something went wrong!