Question: The algorithm to insert a record into a hash table, shown in Algorithm 13.3, calculates the hash value of the new record from its key,



The algorithm to insert a record into a hash table, shown in Algorithm 13.3, calculates the hash value of the new record from its key, which is an argument to the algorithm. On Lines 2 - 3, the key and next properties of the new record are set Line 4 checks if there are already entries in the hash table for that hash value. If there are no entries, the new record is added as the first element at that location on Lines 5 - 6. If there are entries, Lines 9 - 13 check if the record is already in the hash table. Lines 15 - 16 traverse the chain for the position where the new record will be in alphabetical order. On Lines 17 - 19, the pointers for the existing records are updated to include the new record. Pre - conditions Unused indices in the hash table are set to NULL. value is a valid hash table key value. Post - conditions Record inserted into the hash table at the correct location, as specified by the hash function. insert (value) index = hashSum(value, tablesize)//hash is calculated hashElementkey = value//set properties of new record hashElementnext = NULL hashElementprevious = NULL The steps to delete a record from a hash table, shown in Algorithm 13.4, follow the same pattern initially as the steps to insert and search for a record. The index for the element to delete is identified on Line 1 with a call to the hash function. Once the key value is found in the hash table chain, on Line 5, the next and previous pointers for the surrounding nodes in the chain are updated on Lines 6 - 8. One Line 9, the node is deleted, which frees the memory. Pre - conditions Unused indices in the hash table are NULL. value is a valid search key for a record in the hash table. Post - conditions Node with the specified key value deleted from the chain and the memory freed. Pointers in the linked list updated to bypass the deleted node. delete (value) index = hashSum (name, tableSize) if (hashTable [index].next ! = NULL)//there is a linked list. tmp = hashTable[index].next while (tmp ! = NULL) if (tmp.key = = value) tmp.previous.next = tmp.next Given the array: {Whiplash, Star Wars, The Godfather, Se7en, Beauty and the Beast} where after they were sent into the hashSum, the indexes returned were 0, 0, 2, 3, 0 respectively. Draw out a hash table based on the insert function shown in class, if the hashTable size of 4
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
