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>.
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
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] !=...
C++ language Briefly explain and write the pseudocode to delete an element from the red-black tree....
C++ language Briefly explain and write the pseudocode to delete an element from the red-black tree. Include all cases for full credit.
Using Java: Write a program that uses hash tables and reads in a string from the...
Using Java: Write a program that uses hash tables and reads in a string from the command line and a dictionary of words from standard input, and checks whether it is a "good" password. Here, assume "good" means that it (i) is at least 8 characters long, (ii) is not a word in the dictionary, (iii) is not a word in the dictionary followed by a digit 0-9 (e.g., hello5), (iv) is not two words separated by a digit (e.g.,...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT