question stringlengths 34 5.67k | answer stringlengths 20 20.1k | support_files listlengths 0 4 | metadata dict |
|---|---|---|---|
About how many compares are required, on the average, to find the smallest of N items using select()? | 2.1.7
Selection sort because even though both selection sort and insertion sort will run in quadratic time, selection sort will
only make N exchanges, while insertion sort will make N * N / 2 exchanges.
Thanks to LudekCizinsky (https://github.com/LudekCizinsky), rg9a27 (https://github.com/rg9a27) and
BOTbkcd (https:/... | [] | {
"number": "2.5.7",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Exercise"
} |
Write a program Frequency that reads strings from standard input and prints the number of times each string occurs, in descending order of frequency. | 2.1.8
Quadratic. Insertion sort's running time is linear when the array is already sorted or all elements are equal.
With three possible values the running time quadratic. | [] | {
"number": "2.5.8",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Exercise"
} |
Develop a data type that allows you to write a client that can sort a file such as the one shown at right. | 2.1.9
a[]
h i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
E A S Y S H E L L S O R T Q U E S T I O N
13 13 13 E A S Y S H E L L S O R T Q U E S T I O N
13 14 14 E A S Y S H E L L S O R T Q U E S T I O N
13 15 2 E A E Y S H E L L S O R T Q U S S ... | [] | {
"number": "2.5.9",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Exercise"
} |
Create a data type Version that represents a software version number, such as 115.1.1, 115.10.1, 115.10.2. Implement the Comparable interface so that 115.1.1 is less than 115.10.1, and so forth. | 2.1.10
Insertion sort is faster than selection sort for h-sorting because as "h" decreases, the array becomes partially sorted.
Insertion sort makes less comparisons in partially sorted arrays than selection sort.
Also, when h-sorting, eventually h will have an increment value of 1.
Using selection sort with an increme... | [] | {
"number": "2.5.10",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Exercise"
} |
Load balancing. Write a program LPT.java that takes an integer M as a command-line argument, reads job names and processing times from standard input and prints a schedule assigning the jobs to M processors that approximately minimizes the time when the last job completes using the longest processing time first rule, a... | 2.1.13 - Deck sort
I would use selection sort, comparing the cards first by suit, and if they have the same suit, by rank.
As we are dealing with physical objects it makes sense to minimize the number of swaps.
With selection sort it may be needed to look at more cards than insertion sort (twice as many in the average ... | [] | {
"number": "2.5.13",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Sort by reverse domain. Write a data type Domain that represents domain names, including an appropriate compareTo() method where the natural order is in order of the reverse domain name. For example, the reverse domain of cs.princeton.edu is edu.princeton.cs. This is useful for web log analysis. Hint: Use s.split("\\."... | 2.1.14 - Dequeue sort
I would use a variation of bubble sort.
1- I would compare both top cards and, if the top card were bigger than the second card, I would swap them.
2- I would mark the top card, so I could know it was the first card (in this iteration) sent to the bottom of the deck.
3- I would send the top card ... | [] | {
"number": "2.5.14",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Spam campaign. To initiate an illegal spam campaign, you have a list of email addresses from various domains (the part of the email address that follows the @ symbol). To better forge the return addresses, you want to send the email from another user at the same domain. For example, you might want to forge an email fro... | 2.1.15 - Expensive exchange
The clerk should use selection sort. Since the cost of compares is low, the N^2 complexity won't be a problem.
And it guarantees a cost of at most N exchanges. | [] | {
"number": "2.5.15",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Idle time. Suppose that a parallel machine processes N jobs. Write a program that, given the list of job start and finish times, finds the largest interval where the machine is idle and the largest interval where the machine is not idle. | 2.1.20 - Shellsort best case
Just like insertion sort, the best case for shellsort is when the array is already ordered. This causes every element to
be compared only once in each iteration.
The time complexity in this case is O(n log n).
Good explanation: https://www.toptal.com/developers/sorting-algorithms/shell-... | [] | {
"number": "2.5.20",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Sampling for selection. Investigate the idea of using sampling to improve selection. Hint: Using the median may not always be helpful. | 2.1.23 - Deck sort
They separate all cards by suit, making the swaps in order to keep cards in the same suit next to each other (dividing the row in 4 areas).
Then, in each suit, they sort the cards by rank using selection sort.
In the second step (once suits are sorted), some of them move the unsorted smaller ranks n... | [] | {
"number": "2.5.23",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Stable priority queue. Develop a stable priority-queue implementation (which returns duplicate keys in the same order in which they were inserted). | 2.1.24 - Insertion sort with sentinel
For 100000 random doubles
Insertion Sort default is 1.1 times faster than Insertion Sort with a sentinel
Insertion Sort default is slightly faster than Insertion Sort with a sentinel. | [] | {
"number": "2.5.24",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Points in the plane. Write three static comparators for the Point2D data type of page 77, one that compares points by their x coordinate, one that compares them by their y coordinate, and one that compares them by their distance from the origin. Write two non-static comparators for the Point2D data type, one that compa... | 2.1.25 - Insertion sort without exchanges
For 100000 random doubles
Insertion Sort default is 0.7 times faster than Insertion Sort without exchanges
Insertion Sort without exchanges is faster than Insertion Sort default. | [] | {
"number": "2.5.25",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Simple polygon. Given N points in the plane, draw a simple polygon with N points as vertices. Hint: Find the point p with the smallest y coordinate, breaking ties with the smallest x coordinate. Connect the points in increasing order of the polar angle they make with p. | 2.1.26 - Primitive types
For 100000 random doubles
Insertion Sort with primitives is 2.0 times faster than Insertion Sort with objects
Insertion Sort with primitives is faster than Insertion Sor with objects. | [] | {
"number": "2.5.26",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Sorting parallel arrays. When sorting parallel arrays, it is useful to have a version of a sorting routine that returns a permutation, say index[], of the indices in sorted order. Add a method indirectSort() to Insertion that takes an array of Comparable objects a[] as argument, but instead of rearranging the entries o... | 2.1.27 - Shellsort is subquadratic
For a total of 11 experiments with 128, 256, ..., 262,144 values
Shell Sort is 343.0 times faster than Selection Sort
Shell Sort is 338.8 times faster than Insertion Sort | [] | {
"number": "2.5.27",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Sort files by name. Write a program FileSorter that takes the name of a directory as a command-line argument and prints out all of the files in the current directory, sorted by file name. Hint: Use the File data type. | 2.1.28 - Equal keys
From the book we know that:
Proposition A. Selection sort uses ~(N^2 / 2) compares and N exchanges to sort an array of length N.
Proposition B. Insertion sort uses ~(N^2 / 4) compares and ~(N^2 / 4) exchanges to sort a randomly ordered array of
length N with distinct keys, on the average. The worst... | [] | {
"number": "2.5.28",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Sort files by size and date of last modification. Write comparators for the type File to order by increasing/decreasing order of file size, ascending/descending order of file name, and ascending/descending order of last modification date. Use these comparators in a program LS that takes a command-line argument and list... | 2.1.29 - Shellsort increments
Sedgewicks's sequence is faster than the 3N + 1 sequence.
Experiments:
For an array of size 32768:
3N + 1 sequence: 0.0
Sedgewicks's sequence: 0.0
For an array of size 65536:
3N + 1 sequence: 0.0
Sedgewicks's sequence: 0.0
For an array of size 131072:
3N + 1 sequence: 0.1
Sedg... | [] | {
"number": "2.5.29",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Boerner’s theorem. True or false: If you sort each column of a matrix, then sort each row, the columns are still sorted. Justify your answer. | 2.1.30 - Geometric increments
Best 1 tValue: 2.10
Best 1 sequence:
1 2 4 9 19 40 85 180 378 794 1667 3502 7355 15447 32439 68122 143056 300419 630880
Best 2 tValue: 2.40
Best 2 sequence:
1 2 5 13 33 79 191 458 1100 2641 6340 15216 36520 87648 210357 504857
Best 3 tValue: 2.20
Best 3 sequence:
1 2 4 10 23 51 113 249 ... | [] | {
"number": "2.5.30",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 2,
"chapter_title": "Sorting",
"section": 2.5,
"section_title": "Applications",
"type": "Creative Problem"
} |
Give the number of calls to put() and get() issued by FrequencyCounter, as a function of the number W of words and the number D of distinct words in the input. | 3.1.6
The put() method will be called once for every word.
The get() method will be called once for every word, except on the first time a word in being inserted in the symbol table.
Calls to put() = W
Calls to get() = W - D
Thanks to faame (https://github.com/faame) for suggesting a better answer.
https://github.co... | [] | {
"number": "3.1.6",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
What is the average number of distinct keys that FrequencyCounter will find among N random nonnegative integers less than 1,000, for N=10, 10^2, 10^3, 10^4, 10^5, and 10^6? | 3.1.7
Number of distinct keys among 10 random nonnegative integers less than 1000: 10
Number of distinct keys among 100 random nonnegative integers less than 1000: 97
Number of distinct keys among 1000 random nonnegative integers less than 1000: 635
Number of distinct keys among 10000 random nonnegative integers less ... | [] | {
"number": "3.1.7",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
What is the most frequently used word of ten letters or more in Tale of Two Cities? | 3.1.8
Most frequently used word of ten letters or more in Tale of Two Cities: Monseigneur Frequency: 47 | [] | {
"number": "3.1.8",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Add code to FrequencyCounter to keep track of the last call to put(). Print the last word inserted and the number of words that were processed in the input stream prior to this insertion. Run your program for tale.txt with length cutoffs 1, 8, and 10. | 3.1.9
Length cutoff Last word inserted Words processed
1 known." 135937
8 faltering 135905
10 disfigurement 135890 | [] | {
"number": "3.1.9",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Give a trace of the process of inserting the keys E A S Y Q U E S T I O N into an initially empty table using SequentialSearchST. How many compares are involved? | 3.1.10
key value first
E 0 E0 0 compares
A 1 A1 E0 1 compare
S 2 S2 A1 E0 2 compares
Y 3 Y3 S2 A1 E0 3 compares
Q 4 Q4 Y3 S... | [] | {
"number": "3.1.10",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Give a trace of the process of inserting the keys E A S Y Q U E S T I O N into an initially empty table using BinarySearchST. How many compares are involved? | 3.1.11
keys[] vals[]
key value 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9
E 0 E 1 0 0 compares
A 1 A E 2 1 0 2 compares
S 2 A... | [] | {
"number": "3.1.11",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Which of the symbol-table implementations in this section would you use for an application that does 10^3 put() operations and 10^6 get() operations, randomly intermixed? Justify your answer. | 3.1.13
For an application that does 10^3 put() operations and 10^6 get() operations I would use a binary search symbol table implementation.
The application does a lot more get() than put() operations and a binary search symbol table implementation has a O(log(n)) runtime complexity for the get() operation, which is b... | [] | {
"number": "3.1.13",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Which of the symbol-table implementations in this section would you use for an application that does 10^6 put() operations and 10^3 get() operations, randomly intermixed? Justify your answer. | 3.1.14
For an application that does 10^6 put() operations and 10^3 get() operations I would use a sequential search symbol table implementation.
The worst-case runtime cost of the put() operation for a binary search symbol table is 2N while for a sequential search symbol table it is N.
For the get() operation, the wo... | [] | {
"number": "3.1.14",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Assume that searches are 1,000 times more frequent than insertions for a BinarySearchST client. Estimate the percentage of the total time that is devoted to insertions, when the number of searches is 10^3, 10^6, and 10^9. | 3.1.15
Searches Percentage of total time spent on insertions
1000 0.00%
1000000 6.10%
1000000000 96.26%
The average insertion cost is N, so the total insertion cost for N keys is ... | [] | {
"number": "3.1.15",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Prove that the rank() method in BinarySearchST is correct. | 3.1.18
The rank() method in BinarySearchST always starts the search in the middle of the array if it has an odd number of elements.
If the array has an even number of elements, the rank() method starts the search on the left of the two middle elements.
After comparing the middle element with the search key, if it is s... | [] | {
"number": "3.1.18",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Complete the proof of Proposition B (show that it holds for all values of N). Hint: Start by showing that C(N) is monotonic: C(N) <= C(N+1) for all N > 0. | 3.1.20
Proposition B. Binary search in an ordered array with N keys uses no more than lg N + 1 compares for a search (successful or unsuccessful).
Proof: Let C(N) be the number of compares to search for a key in a symbol table of size N.
We have C(0) = 0, C(1) = 1, and for N > 0 we can write a recurrence relationshi... | [] | {
"number": "3.1.20",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Exercise"
} |
Memory usage. Compare the memory usage of BinarySearchST with that of SequentialSearchST for N key-value pairs, under the assumptions described in Section 1.4. Do not count the memory for the keys and values themselves, but do count references to them. For BinarySearchST, assume that array resizing is used, so that the... | 3.1.21 - Memory usage
* BinarySearchST
object overhead -> 16 bytes
Key[] reference (keys) -> 8 bytes
Value[] reference (values) -> 8 bytes
int value (size) -> 4 bytes
padding -> 4 bytes
Key[]
object overhead -> 16 bytes
int value (length) -> 4 bytes
padding -> 4 bytes
N Key re... | [] | {
"number": "3.1.21",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Creative Problem"
} |
Analysis of binary search. Prove that the maximum number of compares used for a binary search in a table of size N is precisely the number of bits in the binary representation of N, because the operation of shifting 1 bit to the right converts the binary representation of N into the binary representation of floor(N/2). | 3.1.23 - Analysis of binary search
As the book and exercise 3.1.20 have proven, the maximum number of compares used for a binary search in a table of size N is lg N + 1.
A number N has exactly lg N + 1 bits. This is because shifting 1 bit to the right reduces the number by half (rounded down).
For example:
N Bit ... | [] | {
"number": "3.1.23",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Creative Problem"
} |
Small tables. Suppose that a BinarySearchST client has S search operations and N distinct keys. Give the order of growth of S such that the cost of building the table is the same as the cost of all the searches. | 3.1.27 - Small tables
Building the binary search symbol table requires N calls to put().
Every put() operation makes a call to rank() and does a search, an operation with order of growth O(lg N).
Assuming that we can choose the order of the keys to insert, we can create the table in a sorted order, starting with the s... | [] | {
"number": "3.1.27",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.1,
"section_title": "Symbol Tables",
"type": "Creative Problem"
} |
Add to BST a method height() that computes the height of the tree. Develop two implementations: a recursive method (which takes linear time and space proportional to the height), and a method like size() that adds a field to each node in the tree (and takes linear space and constant time per query). | 3.1.6
The put() method will be called once for every word.
The get() method will be called once for every word, except on the first time a word in being inserted in the symbol table.
Calls to put() = W
Calls to get() = W - D
Thanks to faame (https://github.com/faame) for suggesting a better answer.
https://github.co... | [] | {
"number": "3.2.6",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Add to BST a recursive method avgCompares() that computes the average number of compares required by a random search hit in a given BST (the internal path length of the tree divided by its size, plus one). Develop two implementations: a recursive method (which takes linear time and space proportional to the height), an... | 3.1.7
Number of distinct keys among 10 random nonnegative integers less than 1000: 10
Number of distinct keys among 100 random nonnegative integers less than 1000: 97
Number of distinct keys among 1000 random nonnegative integers less than 1000: 635
Number of distinct keys among 10000 random nonnegative integers less ... | [] | {
"number": "3.2.7",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Write a static method optCompares() that takes an integer argument N and computes the number of compares required by a random search hit in an optimal (perfectly balanced) BST, where all the null links are on the same level if the number of links is a power of 2 or on one of two levels otherwise. | 3.1.8
Most frequently used word of ten letters or more in Tale of Two Cities: Monseigneur Frequency: 47 | [] | {
"number": "3.2.8",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Draw all the different BST shapes that can result when N keys are inserted into an initially empty tree, for N = 2, 3, 4, 5, and 6. | 3.1.9
Length cutoff Last word inserted Words processed
1 known." 135937
8 faltering 135905
10 disfigurement 135890 | [] | {
"number": "3.2.9",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Write a test client TestBST.java for use in testing the implementations of min(), max(), floor(), ceiling(), select(), rank(), delete(), deleteMin(), deleteMax(), and keys() that are given in the text. Start with the standard indexing client given on page 370. Add code to take additional command-line arguments, as appr... | 3.1.10
key value first
E 0 E0 0 compares
A 1 A1 E0 1 compare
S 2 S2 A1 E0 2 compares
Y 3 Y3 S2 A1 E0 3 compares
Q 4 Q4 Y3 S... | [] | {
"number": "3.2.10",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
How many binary tree shapes of N nodes are there with height N? How many different ways are there to insert N distinct keys into an initially empty BST that result in a tree of height N? (See Exercise 3.2.2.) | 3.1.11
keys[] vals[]
key value 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9
E 0 E 1 0 0 compares
A 1 A E 2 1 0 2 compares
S 2 A... | [] | {
"number": "3.2.11",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Give nonrecursive implementations of get() and put() for BST. | 3.1.13
For an application that does 10^3 put() operations and 10^6 get() operations I would use a binary search symbol table implementation.
The application does a lot more get() than put() operations and a binary search symbol table implementation has a O(log(n)) runtime complexity for the get() operation, which is b... | [] | {
"number": "3.2.13",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Give nonrecursive implementations of min(), max(), floor(), ceiling(), rank(), and select(). | 3.1.14
For an application that does 10^6 put() operations and 10^3 get() operations I would use a sequential search symbol table implementation.
The worst-case runtime cost of the put() operation for a binary search symbol table is 2N while for a sequential search symbol table it is N.
For the get() operation, the wo... | [] | {
"number": "3.2.14",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Give the sequences of nodes examined when the methods in BST are used to compute each of the following quantities for the tree drawn at right.
a. floor("Q")
b. select(5)
c. ceiling("Q")
d. rank("J")
e. size("D", "T")
f. keys("D", "T") | 3.1.15
Searches Percentage of total time spent on insertions
1000 0.00%
1000000 6.10%
1000000000 96.26%
The average insertion cost is N, so the total insertion cost for N keys is ... | [] | {
"number": "3.2.15",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Draw the sequence of BSTs that results when you delete the keys from the tree of Exercise 3.2.1, one by one, in alphabetical order. | 3.1.18
The rank() method in BinarySearchST always starts the search in the middle of the array if it has an odd number of elements.
If the array has an even number of elements, the rank() method starts the search on the left of the two middle elements.
After comparing the middle element with the search key, if it is s... | [] | {
"number": "3.2.18",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Prove that the running time of the two-argument keys() in a BST with N nodes is at most proportional to the tree height plus the number of keys in the range. | 3.1.20
Proposition B. Binary search in an ordered array with N keys uses no more than lg N + 1 compares for a search (successful or unsuccessful).
Proof: Let C(N) be the number of compares to search for a key in a symbol table of size N.
We have C(0) = 0, C(1) = 1, and for N > 0 we can write a recurrence relationshi... | [] | {
"number": "3.2.20",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Add a BST method randomKey() that returns a random key from the symbol table in time proportional to the tree height, in the worst case. | 3.1.21 - Memory usage
* BinarySearchST
object overhead -> 16 bytes
Key[] reference (keys) -> 8 bytes
Value[] reference (values) -> 8 bytes
int value (size) -> 4 bytes
padding -> 4 bytes
Key[]
object overhead -> 16 bytes
int value (length) -> 4 bytes
padding -> 4 bytes
N Key re... | [] | {
"number": "3.2.21",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Is delete() commutative? (Does deleting x, then y give the same result as deleting y, then x?) | 3.1.23 - Analysis of binary search
As the book and exercise 3.1.20 have proven, the maximum number of compares used for a binary search in a table of size N is lg N + 1.
A number N has exactly lg N + 1 bits. This is because shifting 1 bit to the right reduces the number by half (rounded down).
For example:
N Bit ... | [] | {
"number": "3.2.23",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Exercise"
} |
Memory usage. Compare the memory usage of BST with the memory usage of BinarySearchST and SequentialSearchST for N key-value pairs, under the assumptions described in Section 1.4 (see Exercise 3.1.21). Do not count the memory for the keys and values themselves, but do count references to them. Then draw a diagram that ... | 3.1.27 - Small tables
Building the binary search symbol table requires N calls to put().
Every put() operation makes a call to rank() and does a search, an operation with order of growth O(lg N).
Assuming that we can choose the order of the keys to insert, we can create the table in a sorted order, starting with the s... | [] | {
"number": "3.2.27",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Equal key check. Write a method hasNoDuplicates() that takes a Node as argument and returns true if there are no equal keys in the binary tree rooted at the argument node, false otherwise. Assume that the test of the previous exercise has passed. | 3.1.31 - Performance driver
Number of Experiments | Array Size | AVG Time Taken
1 100000 12.32
2 100000 12.13
3 100000 12.15
4 100000 14.73
5... | [] | {
"number": "3.2.31",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Certification. Write a method isBST() that takes a Node as argument and returns true if the argument node is the root of a binary search tree, false otherwise. Hint: This task is also more difficult than it might seem, because the order in which you call the methods in the previous three exercises is important. | 3.1.32 - Exercise driver
Input Type | Array Size | Running Time
Ordered 100000 6.8
Ordered 200000 37.7
Ordered 400000 142.9
Reverse Ordered 100000 24.1
Reve... | [] | {
"number": "3.2.32",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Select/rank check. Write a method that checks, for all i from 0 to size()-1, whether i is equal to rank(select(i)) and, for all keys in the BST, whether key is equal to select(rank(key)). | 3.1.33 - Driver for self-organizing search
Array Size | Self-Organizing Search Time | Default Binary Search Time
1000 0.01 0.01
10000 0.05 0.01
100000 4.46 ... | [] | {
"number": "3.2.33",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Threading. Your goal is to support an extended API ThreadedST that supports the following additional operations in constant time:
Key next(Key key) key that follows key (null if key is the maximum)
Key prev(Key key) key that precedes key (null if key is the minimum)
To do so, add fields pred and succ to Node that conta... | 3.1.34 - Zipf's law
Array Size | Move-to-Front Time | Optimal Arrangement Time
1000 0.04 0.01
10000 0.42 0.18
100000 33.46 6.18
1000000 4069.8... | [] | {
"number": "3.2.34",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Refined analysis. Refine the mathematical model to better explain the experimental results in the table given in the text. Specifically, show that the average number of compares for a successful search in a tree built from random keys approaches the limit 2 ln N + 2γ - 3 ≈ 1.39 lg N – 1.85 as N increases, where γ ≈ .57... | 3.1.35 - Performance validation I
The running time of FrequencyCounter is close to quadratic when it uses SequentialSearchST for its symbol table.
Number of Words | Running Time | Ratio | Complexity (N^X)
1000 0.01 1.00 0.00
2000 0.01 1.60 ... | [] | {
"number": "3.2.35",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Iterator. Is it possible to write a nonrecursive version of keys() that uses space proportional to the tree height (independent of the number of keys in the range)? | 3.1.36 - Performance validation II
The performance of BinarySearchST and SequentialSearchST for FrequencyCounter is even better than predicted by analysis because of the details of the FrequencyCounter application.
FrequencyCounter does not add all the words in the symbol table (only the words with a length higher th... | [] | {
"number": "3.2.36",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Level-order traversal. Write a method printLevel() that takes a Node as argument and prints the keys in the subtree rooted at that node in level order (in order of their distance from the root, with nodes on each level in order from left to right). Hint: Use a Queue. | 3.1.37 - Put/get ratio
For a symbol table with 10-bit int values the amount of time that BinarySearchST spent on put() was lower than the amount of time it spent on get().
However, for all the other scenarios (counting the frequency of 20 and 30-bit values and the frequency of the words in the book Tale of Two Cities)... | [] | {
"number": "3.2.37",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.2,
"section_title": "Binary Search Trees",
"type": "Creative Problem"
} |
Find the probability that each of the 2-3 trees in Exercise 3.3.5 is the result of the insertion of N random distinct keys into an initially empty tree. | 3.1.6
The put() method will be called once for every word.
The get() method will be called once for every word, except on the first time a word in being inserted in the symbol table.
Calls to put() = W
Calls to get() = W - D
Thanks to faame (https://github.com/faame) for suggesting a better answer.
https://github.co... | [] | {
"number": "3.3.6",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Draw diagrams like the one at the top of page 428 for the other five cases in the diagram at the bottom of that page. | 3.1.7
Number of distinct keys among 10 random nonnegative integers less than 1000: 10
Number of distinct keys among 100 random nonnegative integers less than 1000: 97
Number of distinct keys among 1000 random nonnegative integers less than 1000: 635
Number of distinct keys among 10000 random nonnegative integers less ... | [] | {
"number": "3.3.7",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Show all possible ways that one might represent a 4-node with three 2-nodes bound together with red links (not necessarily left-leaning). | 3.1.8
Most frequently used word of ten letters or more in Tale of Two Cities: Monseigneur Frequency: 47 | [] | {
"number": "3.3.8",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Which of the following are red-black BSTs? | 3.1.9
Length cutoff Last word inserted Words processed
1 known." 135937
8 faltering 135905
10 disfigurement 135890 | [] | {
"number": "3.3.9",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Draw the red-black BST that results when you insert items with the keys E A S Y Q U T I O N in that order into an initially empty tree. | 3.1.10
key value first
E 0 E0 0 compares
A 1 A1 E0 1 compare
S 2 S2 A1 E0 2 compares
Y 3 Y3 S2 A1 E0 3 compares
Q 4 Q4 Y3 S... | [] | {
"number": "3.3.10",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Draw the red-black BST that results when you insert items with the keys Y L P M X H C R A E S in that order into an initially empty tree. | 3.1.11
keys[] vals[]
key value 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9
E 0 E 1 0 0 compares
A 1 A E 2 1 0 2 compares
S 2 A... | [] | {
"number": "3.3.11",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
True or false: If you insert keys in increasing order into a red-black BST, the tree height is monotonically increasing. | 3.1.13
For an application that does 10^3 put() operations and 10^6 get() operations I would use a binary search symbol table implementation.
The application does a lot more get() than put() operations and a binary search symbol table implementation has a O(log(n)) runtime complexity for the get() operation, which is b... | [] | {
"number": "3.3.13",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Draw the red-black BST that results when you insert letters A through K in order into an initially empty tree, then describe what happens in general when trees are built by insertion of keys in ascending order (see also the figure in the text). | 3.1.14
For an application that does 10^6 put() operations and 10^3 get() operations I would use a sequential search symbol table implementation.
The worst-case runtime cost of the put() operation for a binary search symbol table is 2N while for a sequential search symbol table it is N.
For the get() operation, the wo... | [] | {
"number": "3.3.14",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Answer the previous two questions for the case when the keys are inserted in descending order. | 3.1.15
Searches Percentage of total time spent on insertions
1000 0.00%
1000000 6.10%
1000000000 96.26%
The average insertion cost is N, so the total insertion cost for N keys is ... | [] | {
"number": "3.3.15",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Draw all the structurally different red-black BSTs with N keys, for N from 2 up to 10 (see Exercise 3.3.5). | 3.1.18
The rank() method in BinarySearchST always starts the search in the middle of the array if it has an odd number of elements.
If the array has an even number of elements, the rank() method starts the search on the left of the two middle elements.
After comparing the middle element with the search key, if it is s... | [] | {
"number": "3.3.18",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Compute the internal path length in a perfectly balanced BST of N nodes, when N is a power of 2 minus 1. | 3.1.20
Proposition B. Binary search in an ordered array with N keys uses no more than lg N + 1 compares for a search (successful or unsuccessful).
Proof: Let C(N) be the number of compares to search for a key in a symbol table of size N.
We have C(0) = 0, C(1) = 1, and for N > 0 we can write a recurrence relationshi... | [] | {
"number": "3.3.20",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
Create a test client TestRB.java, based on your solution to Exercise 3.2.10. | 3.1.21 - Memory usage
* BinarySearchST
object overhead -> 16 bytes
Key[] reference (keys) -> 8 bytes
Value[] reference (values) -> 8 bytes
int value (size) -> 4 bytes
padding -> 4 bytes
Key[]
object overhead -> 16 bytes
int value (length) -> 4 bytes
padding -> 4 bytes
N Key re... | [] | {
"number": "3.3.21",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Exercise"
} |
2-3 trees without balance restriction. Develop an implementation of the basic symbol-table API that uses 2-3 trees that are not necessarily balanced as the underlying data structure. Allow 3-nodes to lean either way. Hook the new node onto the bottom with a black link when inserting into a 3-node at the bottom. Run exp... | 3.1.23 - Analysis of binary search
As the book and exercise 3.1.20 have proven, the maximum number of compares used for a binary search in a table of size N is lg N + 1.
A number N has exactly lg N + 1 bits. This is because shifting 1 bit to the right reduces the number by half (rounded down).
For example:
N Bit ... | [] | {
"number": "3.3.23",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Allow right-leaning red links. Develop a modified version of your solution to Exercise 3.3.25 that allows right-leaning red links in the tree. | 3.1.27 - Small tables
Building the binary search symbol table requires N calls to put().
Every put() operation makes a call to rank() and does a search, an operation with order of growth O(lg N).
Assuming that we can choose the order of the keys to insert, we can create the table in a sorted order, starting with the s... | [] | {
"number": "3.3.27",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Tree drawing. Add a method draw() to RedBlackBST that draws red-black BST figures in the style of the text (see Exercise 3.2.38). | 3.1.31 - Performance driver
Number of Experiments | Array Size | AVG Time Taken
1 100000 12.32
2 100000 12.13
3 100000 12.15
4 100000 14.73
5... | [] | {
"number": "3.3.31",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
AVL trees. An AVL tree is a BST where the height of every node and that of its sibling differ by at most 1. (The oldest balanced tree algorithms are based on using rotations to maintain height balance in AVL trees.) Show that coloring red links that go from nodes of even height to nodes of odd height in an AVL tree giv... | 3.1.32 - Exercise driver
Input Type | Array Size | Running Time
Ordered 100000 6.8
Ordered 200000 37.7
Ordered 400000 142.9
Reverse Ordered 100000 24.1
Reve... | [] | {
"number": "3.3.32",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Certification. Add to RedBlackBST a method is23() to check that no node is connected to two red links and that there are no right-leaning red links and a method isBalanced() to check that all paths from the root to a null link have the same number of black links. Combine these methods with code from isBST() in Exercise... | 3.1.33 - Driver for self-organizing search
Array Size | Self-Organizing Search Time | Default Binary Search Time
1000 0.01 0.01
10000 0.05 0.01
100000 4.46 ... | [] | {
"number": "3.3.33",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
All 2-3 trees. Write code to generate all structurally different 2-3 trees of height 2, 3, and 4. There are 2, 7, and 122 such trees, respectively. (Hint: Use a symbol table.) | 3.1.34 - Zipf's law
Array Size | Move-to-Front Time | Optimal Arrangement Time
1000 0.04 0.01
10000 0.42 0.18
100000 33.46 6.18
1000000 4069.8... | [] | {
"number": "3.3.34",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
2-3 trees. Write a program TwoThreeST.java that uses two node types to implement 2-3 search trees directly. | 3.1.35 - Performance validation I
The running time of FrequencyCounter is close to quadratic when it uses SequentialSearchST for its symbol table.
Number of Words | Running Time | Ratio | Complexity (N^X)
1000 0.01 1.00 0.00
2000 0.01 1.60 ... | [] | {
"number": "3.3.35",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
2-3-4-5-6-7-8 trees. Describe algorithms for search and insertion in balanced 2-3-4-5-6-7-8 search trees. | 3.1.36 - Performance validation II
The performance of BinarySearchST and SequentialSearchST for FrequencyCounter is even better than predicted by analysis because of the details of the FrequencyCounter application.
FrequencyCounter does not add all the words in the symbol table (only the words with a length higher th... | [] | {
"number": "3.3.36",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Memoryless. Show that red-black BSTs are not memoryless: for example, if you insert a key that is smaller than all the keys in the tree and then immediately delete the minimum, you may get a different tree. | 3.1.37 - Put/get ratio
For a symbol table with 10-bit int values the amount of time that BinarySearchST spent on put() was lower than the amount of time it spent on get().
However, for all the other scenarios (counting the frequency of 20 and 30-bit values and the frequency of the words in the book Tale of Two Cities)... | [] | {
"number": "3.3.37",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Delete the minimum. Implement the deleteMin() operation for red-black BSTs by maintaining the correspondence with the transformations given in the text for moving down the left spine of the tree while maintaining the invariant that the current node is not a 2-node. | 3.1.39 - Actual timings
As expected, BinarySearchST total running time is much smaller than SequentialSearchST total running time.
BinarySearchST total running time for all the calls to get() and put() was 204 milliseconds while SequentialSearchST total running time was 2237 milliseconds. | [] | {
"number": "3.3.39",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Delete the maximum. Implement the deleteMax() operation for red-black BSTs. Note that the transformations involved differ slightly from those in the previous exercise because red links are left-leaning. | 3.1.40 - Crossover to binary search
Analysis:
Considering that sequential search has a complexity of O(n) and binary search has a complexity of O(lg (n)):
N = 58: While sequential search does 58 operations (in the worst case), binary search does ~5.8 operations (in the worst case).
Binary search is ~10 times faster t... | [] | {
"number": "3.3.40",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Delete. Implement the delete() operation for red-black BSTs, combining the methods of the previous two exercises with the delete() operation for BSTs. | 3.1.41 - Crossover to interpolation search
Analysis:
Considering that binary search has a complexity of O(lg (n)) and interpolation search has a complexity of O(lg(lg (n))) when the elements are uniformly distributed (and it is O(n) otherwise).
Assuming an array with elements uniformly distributed:
N = 1: Both binar... | [] | {
"number": "3.3.41",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.3,
"section_title": "Balanced Search Trees",
"type": "Creative Problem"
} |
Suppose that keys are t-bit integers. For a modular hash function with prime M, prove that each key bit has the property that there exist two keys differing only in that bit that have different hash values. | 3.1.6
The put() method will be called once for every word.
The get() method will be called once for every word, except on the first time a word in being inserted in the symbol table.
Calls to put() = W
Calls to get() = W - D
Thanks to faame (https://github.com/faame) for suggesting a better answer.
https://github.co... | [] | {
"number": "3.4.6",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Consider the idea of implementing modular hashing for integer keys with the code (a * k) % M, where a is an arbitrary fixed prime. Does this change mix up the bits sufficiently well that you can use nonprime M? | 3.1.7
Number of distinct keys among 10 random nonnegative integers less than 1000: 10
Number of distinct keys among 100 random nonnegative integers less than 1000: 97
Number of distinct keys among 1000 random nonnegative integers less than 1000: 635
Number of distinct keys among 10000 random nonnegative integers less ... | [] | {
"number": "3.4.7",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
How many empty lists do you expect to see when you insert N keys into a hash table with SeparateChainingHashST, for N=10, 10^2, 10^3, 10^4, 10^5, and 10^6? Hint: See Exercise 2.5.31. | 3.1.8
Most frequently used word of ten letters or more in Tale of Two Cities: Monseigneur Frequency: 47 | [] | {
"number": "3.4.8",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Implement an eager delete() method for SeparateChainingHashST. | 3.1.9
Length cutoff Last word inserted Words processed
1 known." 135937
8 faltering 135905
10 disfigurement 135890 | [] | {
"number": "3.4.9",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Insert the keys E A S Y Q U T I O N in that order into an initially empty table of size M =16 using linear probing. Use the hash function 11 k % M to transform the kth letter of the alphabet into a table index. Redo this exercise for M = 10. | 3.1.10
key value first
E 0 E0 0 compares
A 1 A1 E0 1 compare
S 2 S2 A1 E0 2 compares
Y 3 Y3 S2 A1 E0 3 compares
Q 4 Q4 Y3 S... | [] | {
"number": "3.4.10",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Give the contents of a linear-probing hash table that results when you insert the keys E A S Y Q U T I O N in that order into an initially empty table of initial size M = 4 that is expanded with doubling whenever half full. Use the hash function 11 k % M to transform the kth letter of the alphabet into a table index. | 3.1.11
keys[] vals[]
key value 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9
E 0 E 1 0 0 compares
A 1 A E 2 1 0 2 compares
S 2 A... | [] | {
"number": "3.4.11",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Which of the following scenarios leads to expected linear running time for a random search hit in a linear-probing hash table?
a. All keys hash to the same index.
b. All keys hash to different indices.
c. All keys hash to an even-numbered index.
d. All keys hash to different even-numbered indices. | 3.1.13
For an application that does 10^3 put() operations and 10^6 get() operations I would use a binary search symbol table implementation.
The application does a lot more get() than put() operations and a binary search symbol table implementation has a O(log(n)) runtime complexity for the get() operation, which is b... | [] | {
"number": "3.4.13",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Answer the previous question for search miss, assuming the search key is equally likely to hash to each table position. | 3.1.14
For an application that does 10^6 put() operations and 10^3 get() operations I would use a sequential search symbol table implementation.
The worst-case runtime cost of the put() operation for a binary search symbol table is 2N while for a sequential search symbol table it is N.
For the get() operation, the wo... | [] | {
"number": "3.4.14",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
How many compares could it take, in the worst case, to insert N keys into an initially empty table, using linear probing with array resizing? | 3.1.15
Searches Percentage of total time spent on insertions
1000 0.00%
1000000 6.10%
1000000000 96.26%
The average insertion cost is N, so the total insertion cost for N keys is ... | [] | {
"number": "3.4.15",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Add a constructor to SeparateChainingHashST that gives the client the ability to specify the average number of probes to be tolerated for searches. Use array resizing to keep the average list size less than the specified value, and use the technique described on page 478 to ensure that the modulus for hash() is prime. | 3.1.18
The rank() method in BinarySearchST always starts the search in the middle of the array if it has an odd number of elements.
If the array has an even number of elements, the rank() method starts the search on the left of the two middle elements.
After comparing the middle element with the search key, if it is s... | [] | {
"number": "3.4.18",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Add a method to LinearProbingHashST that computes the average cost of a search hit in the table, assuming that each key in the table is equally likely to be sought. | 3.1.20
Proposition B. Binary search in an ordered array with N keys uses no more than lg N + 1 compares for a search (successful or unsuccessful).
Proof: Let C(N) be the number of compares to search for a key in a symbol table of size N.
We have C(0) = 0, C(1) = 1, and for N > 0 we can write a recurrence relationshi... | [] | {
"number": "3.4.20",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Add a method to LinearProbingHashST that computes the average cost of a search miss in the table, assuming a random hash function. Note: You do not have to compute any hash functions to solve this problem. | 3.1.21 - Memory usage
* BinarySearchST
object overhead -> 16 bytes
Key[] reference (keys) -> 8 bytes
Value[] reference (values) -> 8 bytes
int value (size) -> 4 bytes
padding -> 4 bytes
Key[]
object overhead -> 16 bytes
int value (length) -> 4 bytes
padding -> 4 bytes
N Key re... | [] | {
"number": "3.4.21",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Consider modular hashing for string keys with R = 256 and M = 255. Show that this is a bad choice because any permutation of letters within a string hashes to the same value. | 3.1.23 - Analysis of binary search
As the book and exercise 3.1.20 have proven, the maximum number of compares used for a binary search in a table of size N is lg N + 1.
A number N has exactly lg N + 1 bits. This is because shifting 1 bit to the right reduces the number by half (rounded down).
For example:
N Bit ... | [] | {
"number": "3.4.23",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Exercise"
} |
Double probing. Modify SeparateChainingHashST to use a second hash function and pick the shorter of the two lists. Give a trace of the process of inserting the keys E A S Y Q U T I O N in that order into an initially empty table of size M =3 using the function 11 k % M (for the kth letter) as the first hash function an... | 3.1.27 - Small tables
Building the binary search symbol table requires N calls to put().
Every put() operation makes a call to rank() and does a search, an operation with order of growth O(lg N).
Assuming that we can choose the order of the keys to insert, we can create the table in a sorted order, starting with the s... | [] | {
"number": "3.4.27",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Creative Problem"
} |
Cuckoo hashing. Develop a symbol-table implementation that maintains two hash tables and two hash functions. Any given key is in one of the tables, but not both. When inserting a new key, hash to one of the tables; if the table position is occupied, replace that key with the new key and hash the old key into the other ... | 3.1.31 - Performance driver
Number of Experiments | Array Size | AVG Time Taken
1 100000 12.32
2 100000 12.13
3 100000 12.15
4 100000 14.73
5... | [] | {
"number": "3.4.31",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Creative Problem"
} |
Hash attack. Find 2^N strings, each of length 2^N, that have the same hashCode() value, supposing that the hashCode() implementation for String is the following:
public int hashCode() {
int hash = 0;
for (int i = 0; i < length(); i ++)
hash = (hash * 31) + charAt(i);
return hash;
}
Strong hint: Aa and BB have t... | 3.1.32 - Exercise driver
Input Type | Array Size | Running Time
Ordered 100000 6.8
Ordered 200000 37.7
Ordered 400000 142.9
Reverse Ordered 100000 24.1
Reve... | [] | {
"number": "3.4.32",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Creative Problem"
} |
Bad hash function. Consider the following hashCode() implementation for String, which was used in early versions of Java:
public int hashCode() {
int hash = 0;
int skip = Math.max(1, length()/8);
for (int i = 0; i < length(); i += skip)
hash = (hash * 37) + charAt(i);
return hash;
}
Explain why you think the d... | 3.1.33 - Driver for self-organizing search
Array Size | Self-Organizing Search Time | Default Binary Search Time
1000 0.01 0.01
10000 0.05 0.01
100000 4.46 ... | [] | {
"number": "3.4.33",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.4,
"section_title": "Hash Tables",
"type": "Creative Problem"
} |
Develop classes HashSETint and HashSETdouble for maintaining sets of keys of primitive int and double types, respectively. (Eliminate code involving values in your solution to Exercise 3.5.4.) | 3.1.6
The put() method will be called once for every word.
The get() method will be called once for every word, except on the first time a word in being inserted in the symbol table.
Calls to put() = W
Calls to get() = W - D
Thanks to faame (https://github.com/faame) for suggesting a better answer.
https://github.co... | [] | {
"number": "3.5.6",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Develop classes SETint and SETdouble for maintaining ordered sets of keys of primitive int and double types, respectively. (Eliminate code involving values in your solution to Exercise 3.5.5.) | 3.1.7
Number of distinct keys among 10 random nonnegative integers less than 1000: 10
Number of distinct keys among 100 random nonnegative integers less than 1000: 97
Number of distinct keys among 1000 random nonnegative integers less than 1000: 635
Number of distinct keys among 10000 random nonnegative integers less ... | [] | {
"number": "3.5.7",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Modify LinearProbingHashST to keep duplicate keys in the table. Return any value associated with the given key for get(), and remove all items in the table that have keys equal to the given key for delete(). | 3.1.8
Most frequently used word of ten letters or more in Tale of Two Cities: Monseigneur Frequency: 47 | [] | {
"number": "3.5.8",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Modify BST to keep duplicate keys in the tree. Return any value associated with the given key for get(), and remove all nodes in the tree that have keys equal to the given key for delete(). | 3.1.9
Length cutoff Last word inserted Words processed
1 known." 135937
8 faltering 135905
10 disfigurement 135890 | [] | {
"number": "3.5.9",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Modify RedBlackBST to keep duplicate keys in the tree. Return any value associated with the given key for get(), and remove all nodes in the tree that have keys equal to the given key for delete(). | 3.1.10
key value first
E 0 E0 0 compares
A 1 A1 E0 1 compare
S 2 S2 A1 E0 2 compares
Y 3 Y3 S2 A1 E0 3 compares
Q 4 Q4 Y3 S... | [] | {
"number": "3.5.10",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Develop a MultiSET class that is like SET, but allows equal keys and thus implements a mathematical multiset. | 3.1.11
keys[] vals[]
key value 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9
E 0 E 1 0 0 compares
A 1 A E 2 1 0 2 compares
S 2 A... | [] | {
"number": "3.5.11",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Modify LookupCSV to make a program RangeLookupCSV that takes two key values from the standard input and prints all key-value pairs in the .csv file such that the key falls within the range specified. | 3.1.13
For an application that does 10^3 put() operations and 10^6 get() operations I would use a binary search symbol table implementation.
The application does a lot more get() than put() operations and a binary search symbol table implementation has a O(log(n)) runtime complexity for the get() operation, which is b... | [] | {
"number": "3.5.13",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Develop and test a static method invert() that takes as argument an ST<String, Bag<String>> and produces as return value the inverse of the given symbol table (a symbol table of the same type). | 3.1.14
For an application that does 10^6 put() operations and 10^3 get() operations I would use a sequential search symbol table implementation.
The worst-case runtime cost of the put() operation for a binary search symbol table is 2N while for a sequential search symbol table it is N.
For the get() operation, the wo... | [] | {
"number": "3.5.14",
"code_execution": false,
"url": null,
"params": null,
"dependencies": null,
"chapter": 3,
"chapter_title": "Searching",
"section": 3.5,
"section_title": "Applications",
"type": "Exercise"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.