Suppose we want to store items a, b, c, d, e, f, g, whose keys are:a.key=23b.key=7c.key=54d.key=21e.key=12f.key=42g.key=39We want to store them in a hash table of size 7 implemented with a dynamic array A, using the hash function h(k) = k mod 7, by handling collisions using chains.Start with the array A being of size 7, with all entries initialised to None (case-sensitive), and insert the items above in order (a, b, c, d, e, f, and then g). Fill the blanks with the contents of each entry: if there is only one item in A[ i ], then just write the name of the item (not the key!), for instance "c" without the double quotes; if A points to a chain containing multiple items, just list them in alphabetical order by separating them with a comma and no spaces. For example, if A[ 2 ] points to a chain containing items a and b, fill the blank next to A[ 2 ] with "a,b" without the double quotes.
Question
Suppose we want to store items a, b, c, d, e, f, g, whose keys are:a.key=23b.key=7c.key=54d.key=21e.key=12f.key=42g.key=39We want to store them in a hash table of size 7 implemented with a dynamic array A, using the hash function h(k) = k mod 7, by handling collisions using chains.Start with the array A being of size 7, with all entries initialised to None (case-sensitive), and insert the items above in order (a, b, c, d, e, f, and then g). Fill the blanks with the contents of each entry: if there is only one item in A[ i ], then just write the name of the item (not the key!), for instance "c" without the double quotes; if A points to a chain containing multiple items, just list them in alphabetical order by separating them with a comma and no spaces. For example, if A[ 2 ] points to a chain containing items a and b, fill the blank next to A[ 2 ] with "a,b" without the double quotes.
Solution
Here is how you would insert the items into the hash table:
-
Start with an empty hash table of size 7: A = [None, None, None, None, None, None, None]
-
Insert item a with key 23: h(23) = 23 mod 7 = 2, so A[2] = "a"
-
Insert item b with key 7: h(7) = 7 mod 7 = 0, so A[0] = "b"
-
Insert item c with key 54: h(54) = 54 mod 7 = 5, so A[5] = "c"
-
Insert item d with key 21: h(21) = 21 mod 7 = 0, so A[0] = "b,d"
-
Insert item e with key 12: h(12) = 12 mod 7 = 5, so A[5] = "c,e"
-
Insert item f with key 42: h(42) = 42 mod 7 = 0, so A[0] = "b,d,f"
-
Insert item g with key 39: h(39) = 39 mod 7 = 4, so A[4] = "g"
So, the final hash table would look like this:
A[0] = "b,d,f" A[1] = None A[2] = "a" A[3] = None A[4] = "g" A[5] = "c,e" A[6] = None
Similar Questions
Suppose we want to store items a, b, c, d, e, f, g, whose keys are:a.key=23b.key=7c.key=54d.key=21e.key=12f.key=42g.key=39We want to store them in a hash table of size 7 implemented with a dynamic array A, using the hash function h(k) = k mod 7, by handling collisions using linear probing.Run the following operations in order, assuming that the array A is of size 7 and is initialised to None everywhere at the start.1. insert(a)2. insert(b)3. insert(c)4. insert(d)5. insert(e)6. remove(23)7. remove(21)8. insert(f)9. insert(g)What are the entries of the array after performing the above operations in the order given?
Design a hash function for storing strings (e.g., names) in a hash table of size 10. Describe how your hash function handles collisions and provide an example of inserting three names into the hash table.
Sindhu is working on implementing a hash table with linear probing to efficiently store a set of unique integer keys. She is seeking your help to develop a program that accomplishes this.Your task is to write a program that takes a series of integers as input, hashes them into a hash table, resolves any collisions using linear probing, and then displays the keys and their corresponding indices in ascending order of their original insertion.ExampleInput 5514 16 17 26 10Output index: 0, value: 10index: 1, value: 16index: 2, value: 17index: 3, value: 26index: 4, value: 14ExplanationThe keys are inserted into the hash table division method using linear probing to resolve any collisions.Key 14 is inserted at index 4.Key 16 is inserted at index 1.Key 17 is inserted at index 2.Key 26 initially collides with index 1 but is inserted at the next available slot, which is index 3, using linear probing.Key 10 is inserted at index 0.The keys and their respective indices are sorted in ascending order of their original insertion.Input format :The first line of input consists of an integer size, which represents the size of the hash table.The second line of input consists of an integer n, which represents the number of keys to be inserted.The third line of input consists of n integers separated by a space, representing the keys to be inserted.Output format :For each key, the output displays a line with the following format: "index: [index], value: [value]".where [index] is the index in the hash table in ascending order, where the key is stored after resolving collisions, and [value] is the corresponding key value.Refer to the sample output for the formatting specifications.Code constraints :The test cases will fall under the following constraints:1 ≤ size ≤ 101 ≤ n ≤ 101 ≤ keys ≤ 100Sample test cases :Input 1 :5514 16 17 26 10Output 1 :index: 0, value: 10index: 1, value: 16index: 2, value: 17index: 3, value: 26index: 4, value: 14Input 2 :10510 43 68 90 13Output 2 :index: 0, value: 10index: 1, value: 90index: 3, value: 43index: 4, value: 13index: 8, value: 68
Consider a hash table with 11 slots. The hash functionis h(k) = k mod 11. The collisions are resolved byopen Hashing(Chaining). The following 11 keys are inserted in theorder: 28, 19, 15, 20, 33, 30, 42, 63, 60, 32, 43. Themaximum, minimum, and average chain lengths in thehash table, respectively, are-
Hash Table Operations:Consider a hash table with 7 slots that uses chaining to resolve collisions. The hash function is h(x) = x mod 7. Show the structure of the hash table after inserting the following sequence of integers: {15, 22, 36, 7, 10, 42, 29}. Demonstrate the process of searching for the number 22 in the hash table.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.