Lecture15-16

Lecture15-16 - Hashing Implementation Issues EECS 233...

This preview shows pages 1–10. Sign up to view the full content.

Hashing: Implementation Issues EECS 233

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-2- Previous Lecture Hash functions desiderata Handling collisions with open addressing Linear probing Quadratic probing Double hashing Need to distinguish between “removed” and “empty” positions
-3- Implementation of Hash Tables A simple hash table with open addressing. (Another example in textbook.) public class HashTable { private class Entry { private String key; private String etymology; private boolean removed; } private Entry[] table; private int tableSize; ... } for an empty position, table[i] will equal null for a removed position, table[i] will refer to an Entry object whose removed ﬁeld equals true for an occupied position, table[i] will refer to an Entry object whose ﬁeld equals false

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-4- Constructors Initializing the hash table public HashTable(int size) { table = new Entry[size]; tableSize = size; } Initializing an entry (before insertion) private Entry(String key, String etymology) { this.key = key; this.etymology = etymology; removed = false; }
-5- Finding An Open Position Using double hashing private int probe(String key) { int i = h1(key); // ﬁrst hash function int j = h2(key); // second hash function // keep probing while the current position is occupied (non-empty and non- removed) while ( ) return i; } Does it always terminate? table[i] != null && table[i].removed==false i = (i + j) % tableSize;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-6- Finding An Open Position: Inﬁnite Loops The loop in our probe method could lead to an inﬁnite loop. When would this happen? To avoid inﬁnite loops, we can stop probing after checking n positions (n = table size) because the probe sequence will just repeat after that point. Why? for double hashing: (h1 + n*h2) % n = h1 % n (h1 + (n+1)*h2) % n = (h1 + n*h2 + h2) % n = (h1 + h2)%n (h1 + (n+2)*h2) % n = (h1 + n*h2 + 2*h2) % n = (h1 + 2*h2)%n for quadratic probing: (h1 + n 2 ) % n = h1 % n (h1 + (n+1) 2 ) % n = (h1 + n 2 + 2n + 1) % n = (h1 + 1)%n (h1 + (n+2) 2 ) % n = (h1 + n 2 + 4n + 4) % n = (h1 + 4)%n
-7- Finding An Open Position: Inﬁnite Loop Protection private int probe(String key) { int i = h1(key); // ﬁrst hash function int j = h2(key); // second hash function int iterations = 0; // keep probing until we get an empty or removed position while (table[i] != null && table[i].removed==false) { i = (i + j) % tableSize; iterations++; if (iterations > tableSize) return -1; } return i; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-8- Finding the Position of A Key Different from probe() Returns position of the key, not an available position. private int ﬁndKey(String key) { int i = h1(key); // ﬁrst hash function int j = h2(key); // second hash function int iterations = 0; // keep probing while the entry is not empty while ( ? ) { // return if key is found, otherwise continue i = (i + j) % tableSize; iterations++; if (iterations > tableSize) return -1; } return -1; } table[i] != null if (table[i].removed==false && table[i].key.equals(key)) return i;
-9- Search() Method Search for the entry with the key, and return the Etymology (in other implementation, we may return other ﬁelds instead) public String search(String key) { int i = ﬁndKey(key); if (i == -1) return null; else return table[i].etymology ; } It calls the helper method ﬁndKey() to locate the position of the key.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/07/2008 for the course EECS 233 taught by Professor Rabinovich during the Spring '08 term at Case Western.

Page1 / 31

Lecture15-16 - Hashing Implementation Issues EECS 233...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online