9.3Separate Chaining (Linked List)
Delete
Deleting is awkward with open addressing: if a cell is emptied, the path to the keys behind it breaks, so a deleted marker is left. Chaining has no such trouble. Every key sits in the linked list of its own bucket; deleting means taking one node out of that list. In this lesson you write that removal.
- How the node to delete and the node before it are found in the chain
- Why the first node of the chain and a node in the middle are unlinked differently
- Why deletion with chaining needs no marker
1Idea
First the bucket is computed: i = hash(key). Then two pointers walk the chain: temp points to the node being checked and prev to the node before it. To take a node out of a singly linked list you have to change the next field of the node before it; that is why prev is kept.
The loop stops when the key is found or the chain ends. If temp is NULL the key is not in the table and the function returns false. If it was found there are two cases. If prev is NULL, the node being deleted is the first node of the chain; then the bucket itself must point to the next node. Otherwise prev->next is linked to the node after the deleted one. This is exactly deleting from the middle of a linked list.
In C the node is released at the end with free and count goes down by one. In Java and Python nothing points to the node any more, so the garbage collector cleans it up. In C the function is called removeKey, because stdio.h already declares a remove that deletes files.
With chaining a deleted key leaves no trace behind. The other keys stay in other buckets or inside the same chain, so no key loses its path. The deleted markers of open addressing, on the other hand, pile up over time and slow searches down.
2Step by step
In the drawing bucket 4's chain is 26, 37 and 15, and 37 in the middle is deleted. At each step the matching line of code lights up.
- Start. Bucket 4's chain has three nodes; 37 will be deleted.
- The bucket is computed: 37 % 11 = 4.
tempstarts at the head of the chain andprevis NULL: there is no node before the first one.- 26 is not 37. The loop goes on.
- Both pointers move together:
prevpoints to 26 andtempto 37. - 37 = 37: the loop stops.
tempis not NULL and neither isprev, so the node is in the middle of the chain. In the drawing 37 was moved down so that the new link can be seen. prev->nextis linked to the node after 37 (15). 37 is now a node the chain no longer passes through.- In C the memory of 37 is released with
free. - The counter goes down by one and the function returns
true. The chain is now 26, 15.
#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;} bool removeKey(struct HashTable* t, int key) { int i = hash(key); struct Node* prev = NULL; struct Node* temp = t->buckets[i]; while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return false; // key not there if (prev == NULL) t->buckets[i] = temp->next; // first node of the chain else prev->next = temp->next; // unlink from the middle free(temp); t->count--; return true;}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; } boolean remove(int key) { int i = hash(key); Node prev = null; Node temp = buckets[i]; while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } if (temp == null) return false; // key not there if (prev == null) buckets[i] = temp.next; // first node of the chain else prev.next = temp.next; // unlink from the middle count--; return true; }}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 remove(self, key): i = self.h(key) prev = None temp = self.buckets[i] while temp is not None and temp.data != key: prev = temp temp = temp.next if temp is None: # key not there return False if prev is None: self.buckets[i] = temp.next # first node of the chain else: prev.next = temp.next # unlink from the middle self.count -= 1 return TrueThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
The bucket is found in O(1), then the chain is walked: O(1 + α) on average, O(n) if every key is in the same bucket. Unlinking the node itself is O(1).
Two helper pointers; the extra room is constant.
Deletion lowers the load factor of the table. If the table stays much larger than needed after many deletions, some implementations shrink it; that is the opposite of resizing.
4Common mistake
A common mistake is forgetting the first-node case:
prev->next = temp->next; // crashes if prev is NULLIf the key to delete is at the head of the chain, the loop never runs and prev stays NULL; reading prev->next crashes the program. In Java you get a NullPointerException, in Python an AttributeError. When the first node is deleted, what has to change is not a node but the bucket itself: t->buckets[i] = temp->next.
5Try it
Which assignment is made when 26 is deleted from the starting table in the drawing?
The answer is B. 26 is the first node of the chain, the loop does not run and prev stays NULL. In that case the bucket must point to the next node (37).
Show the answer
The answer is B. 26 is the first node of the chain, the loop does not run and prev stays NULL. In that case the bucket must point to the next node (37).
Why is no deleted marker left for a deleted key with chaining?
The answer is A. With open addressing the probe path to a key can pass through the emptied cell. With chaining the rest of the list is relinked when a node is removed; no key loses its path.
Show the answer
The answer is A. With open addressing the probe path to a key can pass through the emptied cell. With chaining the rest of the list is relinked when a node is removed; no key loses its path.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.