Consider a hash table \(T[0\cdots m-1]\) where quadratic probing is used to resolve collision. Recall that to resolve collision using quadratic probing when inserting a key $$e$$ in the hash table , addresses \(h(e)\), \(h(e)+1\), \(h(e)+4\), \(\cdots\), \(h(e)+i^2\) are probed until a vacant position is found or \(m/2\) trials are tested.
- Write the corresponding declarations.
- Write a function \(delete(T,e)\) that deletes the key \(e\) from the table and preserves the quadratic probing characteristic.
Example: after inserting \(aa\), \(bb\), \(cc\), \(dd\), the hash table is:
\begin{array}{ |c | c | c| c| c| c|c | c | c | c| c| c| } \hline
index & 0 &1 &2 &3 &4& 5& 6 &7 &8 &9 &10 \\ \hline
e& dd & & aa & bb & & & cc & & & & \\ \hline
v=H(e)& 2& &2 &2 & & &2 & & & & \\ \hline
\end{array}
After deleting \(e=bb\) at position 3, the table becomes:
\begin{array}{ |c | c | c| c| c| c|c | c | c | c| c| c| } \hline
index & 0 &1 &2 &3 &4& 5& 6 &7 &8 &9 &10 \\ \hline
e& & & aa & cc & & & dd & & & & \\ \hline
v=H(e)& & &2 &2 & & &2 & & & & \\ \hline
\end{array}
Difficulty level
Video recording
This exercise is mostly suitable for students
int delete(hash h, element e, int size)
{
int i, nb ,step, pos, old;
int vhash = hashfunction(e,size);
nb=1;
step=0;
while(nb<(size/2))
{
if(h[(vhash+step*step)%size] == e) // found
{
pos=(vhash+step*step)%size;
while( nb<(size/2) )
{
step++;
if(h[(vhash+step*step)%size]!='0' && hashfunction(h[(vhash+step*step)%size],size)==vhash)
{
if(old==(vhash+step*step)%size) break;
h[pos] = h[(vhash+step*step)%size];
h[(vhash+step*step)%size]='0';
old = pos;
pos=(vhash+step*step)%size;
}
nb++;
}
return 1;
}
step++;
nb++;
}
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Binary search trees in levels