
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.4
Define an “element” in the comparator-based priority queue to be a pair consisting of a key and an element. Define the comparator function so that, given two key-element pairs, returns the result of the comparison of the two keys. Finally, define the operation min so that it returns just the element part of a key-element pair.
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.6
(1, d), (3, j ), (4, b), (5, a), (2, h), (6, c).
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.7
The best data structure for this air-traffic control simulation is a priority queue. The priority queue will enable the handling of the time-stamps and keep the events in order so that the event with the smallest time-stamp is extracted easily
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.9
221536441039132925
315364410229132925
393644102215132925
391044362215132925
391013362215442925
391013152236442925
391013152236442925
391013152225442936
391013152225294436
391013152225293644
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.10
221536441039132925
152236441039132925
152236441039132925
101522364439132925
310152236449132925
391015223644132925
391013152236442925
391013152229364425
391013152225293644
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.11
A worst-case sequence for insertion sort would be one that is in descending order of keys, e.g., 443629252215131093. With this sequence, each element will first be moved to the front and then moved back in the sequence incrementally, as every remaining is processed. Thus, each element will be moved n times. For n elements, this means at a total of n2 times, which implies Ω(n2 ) time overall.
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.12
The largest key in a heap may be stored at the root (for a max heap) or at any leaf node (for a min heap).
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.15
Yes, tree T is a heap. It is a complete binary tree and, except for the root, each node stores a key value greater than the key of its parent.
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.16
Since a heap is a complete binary tree, levels of the heap are always filled left to right. Thus, if a left child is external, then the right child must either be external or non-existent. Likewise, if the right child is internal, then the left child is internal.
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.17
With a preorder traversal, a heap that produces its elements in sorted order is that which is represented by the vector (1, 2, 5, 3, 4, 6, 7). There does not exist a heap for which an inorder traversal produces the keys in sorted order. This is because in a heap the parent is always less than all of its children or greater than all of its children. The heap represented by (7, 3, 6, 1, 2, 4, 5) is an example of one which produces its keys in sorted order during a postorder traversal.
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.18
The number of nodes in a binary tree is done by a function known as level numbering. The nodes are numbered level by level (top to bottom), left to right. Since a heap is a complete binary tree, insertions will always be done on the bottom-most level to the right of the current rightmost leaf node. Thus, the insertion will always be done at position n + 1, the position to the right of the last node, the node in position n.
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.20
Consider the last n/2 terms in this sum. Each one is at least log n/2 = log n 1. Thus, this sum is at least (n/2) log n n/2, which is O(n log n).
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.21
Imagine the heap which is represented by the vector [x, 1, 5, 2, 8, 9, 7, 6]. This heap will not produce keys in sorted order when a preorder traversal is used.
Data Structures and Algorithms in C++ (Second Edition)
M. T. Goodrich, R. Tamassia, and D. M. Mount John Wiley & Sons
Solution of Exercise R-8.22
Imagine the heap which is represented by the vector [x, 1, 5, 2, 8, 9, 7, 6]. This heap will not produce keys in nonincreasing order when a postorder traversal is used.