9.3Separate Chaining (Linked List)
Search
The whole point of a hash table is fast search. With chaining a key cannot be just anywhere in the table, only in the chain of its own bucket. So a search walks a single short linked list instead of the whole table. In this lesson you write that walk.
- Why a search looks only at the chain of one bucket
- How many nodes a successful and an unsuccessful search look at
- How the cost of a search depends on the load factor
1Idea
A search starts where insertion does: hash(key) gives the bucket. If the key is in the table, it can only be in this bucket's chain, because insertion put it there. The other buckets are never looked at.
Then the chain is walked like searching a linked list. If a node's value equals the key, that node is returned; if the end of the chain (NULL) is reached, the key is not in the table and NULL is returned. Returning the node also gives access to any other data stored with the key (for example the value in a dictionary).
A successful search stops where it finds the key; it walks part of the chain. An unsuccessful search walks the whole chain. This difference matters in exam questions about the average number of comparisons.
With open addressing a search stops at an empty cell and may pass the keys of other buckets on the way. With chaining you only meet the keys of your own bucket; a crowd in another bucket does not slow you down.
2Step by step
The drawing searches for 15. Bucket 4's chain holds 26, 37 and 15. At each step the matching line of code lights up.
- Start. The table holds 5 keys; bucket 4's chain has three nodes.
- The bucket is computed: 15 % 11 = 4.
tempstarts at the head of this bucket's chain. The other 10 buckets will not be looked at. - 26 is not 15.
tempmoves to the next node.- 37 is not 15.
tempmoves to the next node.- 15 = 15: the key is found.
- The function returns the node holding 15. Three comparisons were made.
#define M 11 struct Node { int data; struct Node* next;}; struct HashTable { struct Node* buckets[M]; int count;}; int hash(int key) { return key % M;} struct Node* search(struct HashTable* t, int key) { struct Node* temp = t->buckets[hash(key)]; while (temp != NULL) { if (temp->data == key) return temp; // found temp = temp->next; } return NULL; // not in the chain}class Node { int data; Node next; Node(int data) { this.data = data; }} class ChainedHashTable { static final int M = 11; Node[] buckets = new Node[M]; int count = 0; int hash(int key) { return key % M; } Node search(int key) { Node temp = buckets[hash(key)]; while (temp != null) { if (temp.data == key) return temp; // found temp = temp.next; } return null; // not in the chain }}M = 11 class Node: def __init__(self, data): self.data = data self.next = None class ChainedHashTable: def __init__(self): self.buckets = [None] * M self.count = 0 def h(self, key): return key % M def search(self, key): temp = self.buckets[self.h(key)] while temp is not None: if temp.data == key: return temp # found temp = temp.next return None # not in the chainThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
The bucket is found in O(1). The average chain length is the load factor α: an unsuccessful search looks at α nodes on average and a successful one at about 1 + α/2. In total O(1 + α); if every key is in one bucket, O(n).
Only one helper pointer is used.
If α is kept constant (for example by growing the table once it goes above 1), a search is O(1) on average. The worst case depends on how evenly the hash function spreads the keys over the buckets.
4Common mistake
A common mistake is searching the whole table for the key:
for (int i = 0; i < M; i++) for (struct Node* p = t->buckets[i]; p != NULL; p = p->next) if (p->data == key) return p;This code gives the right answer but throws away the whole benefit of the hash table: every search looks at every key and takes O(n + M). The key can only be in bucket hash(key); looking anywhere else is wasted work.
5Try it
The table in the drawing is searched for 48 (48 % 11 = 4). How many nodes are looked at?
The answer is B. 48 lands in bucket 4. All three nodes of the chain, 26, 37 and 15, are looked at, then NULL is reached: an unsuccessful search walks the whole chain.
Show the answer
The answer is B. 48 lands in bucket 4. All three nodes of the chain, 26, 37 and 15, are looked at, then NULL is reached: an unsuccessful search walks the whole chain.
The table in the drawing is searched for 33 (33 % 11 = 0). How many nodes are looked at?
The answer is B. Bucket 0 holds only 22. It is compared with 22, not equal, then NULL: one node. 33 is not in the table.
Show the answer
The answer is B. Bucket 0 holds only 22. It is compared with 22, not equal, then NULL: one node. 33 is not in the table.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.