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

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

View Full Document Right Arrow Icon
Hashing: Implementation Issues EECS 233
Background image of page 1

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

View Full DocumentRight Arrow Icon
-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
Background image of page 2
-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 field equals true for an occupied position, table[i] will refer to an Entry object whose field equals false
Background image of page 3

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

View Full DocumentRight Arrow Icon
-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; }
Background image of page 4
-5- Finding An Open Position Using double hashing private int probe(String key) { int i = h1(key); // first 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;
Background image of page 5

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

View Full DocumentRight Arrow Icon
-6- Finding An Open Position: Infinite Loops The loop in our probe method could lead to an infinite loop. When would this happen? To avoid infinite 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
Background image of page 6
-7- Finding An Open Position: Infinite Loop Protection private int probe(String key) { int i = h1(key); // first 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; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
-8- Finding the Position of A Key Different from probe() Returns position of the key, not an available position. private int findKey(String key) { int i = h1(key); // first 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;
Background image of page 8
-9- Search() Method Search for the entry with the key, and return the Etymology (in other implementation, we may return other fields instead) public String search(String key) { int i = findKey(key); if (i == -1) return null; else return table[i].etymology ; } It calls the helper method findKey() to locate the position of the key.
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online