Although keys in a map are distinct, the binary search algorithm can be applied in a more
Question:
Transcribed Image Text:
public class Sorted TableMap
public class Sorted TableMap extends AbstractSortedMap { private ArrayList> table public Sorted TableMap() { super(); } public Sorted TableMap(Comparator comp) { super(comp); } /** Returns the smallest index for range table[low.high] inclusive storing an entry with a key greater than or equal to k (or else index high+1, by convention). */ private int findIndex(K key, int low, int high) { if (high < low) return high + 1; int mid = (low + high) / 2; int comp = compare(key, table.get(mid)); if (comp == 0) return mid; else if (comp < 0) return findlndex(key, low, mid – 1); // answer is left of mid (or possibly mid) else new ArrayList<>((); // no entry qualifies 10 11 // found exact match 12 13 14 15 return findlndex(key, mid + 1, high); // answer is right of mid } /** Version of findIndex that searches the entire table */ private int findlndex(K key) { return findIndex(key, 0, table.size() – 1); } /** Returns the number of entries in the map. */ public int size() { return table.size(); } /** Returns the value associated with the specified key (or else null). */ public V get(K key) { int j = findlndex(key); if (j == size() || compare(key, table.get(j)) != 0) return null; // no match return table.get(j).getValue( ); 16 17 18 19 20 21 22 23 24 25 26 27 /** Associates the given value with the given key, returning any overridden value.*/ public V put(K key, V value) { int j = findlndex(key); if (j < size() && compare(key, table.get(j)) == 0) return table.get(j).setValue(value); table.add(j, new MapEntry(key, value)); return null; } /** Removes the entry having key k (if any) and returns its associated value. */ public V remove(K key) { int j = findlndex(key); if (j == size() || compare(key, table.get(j)) != 0) return null; // no match return table.remove(j).getValue(); } 28 29 30 // match exists 31 32 // otherwise new 33 34 35 36 37 38 39 40 41
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 62% (16 reviews)
The original version of Code Fragment 1011 does not guarantee that it finds the leftmost occ...View the full answer
Answered By
Branice Buyengo Ajevi
I have been teaching for the last 5 years which has strengthened my interaction with students of different level.
4.30+
1+ Reviews
10+ Question Solved
Related Book For
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Question Posted:
Students also viewed these Computer science questions
-
Write a program that animates the binary search algorithm. Create an array with numbers from 1 to 20 in this order. The array elements are displayed in a histogram, as shown in Figure 22.13. You need...
-
Recall that Chapter 8 described the binary search algorithm for finding a particular entry in an ordered list. The idea behind binary search is to begin looking in the exact center of the list. If...
-
During the course of an algorithm, we sometimes find that we need to maintain past versions of a dynamic set as it is updated. Such a set is called persistent. One way to implement a persistent set...
-
The administrator of Hope Hospital has been asked to perform an activity analysis of the emergency room (ER). The ER activities include cost of quality and other patient care activities. The lab...
-
Altira Corporation uses a periodic inventory system. The following information related to its merchandise inventory during the month of August 2018 is available: Aug. 1 Inventory on hand-2,000 units;...
-
a. Create a data flow diagram of the current system. b. Create a system flowchart of the existing system. c. Analyze the internal control weaknesses in the system. Model your response according to...
-
Visualize yourself as the manager of an athletic club. Give three examples of data you might be able to use in making decisions about how to improve the profitability of the club. LO.1
-
Because of the individuality of people, there always exist differing views of what management is all about. Below are lists of possible perspectives and a selected group of organizational members....
-
BatCo makes baseball bats. Each bat requires 2 pounds of wood at $18 per pound and 0.25 direct labor hour at $20 per hour. Overhead is applied at the rate of $40 per direct labor hour. Prepare a...
-
Find the third Taylor polynomial P3(x) for the function f (x) = (x 1) ln x about x0 = 1. a. Use P3(0.5) to approximate f (0.5). Find an upper bound for error |f (0.5) P3(0.5)| using the error...
-
Suppose we are given two sorted search tables S and T, each with n entries (with S and T being implemented with arrays). Describe an O(log 2 n)-time algorithm for finding the k th smallest key in the...
-
Describe how to perform a removal from a hash table that uses linear probing to resolve collisions where we do not use a special marker to represent deleted elements. That is, we must rearrange the...
-
Use the Venn diagram in Fig. to list the set of elements in roster form. A © B (2 4 10 11
-
Ja-San Company was started on January 1,2007, when the owners invested \($160,000\) cash in the business. During 2007, the company earned cash revenues of \($90,000\) and incurred cash expenses of...
-
Write a program using the programming language of your choice to implement the representation you designed for Review Question 3.3. Have your program solve the problem, and have it show on the screen...
-
All the lenses in Figure P33.98 are surrounded by air. Which of the lenses are converging, and which are diverging? Data from Figure P33.98 A B C D E F )(II)
-
Change the Growth and GrowthDriver classes described in the Improved Accuracy and Efficiency. Using a Step-with-Midpoint Algorithm subsection. Run your modified program with these inputs: For your...
-
For the three-element series circuit in Fig. 9-39, (a) Find the current I; (b) Find the voltage across each impedance and construct the voltage phasor diagram which shows that V 1 + V 2 + V 3 = 100 0...
-
In Exercises find the slope of the tangent line to the graph of the function at the given point. h(t) = t + 4t, (1,5)
-
[a] Two foam blocks, each with a charge of 19 micro coulombs (1 C = 10-6 C), are both held in place 19 cm apart in the east-west direction. A foam ball with a charge 49 C is placed 55 cm north of the...
-
Why don't we need to set or reset the prev attributes of objects in the implementation of the ALLOCATE-OBJECT and FREE-OBJECT procedures?
-
Write an O(n)-time non recursive procedure that, given an n-node binary tree, prints out the key of each node in the tree. Use a stack as an auxiliary data structure.
-
As written, each loop iteration in the LIST-SEARCH procedure requires two tests: one for x L.nil and one for x.key k. Show how to eliminate the test for x L.nil in each iteration.
-
Lou Barlow, a divisional manager for Sage Company, has an opportunity to manufacture and sell one of two new products for a five - year period. His annual pay raises are determined by his division s...
-
Consider a 5 year debt with a 15% coupon rate paid semi-annually, redeemable at Php1,000 par. The bond is selling at 90%. The flotation cost is Php50 per bind. The firm's tax bracket is 30%.
-
A project will generate annual cash flows of $237,600 for each of the next three years, and a cash flow of $274,800 during the fourth year. The initial cost of the project is $749,600. What is the...
Study smarter with the SolutionInn App