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 continuous data signal is quantized and transmitted using a PCM system. If each data sample at the receiving end of the system must be known to within 0.25% of the peak-to-peak full-scale value,...
-
A pension fund has just paid some of its liabilities, and as a result of this transaction the fund is no longer fully immunized. The fund manager decides that instead of changing the portfolio, the...
-
Water World sells three products: ski vests, slalom skis, and ski ropes. Information related to each product line is provided below: The companys annual fixed costs are approximately $741,000. a....
-
Sage Hill Company has been in business several years. At the end of the current year, the unadjusted balances show: Accounts Receivable Sales Revenue Allowance for Doubtful Accounts $272,800...
-
The Mountain Top Resort Community is an elegant, thriving four-season resort and community of over 1,200 single family homes, 1,000 time-share units, and a multi-million-dollar ski business. Guests...
-
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...
-
Arctic Ice These data give the extent of area covered by ice in arctic regions near the North Pole from September 1979 to 2015. The reduction in the amount of arctic ice is related to global climate...
-
To casually look at a Revit project set up for Worksharing, simply copy it to the Desktop using Windows Explorer. A) True B) False
-
Creating photo-realistic renderings can take a significant amount of time for your computer to process. A) True B) False
-
You can select a different Room Tag via the Type Selector. A) True B) False
-
Fill in the blank field in this text: Use the [1]_________________________palette to adjust the various fields associated with each Room in a plan view.
-
On new sheets, the sheet number on the titleblock will increase by one from the previous sheet number. A) True B) False
-
Given the following ratio-level numbers, calculate the mean: 80, 88, 76, 65, 59, and 77.
-
Multiple Choice Questions: 1. The largest component of aggregate demand is? a. Government purchases. b. Net exports. c. Consumption. d. Investment. 2. A reduction in personal income taxes, other...
-
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.
-
Provide the strengths and weakness of each of the theories of substance use disorders genetic, disease, moral, etc. What are the implications of each theory in an individual's pursuit of recovery...
-
highest mount you can ow finance 2. We sometimes need to find how long it will take a sum of money (or anything else) to grow to some specified amount. Note that you should enter PV as a negative and...
-
11. Selected information for Blake's Restaurant Supply follows. ($ millions) 2020 2021 Net sales 694 782 Cost of goods sold 450 502 Depreciation 51 61 Net income 130 142 Finished goods inventory 39...
Study smarter with the SolutionInn App