Linear Probing: Exercise I
- Describe "Open Addressing with Linear Probing" as a collision resolution.
Suppose we have a hash table with capacity , and we aim to insert integer keys. Further, assume the hashCode()
is defined as the sum of the digits of key
.
Exercise Complete the table after each of the following operations, assuming collision resolution using linear probing (with step size of ). For the find()
operation, output the indices of the positions visited during the search to find the element.
[ 0 ] | [ 1 ] | [ 2 ] | [ 3 ] | [ 4 ] | [ 5 ] | [ 6 ] | Output | |
---|---|---|---|---|---|---|---|---|
insert(1111) | ||||||||
insert(5005) | ||||||||
insert(86) | ||||||||
find(5557) | ||||||||
insert(2332) | ||||||||
insert(8333) | ||||||||
find(2332) | ||||||||
insert(700) |
Solution
[ 0 ] | [ 1 ] | [ 2 ] | [ 3 ] | [ 4 ] | [ 5 ] | [ 6 ] | Output | |
---|---|---|---|---|---|---|---|---|
insert(1111) | 1111 | |||||||
insert(5005) | 5005 | 1111 | ||||||
insert(86) | 86 | 5005 | 1111 | |||||
find(5557) | 86 | 5005 | 1111 | 1: NOT_FOUND | ||||
insert(2332) | 86 | 5005 | 1111 | 2332 | ||||
insert(8333) | 86 | 5005 | 1111 | 2332 | 8333 | |||
find(2332) | 86 | 5005 | 1111 | 2332 | 8333 | 3,4,5: FOUND | ||
insert(700) | 86 | 700 | 5005 | 1111 | 2332 | 8333 |
Therefore, insert at index .
Therefore, insert at index .
Therefore, insert at index .
Lookup for at index . There is no element there! Therefore, return NOT_FOUND
.
Try to insert at index . The position is occupied. Linear probing will take us to index and then index where the element can be inserted. Therefore, insert at index .
Try to insert at index . The position is occupied, but the occupant is not the target! Linear probing will take us to index and then index and finally index where the element can be inserted. Therefore, insert at index .
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 can be found.
Try to insert at index . The position is occupied. Linear probing will take us to index where the element can be inserted. Therefore, insert at index .