All Matches
Solution Library
Expert Answer
Textbooks
Search Textbook questions, tutors and Books
Oops, something went wrong!
Change your search query and then try again
Toggle navigation
FREE Trial
S
Books
FREE
Tutors
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
Ask a Question
Search
Search
Sign In
Register
study help
computer science
introduction to algorithms
Questions and Answers of
Introduction to Algorithms
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
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
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.
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
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
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′
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
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
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
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
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/
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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).
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
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
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
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
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,
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
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
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].
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
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
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
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
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
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
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
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
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
Showing 200 - 300
of 1549
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Last