Linear Probing: Exercise II
- Describe "Open Addressing with Linear Probing" as a collision resolution.
Consider the state of the hash table at the end of the previous exercise; we want to perform two more operations:
[ 0 ] | [ 1 ] | [ 2 ] | [ 3 ] | [ 4 ] | [ 5 ] | [ 6 ] | Output | |
---|---|---|---|---|---|---|---|---|
Current State | 86 | 700 | 5005 | 1111 | 2332 | 8333 | ||
remove(2332) | ||||||||
find(8333) |
Exercise Complete the table above as you carry out the operations. Do you see any (potential) issues with the remove
operation?
Solution
[ 0 ] | [ 1 ] | [ 2 ] | [ 3 ] | [ 4 ] | [ 5 ] | [ 6 ] | Output | |
---|---|---|---|---|---|---|---|---|
Current State | 86 | 700 | 5005 | 1111 | 2332 | 8333 | ||
remove(2332) | 86 | 700 | 5005 | 1111 | 8333 | |||
find(8333) | 86 | 700 | 5005 | 1111 | 8333 | 3,4,5: NF |
Lookup at index . The position is occupied, but the occupant is not the target! Linear probing will take us to index and then index where the element will be found. We can now remove (delete) the element.
Lookup at index . The position is occupied, but the occupant is not the target! Linear probing will take us to index and then index , which is empty. At this point, the algorithm will assume is not in the Hash Table because if it were, it would have been inserted at index . Therefore, it will return NOT_FOUND
.
Removing an item may lead to not being able to find previously inserted items that collided with it.