Question

In: Computer Science

Write an algorithm to delete an element from a hash table that uses linear probing as...

Write an algorithm to delete an element from a hash table that uses linear probing as its clash resolution strategy. Analyze your algorithm and show the results using order notation?

Solutions

Expert Solution

Theory:

A hash table using linear probing for collision/ clash detection works with modulus as the hash function and the function looks as follows

h(key) = key % size

here key the value for which the have to be inserted into hash table.

size is the size of the hash table.

If the block calculated by the key is filled then it is called collision, and in that case, the resolution is to find a key such that

h(key + i) for all i= 1 .. size

and place the key in block where ever place is free or alert user that table is full.

In the same way deletion is as follows

Algorithm:

Note: step 2.1 checks the if the value at index calculated by the hash function h(x) with x as key + i contains key as asked by user

Note: removing element is implementation(language) dependent and at the ouset removing removes the value found at that index from hashTable.

1. input the key to be deleted from user.

2. found = 0

2. for i = 0 ... size

2.1. if hashTable[h(key + i)] == key then

2.1.1. remove the element

2.1.2. found = 1

2.1.3. break

3. if not found

3.1. print " key not found in hash table"

4. else

4.1. print "element deleted"

Analysis:

considering the worst case scenario we have to search all the blocks in the hash Table i.e, size

since, the hash function h(key) takes O(1) complexity.

running the loop 2.1 executes h(key) * size times

Note: This means that h(key) will be executed n times where n is equal to size

i.e, 1 * size

Note:( h(key) takes O(1); size is a variable)

therefore, deletion takes O(1 * size) => O(size) time to remove an element in worst case.

Therefore it is a liner time algorithm in worst case.

and in best case it is constant time, as it is found in the first iteration itself.

Coming to space complexity, it uses only one extra variable i.e, found, therefore space complexity is O(1).

i.e, time: O(size) or O(n)

space: O(1)

Hope your problem solved and you are appreciating this solution,

feel free to ask doubts in comment section.


Related Solutions

Consider a hash table T with m=10 slots that uses open addressing with linear probing and...
Consider a hash table T with m=10 slots that uses open addressing with linear probing and the hash function h(k,i) = ((k mod m) + i) mod m, where i = 0,1,..., m-1 is the probe number. Show T after inserting keys <867,755,535,296,866,135,669,445,524>.
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive...
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive searches or insertions that uses open addressing
Develop an algorithm to demonstrate hashing using hash table with modulo as the hash function. Assume...
Develop an algorithm to demonstrate hashing using hash table with modulo as the hash function. Assume the size of the hash table as 10. To avoid collisions in case of identical keys for two different elements, use Linear Probing collision resolution technique. using c++ add comment on the code
Can someone please write me a header file in c++ using the quadratic probing hash method...
Can someone please write me a header file in c++ using the quadratic probing hash method that will work with this program? #include "hash.h" #include <algorithm> #include <cstdlib> #include <ctime> #include <iostream> #include <list> using namespace std; const size_t KEYS_COUNT = 1000; // Number of keys to generate for testing string random_key(); class HashCheck { private: size_t count; Hash hash; public: HashCheck(Hash& h) { count = 0; hash = h; } void operator() (const string key) { if (hash[key] !=...
Write a program to insert the following elements into a hash table of size 17. The...
Write a program to insert the following elements into a hash table of size 17. The hash function is X mod 17 where X is the input element.   6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45 Use linear probing to resolve any collisions.
Write an algorithm that uses a stack, reads from a stream of objects (operations and values)...
Write an algorithm that uses a stack, reads from a stream of objects (operations and values) in prefix order, and prints the result of that evaluation. Trace 2 examples to demonstrate that your algorithm is valid. Analyze your algorithm with respect to n (the number of objects in the stream)
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array...
Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array code provided to you (to demonstrate how to insert elements in an Array). It is an Array with the following details: Size 10 Length 5. Your task is to Delete element on index 4 please use the code below and write in C++ how to delete the 4th element. #include struct Array { int A[10]; int size; int length; }; void Display(struct Array arr)...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT