Review Questions (Multiple Choice) 1. In a binary search tree, all of the items in the left subtree of a given node are a. less than the item in the node b. greater than the item in the node 2. A binary search tree could be effectively used to implement a a. sorted list b. sorted bag 3. When copying the items in a binary search tree to a new BST, it is best to visit them in a. inorder b. preorder 4. When copying the items in a binary search tree to a new sorted list, it is best to visit them in a. inorder b. preorder 5. The height of a balanced binary search tree is a. log2(n) b. ½ n 6. The items in a heap are generally a. larger than their parent item b. smaller than their parent item 7. The item popped from a heap is the a. smallest remaining item b. largest remaining item 8. In the array representation of a complete binary tree, an item’s parent is found by a. multiplying the item’s array index by 2 b. dividing the item’s array index by 2 2 9. The type of unordered collection with the fasted access times overall is a. a sorted collection b. a collection that uses a hashing strategy 10. Each item in a graph has a. zero or one predecessor and zero or one successor b. zero or one predecessor and zero or more successors c. zero or more predecessors and zero or more successors 11. A sparse graph is best implemented as an a. adjacency matrix b. adjacency list 12. A vertex A is adjacent to a vertex B in a graph if a. there is a directed edge from A to B b. there is a directed edge from B to A 13. The type of graph traversal that visits the adjacent vertices first is called a. breadth first b. depth first 14. A heap can be used to represent a a. sorted list b. priority queue 15. Tree nodes without daughters are called a. interior nodes b. leaf nodes Review Questions (Short Answer) 1. What is the difference between a binary search tree and a heap? 2. What is the difference between a queue and a priority queue? 3. Jack must develop a sorted bag class. He has in his library a bag class. Where should the new class go in the hierarchy, and which methods will have to be added and/or modified? 4. What types of items can be added to sorted collections? 5. Describe two applications of stacks, stating why a stack is the logical choice of a collection for those applications. 3 6. Describe two applications of queues, stating why a queue is the logical choice of a collection for those applications. 7. Assume that the variable lyst refers to a list of integers in random order. Write one line of code that resets this variable to a sorted list containing the same integers. You should assume that this list was originally of type list, and that there exists an ArraySortedBag class. You should not use any sort methods in your code. 8. What is the memory complexity, in big-O notation, of a collection that uses a twoway circular linked structure with a dummy header node? When does an array become a better implementation structure than such a two-way linked structure? 9. Draw a picture of a balanced binary search tree constructed from the numbers 1-7. 10. State the sequences of numbers visited in the preorder, inorder, postorder, and levelorder traversals of the tree in question #9. 11. Why does the iterator for a binary search tree use a preorder traversal? 12. Which shape of a binary search tree produces the worst possible performance of the search operation? 13. Which methods are generally placed in the most concrete classes in a hierarchy of collection classes, and why? 14. Which methods are generally placed in the abstract classes in a hierarchy of collection classes, and why? 15. What is the difference between an iterator and a list iterator? 16. Why do list implementations track a mod count? 17. Draw pictures of the expression trees for the following expressions: a. 45 + 22 * 3 b. (33 – 6) / 4 c. 3 * 2 ^ 4 d. 3 ^ 2 ^ 6 18. The LinkedBST class has an instance variable named self._root. Write a new method named frontier. This method expects no arguments (other than self) and returns an iterator on the leaf items in the tree. You should use a recursive strategy in your code. Review Questions (Matching) Write the worst-case runtime performance value to the right of the algorithm that it characterizes. Each algorithm operates on a collection of elements. The performance values in the right column are the available options, some of which will be repeated as answers. 4 Algorithm Popfromoraddtoa queue Answer O(k) Index-basedinsertioninto alist Appendtoalist(whenno resizeofarrayisneeded) Setintersectionwith sortedsets(bothsetsare thesamesize) Setintersectionwith unsortedsets(bothsets arethesamesize) Removeitemfromalist usingalistiteratorata givencursorposition O(logn) O(n) O(nlogn) O(n2) O(kn) Findavalueinabinary searchtree(notbalanced) Index-basedaccessor replacementinalinked list Insertavalueintoa balancedbinarysearch tree Worst-CasePerformance