2 Problem 1. Last week we implemented a max heap. Show the changes that are necessary to turn it into a min heap. Indicate the changes by writing comments on your max heap code. Note: It is preferable to use your own code. However, a sample code is in the solution folder. 2. Consider the following hashing scheme: a) The hash table has 26 slots indexed 0 to 25. b) The hash function takes a string and returns a number between 0 and 25, depending on the first letter of the string. That is, if the first letter of the string is 'A', it returns 0, if it is 'B' it returns 1 and so on. c) To resolve collisions, we use linear probing with an increment of -1. That is, if a slot is taken, we use the previous one. We insert the following strings into the table in the given order: Bach, Dvorak, Beethoven, Debussy, Chopin Show the contents of the table after each insertion. 3. Solve question 2 if linked lists are used to resolve collisions. 3 Deliverables 1. A PDF file of all the solutions. 2. Code file(s). 1
Added by Karen W.
Close
Step 1
In the max heap implementation, the root node is always the maximum element. In order to turn it into a min heap, we need to make the root node the minimum element. Show more…
Show all steps
Your feedback will help us improve your experience
Ravindra Yadav and 71 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Question 5* The letters of the word KNIGHTED form the list (Xi)1..8 = (K, N, I, G, H, T, E, D). This list is to be sorted into alphabetical order using Selection sort. The sorting is to be achieved by progressively modifying an index function π, rather than by shuffling members of the list itself. So initially (Xi)1..8 = (Xπ(i))1..8 where π = (1 2 3 4 5 6 7 8 / 1 2 3 4 5 6 7 8) and when sorting is complete π is sufficiently changed so that (Xπ(i))1..8 is in order. (a) First apply the Least Element algorithm to (Xi)1..8. Demonstrate the application by completing the trace table at right. (b) Write out the modified index function π resulting from (a). (c) Now apply the Least Element algorithm to (Xi)2..8 using this modified π, again demonstrating the application by a trace table. (d) Write out the newly modified index function π resulting from (c). (e) Without making trace tables, write out the state of index function π after each of the remaining applications of the Least element algorithm needed to complete the Selection sort of (K, N, I, G, H, T, E, D). (f) What is the total number of comparisons used during this sort? (g) By contrast, how many comparisons, in total, would be used to sort (K, N, I, G, H, T, E, D) using the Merge sort algorithm? To find out, carry out the Merge sort algorithm on (K, N, I, G, H, T, E, D) and carefully count the comparisons, remembering that when the Merge algorithm reaches a stage where one of its input lists is empty, it does not need any more comparisons to complete its task.
Sri K.
Write a simple hashing function that uses addition and multiplication, and then find a pair of strings that will return the same hash value for different strings (i.e., you will cause a Hash Collision). The algorithm uses multiplication based on the position of a letter in the hash to avoid a hash collision when two letters are transposed, like in 'ABCDE' and 'ABDCE'. Your strings need to be at least three characters long and no more than ten characters long. Here is the code that computes your hash: ``` while True: txt = input("Enter a string: ") if len(txt) < 1: break hv = 0 pos = 0 for let in txt: pos = (pos % 2) + 1 hv = (hv + (pos * ord(let))) % 1000000 print(let, pos, ord(let), hv) print(hv, txt) ``` For simplicity, we will only use upper and lower case ASCII letters in our text strings. Enter a string: ABCDE A 1 65 65 B 2 66 197 C 1 67 264 D 2 68 400 E 1 69 469 469 ABCDE Enter a string: BACDE B 1 66 66 A 2 65 196 C 1 67 263 D 2 68 399 E 1 69 468 468 BACDE
Supreeta N.
Akash M.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD