We are performing the following operations on a hash set implemented using separ
ID: 3846270 • Letter: W
Question
We are performing the following operations on a hash set implemented using separate chaining. The initial capacity is 5. The table automatically doubles and elements are rehashed when an insert would cause the load factor to be greater than 2 (equal to 2 does not trigger a rehash). When a rehash is triggered, all the existing elements are rehashed into the new array in the order they are encountered when iterating over the hash set. Then the element that triggered the rehashing is inserted. Show the state of the array and the chains right before resizing and the final resized state after rehashing. When we add an element to a list, add policy is to add to the end of the list. The hash function is h=val % arrSize. + == insert, - == delete 22, 20, 12, 13, 32, 12, 14, 16, 29, 20, -12, 15, 11, 19, 33, 17, 28, 10, 1, -13, 9, 2, 35 (please grow the chains downward) 0 1 2 3 4 0 1 2 3 4 5 6 7 8 9 ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ _ We are performing the following operations on a hash set implemented using separate chaining. The initial capacity is 5. The table automatically doubles and elements are rehashed when an insert would cause the load factor to be greater than 2 (equal to 2 does not trigger a rehash). When a rehash is triggered, all the existing elements are rehashed into the new array in the order they are encountered when iterating over the hash set. Then the element that triggered the rehashing is inserted. Show the state of the array and the chains right before resizing and the final resized state after rehashing. When we add an element to a list, add policy is to add to the end of the list. The hash function is h=val % arrSize. + == insert, - == delete 22, 20, 12, 13, 32, 12, 14, 16, 29, 20, -12, 15, 11, 19, 33, 17, 28, 10, 1, -13, 9, 2, 35 (please grow the chains downward) 0 1 2 3 4 0 1 2 3 4 5 6 7 8 9 ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ _ We are performing the following operations on a hash set implemented using separate chaining. The initial capacity is 5. The table automatically doubles and elements are rehashed when an insert would cause the load factor to be greater than 2 (equal to 2 does not trigger a rehash). When a rehash is triggered, all the existing elements are rehashed into the new array in the order they are encountered when iterating over the hash set. Then the element that triggered the rehashing is inserted. Show the state of the array and the chains right before resizing and the final resized state after rehashing. When we add an element to a list, add policy is to add to the end of the list. The hash function is h=val % arrSize. + == insert, - == delete 22, 20, 12, 13, 32, 12, 14, 16, 29, 20, -12, 15, 11, 19, 33, 17, 28, 10, 1, -13, 9, 2, 35 (please grow the chains downward) 0 1 2 3 4 0 1 2 3 4 5 6 7 8 9 ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ _Explanation / Answer
Creating a table of 5 elements. Load factor is 2. So, allowed elements in the table is 10. When the 11th element is about to be inserted, rehashing should happen.
Size = 5.
hash(x) = x%5
Insert 22:
hash(22) = 22%5 = 2
Insert 20:
hash(20) = 20%5 = 0
Insert 12:
hash(12) = 12%5 = 2. Collision occurs. In case of separate chaining, we keep adding more items into the same location through linked lists.
Insert 13:
hash(13) = 13%5 = 3.
Insert 32:
hash(32) = 32%5 = 2.
Insert 12:
hash(12) = 12%5 = 2.
Insert 14:
hash(14) = 14%5 = 4.
Insert 16:
hash(16) = 16%5 = 1.
Insert 29:
hash(29) = 29%5 = 4.
Insert 20:
hash(20) = 20%5 = 0.
Delete 12:
hash(12) = 12%5 = 2.
Insert 15:
hash(15) = 15%5 = 0.
Insert 11:
hash(11) = 11%5 = 1. The threshold is reached. The hash table needs to be rehashed now. New size = 5*2 = 10.
Elements are hashed again from the first index and put into the new table.
Elements order: 20, 20, 15, 16, 22, 32, 12, 13, 14, 29, 11.
Hash(x) = x%10
Insert 19:
hash(19) = 19%10 = 9.
Insert 33:
hash(33) = 33%10 = 3.
Insert 17:
hash(17) = 17%10 = 7.
Insert 28:
hash(28) = 28%10 = 8.
Insert 10:
hash(10) = 10%10 = 0.
Insert 1:
hash(1) = 1%10 = 1.
Delete 13:
hash(13) = 13%10 = 3.
Insert 9:
hash(9) = 9%10 = 9.
Insert 2:
hash(2) = 2%10 = 2.
Insert 35:
hash(35) = 35%10 = 5.
Elements in the first array:
0 ->20 -> 20 -> 15
1 -> 16
2 -> 22 -> 32 -> 12
3 -> 13
4 ->14 -> 29
Elements in the new array
0 -> 20 -> 20 -> 10
1 -> 11 -> 1
2 -> 22 -> 32 -> 12 -> 2
3 -> 33
4 -> 14
5 -> 15 -> 35
6 -> 16
7 -> 17
8 -> 28
9 -> 29 -> 19 -> 9
0 1 2 3 4Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.