New Semester
Started
Get
50% OFF
Study Help!
--h --m --s
Claim Now
Question Answers
Textbooks
Find textbooks, questions and answers
Oops, something went wrong!
Change your search query and then try again
S
Books
FREE
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Tutors
Online Tutors
Find a Tutor
Hire a Tutor
Become a Tutor
AI Tutor
AI Study Planner
NEW
Sell Books
Search
Search
Sign In
Register
study help
computer science
introduction to algorithms
Data Structures and Algorithms in Java 6th edition Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser - Solutions
To implement the preorder method of the AbstractTree class, we relied on the convenience of creating a snapshot. Reimplement a preorder method that creates a lazy iterator. (See Section 7.4.2 for discussion of iterators.) Section 7.4.2 ? /- /** * A (nonstatic) inner class. Note well that each
Describe how to clone a LinkedBinaryTree instance representing a (not necessarily proper) binary tree, with use of the addLeft and addRight methods.
Describe how to clone a LinkedBinaryTree instance representing a proper binary tree, with use of the attach method.
We can simplify parts of our LinkedBinaryTree implementation if we make use of of a single sentinel node, such that the sentinel is the parent of the real root of the tree, and the root is referenced as the left child of the sentinel. Furthermore, the sentinel will take the place of null as the
Add support in LinkedBinaryTree for a method, swap(p, q), that has the effect of restructuring the tree so that the node referenced by p takes the place of the node referenced by q, and vice versa. Make sure to properly handle the case when the nodes are adjacent.
If we exclude isomorphic trees (see Exercise C-8.33), exactly how many proper binary trees exist with exactly 4 leaves?Exercise C-8.33Two ordered trees T′ and T′′ are said to be isomorphic if one of the following holds:• Both T′ and T′′ are empty.• Both T′ and T′′ consist of a
Show that there are more than 2n improper binary trees with n internal nodes such that no pair are isomorphic (see Exercise C-8.33).Exercise C-8.33Two ordered trees T′ and T′′ are said to be isomorphic if one of the following holds:• Both T′ and T′′ are empty.• Both T′ and T′′
Two ordered trees T′ and T′′ are said to be isomorphic if one of the following holds:• Both T′ and T′′ are empty.• Both T′ and T′′ consist of a single node• The roots of T′ and T′′ have the same number k ≥ 1 of subtrees, and the ith such subtree of T′ is isomorphic
Let T be a (not necessarily proper) binary tree with n nodes, and let D be the sum of the depths of all the external nodes of T. Show that if T has the minimum number of external nodes possible, then D is O(n) and if T has the maximum number of external nodes possible, then D is O(nlogn).
Define the internal path length, I(T), of a tree T to be the sum of the depths of all the internal positions in T. Likewise, define the external path length, E(T), of a tree T to be the sum of the depths of all the external positions in T. Show that if T is a proper binary tree with n positions,
The path length of a tree T is the sum of the depths of all positions in T. Describe a linear-time method for computing the path length of a tree T.
Describe an efficient algorithm for converting a fully balanced string of parentheses into an equivalent tree. The tree associated with such a string is defined recursively. The outermost pair of balanced parentheses is associated with the root and each substring inside this pair, defined by the
Describe a modification to parenthesize, from Code Fragment 8.26, that relies on the length( ) method for the String class to output the parenthetic representation of a tree with line breaks added to display the tree in a text window that is 80 characters wide. Fragment 8.26 1 /** Prints
Give the output of the method parenthesize(T, T.root( )), as described in Code Fragment 8.26, when T is the tree of Figure 8.6. Fragment 8.26 Figure 8.6 3 3 5 4
In what order are positions visited during a preorder traversal of the tree of Figure 8.6? + + 3 2 9 5 7 4 3.
Draw the binary tree representation of the following arithmetic expression: “(((5+2) ∗ (2−1))/((2+9)+((7−2)−1)) ∗8)”.
Find the value of the arithmetic expression associated with each subtree of the binary tree of Figure 8.6. + + 3 2 9 5 7 4 3.
Let T be an n-node binary tree that may be improper. Describe how to represent T by means of a proper binary tree T′ with O(n) nodes.
Describe an algorithm, relying only on the BinaryTree operations, that counts the number of leaves in a binary tree that are the left child of their respective parent.
Show a tree achieving the worst-case running time for algorithm depth.
The following questions refer to the tree of Figure 8.3.a. Which node is the root?b. What are the internal nodes?c. How many descendants does node cs016/ have?d. How many ancestors does node cs016/ have?e. What are the siblings of node homeworks/?f. Which nodes are in the subtree rooted at node
Write a simple text editor, which stores and displays a string of characters using the positional list ADT, together with a cursor object that highlights a position in the string. The editor must support the following operations:• Left: Move cursor left one character (do nothing if at
Implement a CardHand class that supports a person arranging a group of cards in his or her hand. The simulator should represent the sequence of cards using a single positional list ADT so that cards of the same suit are kept together. Implement this strategy by means of four “fingers” into the
Implement the FavoritesList class using an array list.
The mergeable heap ADT is an extension of the priority queue ADT consisting of operations insert(k, v), min( ), removeMin( ), and merge(h), where the merge(h) operations performs a union of the mergeable heap h with the present one, incorporating all entries into the current one while emptying h.
Prepare an implementation of splay trees that uses top-down splaying as described in Exercise C-11.52. Perform extensive experimental studies to compare its performance to the standard bottom-up splaying implemented in this chapter.Exercise C-11.52The standard splaying step requires two passes, one
Write a Java class that can take any red-black tree and convert it into its corresponding (2,4) tree and can take any (2,4) tree and convert it into its corresponding red-black tree.
Perform an experimental study to compare the speed of our AVL tree, splay tree, and red-black tree implementations for various sequences of operations.
Modify the TreeMap implementation to support location-aware entries. Provide methods firstEntry( ), lastEntry( ), findEntry(k), before(e), after(e), and remove(e), with all but the last of these returning an Entry instance, and the latter three accepting an Entry e as a parameter.
Reimplement the TreeMap class using null references in place of explicit sentinels for the leaves of a tree.
Describe a sequence of accesses to an n-node splay tree T, where n is odd, that results in T consisting of a single chain of nodes such that the path down T alternates between left children and right children.
Consider a variation of splay trees, called half-splay trees, where splaying a node at depth d stops as soon as the node reaches depth ⌊d/2⌋. Perform an amortized analysis of half-splay trees.
The standard splaying step requires two passes, one downward pass to find the node x to splay, followed by an upward pass to splay the node x. Describe a method for splaying and searching for x in one downward pass. Each substep now requires that you consider the next two nodes in the path down to
Prove that an n-entry multiway search tree has n+1 external nodes.
Let T be a red-black tree storing n entries, and let k be the key of an entry in T. Show how to construct from T, in O(logn) time, two red-black trees T′ and T′′, such that T′ contains all the keys of T less than k, and T′′ contains all the keys of T greater than k. This operation
Draw a splay tree, T1, together with the sequence of updates that produced it, and a red-black tree, T2, on the same set of ten entries, such that a preorder traversal of T1 would be the same as a preorder traversal of T2.
Describe a modification to the binary search-tree data structure that would support the following two index-based operations for a sorted map in O(h) time, where h is the height of the tree.atIndex(i): Return the position p of the entry at index i of a sorted map.indexOf(p): Return the index i of
If we maintain a reference to the position of the leftmost node of a binary search tree, then operation firstEntry can be performed in O(1) time. Describe how the implementation of the other map methods need to be modified to maintain a reference to the leftmost position.
In our AVL implementation, each node stores the height of its subtree, which is an arbitrarily large integer. The space usage for an AVL tree can be reduced by instead storing the balance factor of a node, which is defined as the height of its left subtree minus the height of its right subtree.
Show that at most one node in an AVL tree becomes temporarily unbalanced after the immediate deletion of a node as part of the standard remove map operation.
Show that the nodes that become temporarily unbalanced in an AVL tree during an insertion may be nonconsecutive on the path from the newly inserted node to the root.
Consider a sorted map that is implemented with a standard binary search tree T. Describe how to perform an operation removeSubMap(k1, k2) that removes all the entries whose keys fall within subMap(k1, k2), in worst-case time O(s+h), where s is the number of entries removed and h is the height of T.
Show that any n-node binary tree can be converted to any other n-node binary tree using O(n) rotations.
Explain why you would get the same output in an inorder listing of the entries in a binary search tree, T, independent of whether T is maintained to be an AVL tree, splay tree, or red-black tree.
Draw an example of a red-black tree that is not an AVL tree.
For the following statements about red-black trees, provide a justification for each true statement and a counterexample for each false one.a. A subtree of a red-black tree is itself a red-black tree.b. The sibling of an external node is either external or it is red.c. There is a unique (2,4) tree
The splay tree does not have good performance for the sorted map operations, because those methods lack calls to the rebalanceAccess hook. Reimplement TreeMap to include such calls.
Perform the following sequence of operations in an initially empty splay tree and draw the tree after each set of operations.a. Insert keys 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, in this order.b. Search for keys 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, in this order.c. Delete keys 0, 2, 4, 6, 8, 10, 12, 14,
What does a splay tree look like if its entries are accessed in increasing order by their keys?
If we insert the entries (1,A), (2,B), (3,C), (4,D), and (5,E), in this order, into an initially empty binary search tree, what will it look like?
Write a spell-checker class that stores a lexicon of words, W, in a set, and implements a method, check(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to check(s) returns a list containing only s, as it is assumed to be spelled
Describe how to use a skip list to implement the array list ADT, so that indexbased insertions and removals both run in O(logn) expected time.
Extend the previous project by providing a graphical animation of the skip-list operations. Visualize how entries move up the skip list during insertions and are linked out of the skip list during removals. Also, in a search operation, visualize the scan-forward and drop-down actions.
Design a Java class that implements the skip-list data structure. Use this class to create a complete implementation of the sorted map ADT.
Perform a comparative analysis as in the previous exercise, but for 10-digit telephone numbers instead of character strings.
Perform a comparative analysis that studies the collision rates for various hash codes for character strings, such as polynomial hash codes for different values of the parameter a. Use a hash table to determine collisions, but only count collisions where different strings map to the same hash code
An interesting strategy for hashing with separate chaining is known as powerof-two-choices hashing. Two independent hash functions are computed for each key, and a newly inserted element is placed into the choice of the two indicated buckets that currently has the fewest entries. Give a complete
An interesting strategy for hashing with open addressing is known as cuckoo hashing. Two independent hash functions are computed for each key, and an element is always stored in one of the two cells indicated by those hash functions. When a new element is inserted, if either of those two cells is
An inverted file is a critical data structure for implementing applications such an index of a book or a search engine. Given a document D, which can be viewed as an unordered, numbered list of words, an inverted file is an ordered list of words, L, such that, for each word w in L, we store the
Consider sets whose elements are integers in the range [0,N −1]. A popular scheme for representing a set A of this type is by means of a boolean array, B, where we say that x is in A if and only if B[x] = true. Since each cell of B can be represented with a single bit, B is sometimes referred to
Describe how to modify a skip-list representation so that index-based operations, such as retrieving the entry at index j, can be performed in O(logn) expected time.
Describe how to modify the skip-list data structure to support the method median(), which returns the position of the element in the “bottom” list S0 at index ⌊n/2⌋, Show that your implementation of this method runs in O(logn) expected time.
Show that the methods above(p) and before(p) are not actually needed to efficiently implement a map using a skip list. That is, we can implement insertions and deletions in a skip list using a strictly top-down, scan-forward approach, without ever using the above or before methods.
Give an O(logn)-time solution for the previous problem.Previous problemSuppose 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(log2 n)-time algorithm for finding the kth smallest key in the union of the keys from S and T
Repeat Exercise C-10.45 for the ChainHashMap class.Repeat Exercise C-10.45Develop a location-aware version of the UnsortedTableMap class so that an operation remove(e) for existing Entry e can be implemented in O(1) time.
Repeat the previous exercise for the ProbeHashMap class.Previous exerciseDevelop a location-aware version of the UnsortedTableMap class so that an operation remove(e) for existing Entry e can be implemented in O(1) time.
Develop a location-aware version of the UnsortedTableMap class so that an operation remove(e) for existing Entry e can be implemented in O(1) time.
The java.util.LinkedHashMap class is a subclass of the standard HashMap class that retains the expected O(1) performance for the primary map operations while guaranteeing that iterations report entries of the map according to first-in, firstout (FIFO) principle. That is, the key that has been in
Redesign our ProbeHashMap class so that the sequence of secondary probes for collision resolution can be more easily customized. Demonstrate your new design by providing separate concrete subclasses for linear probing and quadratic probing.
The quadratic probing strategy has a clustering problem related to the way it looks for open slots. Namely, when a collision occurs at bucket h(k), it checks buckets A[(h(k)+i2) mod N], for i = 1,2, . . . ,N −1.a. Show that i2 mod N will assume at most (N +1)/2 distinct values, for N prime, as i
The java.util.HashMap class uses separate chaining, but without any explicit secondary structures. The table is an array of entries, and each entry has an additional next field that can reference another entry in that bucket. In this way, the entry instances can be threaded as a singly linked list.
Redesign the AbstractHashMap class so that it halves the capacity of the table if the load factor falls below 0.25. Your solution must not involve any changes to the concrete ProbeHashMap and ChainHashMap classes.
For an ideal compression function, the capacity of the bucket array for a hash table should be a prime number. Therefore, we consider the problem of locating a prime number in a range [M,2M]. Implement a method for finding such a prime by using the sieve algorithm. In this algorithm, we allocate a
Give a pseudocode description of the remove map operation for a skip list.
Describe how a sorted list implemented as a doubly linked list could be used to implement the sorted map ADT.
What is the worst-case asymptotic running time for performing n deletions from a SortedTableMap instance that initially contains 2n entries?
Explain why a hash table is not suited to implement a sorted map.
Modify our ProbeHashMap to use quadratic probing.
Give a pseudocode description of an insertion into a hash table that uses quadratic probing to resolve collisions, assuming we also use the trick of replacing deleted entries with a special “available” object.
Our AbstractHashMap class maintains a load factor λ ≤ 0.5. Reimplement that class to allow the user to specify the maximum load, and adjust the concrete subclasses accordingly.
Which of the hash table collision-handling schemes could tolerate a load factor above 1 and which could not?
What is the worst-case running time for inserting n key-value pairs into an initially empty map M that is implemented with the UnsortedTableMap class?
Let S be a set of n points in the plane with distinct integer x- and y-coordinates. Let T be a complete binary tree storing the points from S at its external nodes, such that the points are ordered left to right by increasing x-coordinates. For each node v in T, let S(v) denote the subset of S
One of the main applications of priority queues is in operating systems—forscheduling jobs on a CPU. In this project you are to build a program that schedules simulated CPU jobs. Your program should run in a loop, each iteration of which corresponds to a time slice for the CPU. Each job is
Write a program that can process a sequence of stock buy and sell orders as described in Exercise C-9.48.Exercise C-9.48An online computer system for trading stocks needs to process orders of the form “buy 100 shares at $x each” or “sell 100 shares at $y each.” A buy order for $x can only
Write an applet or stand-alone graphical program that animates a heap. Your program should support all the priority queue operations and should visualize the swaps in the up-heap and down-heap bubblings. (Extra: Visualize bottom-up heap construction as well.)
Develop a Java implementation of an adaptable priority queue that is based on an unsorted list and supports location-aware entries.
Implement the in-place heap-sort algorithm. Experimentally compare its running time with that of the standard heap-sort that is not in-place.
Extend a solution to the previous problem so that users are allowed to update the prices for their buy or sell orders that have yet to be processed.
An online computer system for trading stocks needs to process orders of the form “buy 100 shares at $x each” or “sell 100 shares at $y each.” A buy order for $x can only be processed if there is an existing sell order with price $y such that y ≤ x. Likewise, a sell order for $y can only
Give an alternate description of the in-place heap-sort algorithm using the standard minimum-oriented priority queue (instead of a maximum-oriented one).
Describe an in-place version of the selection-sort algorithm for an array that uses only O(1) space for instance variables in addition to the array.
Given a class, MinPriorityQueue, that implements the minimum-oriented priority queue ADT, provide an implementation of a MaxPriorityQueue class that adapts to provide a maximum-oriented abstraction with methods insert, max, and removeMax. Your implementation should notmake any assumption about the
Explain how the k largest elements from an unordered collection of size n can be found in time O(n+k logn) using a maximum-oriented heap.
Provide a justification of the time bounds in Table 9.5. Table 9.5 Method Running Time size, isEmpty, min 0(1) insert 0(log n) remove 0(logn) removeMin 0(logn) replaceKey 0(logn) replaceValue O(1)
When using a linked-tree representation for a heap, an alternative method for finding the last node during an insertion in a heap T is to store, in the last node and each leaf node of T, a reference to the leaf node immediately to its right (wrapping to the first node in the next lower level for
Give an implementation of the HeapPriorityQueue’s downheap method that uses recursion (and no loop).
Give an alternative implementation of the HeapPriorityQueue’s upheap method that uses recursion (and no loop).
Reimplement the SortedPriorityQueue using a Java array. Make sure to maintain removeMin’s O(1) performance.
Professor Idle suggests the following solution to the previous problem. Whenever an entry is inserted into the queue, it is assigned a key that is equal to the current size of the queue. Does such a strategy result in FIFO semantics? Prove that it is so or provide a counter example.
Showing 200 - 300
of 1549
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Last
Step by Step Answers