Quadratic Probing: Exercise III
- Describe other probing strategies (quadratic, double hashing, ... for open address hash table.
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 quadratic probing.
[ 0 ] | [ 1 ] | [ 2 ] | [ 3 ] | [ 4 ] | [ 5 ] | [ 6 ] | |
---|---|---|---|---|---|---|---|
insert(5005) | |||||||
insert(6374) | |||||||
insert(2637) | |||||||
insert(7897) | |||||||
insert(3453) | |||||||
insert(2703) | |||||||
insert(7151) |
Solution
[ 0 ] | [ 1 ] | [ 2 ] | [ 3 ] | [ 4 ] | [ 5 ] | [ 6 ] | |
---|---|---|---|---|---|---|---|
insert(5005) | 5005 | ||||||
insert(6374) | 5005 | 6374 | |||||
insert(2637) | 5005 | 2637 | 6374 | ||||
insert(7897) | 7897 | 5005 | 2637 | 6374 | |||
insert(3453) | 7897 | 3453 | 5005 | 2637 | 6374 | ||
insert(2703) | 7897 | 3453 | 5005 | 2637 | 2703 | 6374 | |
insert(7151) | 7897 | 3453 | 7151 | 5005 | 2637 | 2703 | 6374 |
Here are the calculations:
The position [3]
is already occupied; the quadratic probe would explore the sequence:
- OCCUPIED
- Bingo!
The position [0]
is already occupied; the quadratic probe would explore the sequence:
- OCCUPIED
- OCCUPIED
- Bingo!