We consider a hash table T$$_1$$[$$m$$] in which collisions are resolved by applying the hash coalesced with 2 separated zones: primary zone of size $$p$$ and a reserve zone of size $$r$$ ($$m=p+r$$). After adding many elements to T$$_1$$, there has been a lot of collisions.
To resolve such a problem, we decide to transfer all the elements from T$$_1$$ to a new table T$$_2$$[$$p$$] in which collisions are resolved by open addressing - separate chaining in such a way that elements that collide are stored in a BST (Binary Search Tree).
We assume the existence of a hash function h with values in [0 $$\cdots$$ p-1 ], and that keys in the BST are of type t_Element, and that 2 keys can be compared using comparison operators associated to numbers ($$<, >, ==, ...$$).
Write a function that transfers the elements from T$$_1$$ to T$$_2$$.
Difficulty level
Video recording
This exercise is mostly suitable for students
void transfer(hashtable h, hashtableBST hB)
{
int i, v;
for(i=0;i<P;i++)
{
if(!empty(h,i))
{
v=i;
insert_BST(&hB[i], h[v].data);
while (h[v].link != -1){
v = h[v].link ;
insert_BST(&hB[i], h[v].data);
}
}
}
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Rearranging values in an array