Review Questions for Second Exam

advertisement
Review Questions (Multiple Choice)
1. A collection that contains unique items in no particular order is a
a. bag
b. set
2. A collection in which the last item added is the first one removed is a
a. queue
b. stack
3. An array makes more efficient use of memory than a singly linked structure when
a. the number of items is greater than one half of the array’s capacity
b. the number of items is greater than less than one quarter of the array’s
capacity
4. A sorted list should not include the
a. object-based mutator methods
b. index-based insertion and replacement methods
5. The collection method that never changes its implementation is.
a. isEmpty
b. __eq__
c. __str__
6. An appropriate container of the items within a sorted bag is
a. an array
b. a two-way linked structure
c. a singly linked structure
7. When a class includes an __iter__ method, Python automatically provides code
for the
a. == operatorr
b. [] operator
c. in operator
8. The iterator on a queue visits item items from
a. rear to front
b. front to rear
2
9. The iterator on a stack visits its items
a. from top to bottom
b. from bottom to top
10. Each item in a tree has
a. zero or one predecessor and zero or one successor
b. zero or more predecessors and zero or more succesors
c. zero or one predecessor and zero or more successors
11. A collection in which each item has at most two successors is a
a. binary tree
b. general tree
12. A list iterator allows the programmer to replace all of the items in a linked list in
a. linear time
b. quadratic time
13. The index-based operations on a linked list run in
a. constant time
b. linear time
14. A two-way circular linked structure with a header node allows the programmer to
access the last data node in
a. constant time
b. linear time
15. Tree nodes without daughters are called
a. interior nodes
b. leaf nodes
16. The node at the top of a tree is called
a. the header node
b. the root node
Review Questions (Short Answer)
1. What is the difference between a bag and a set?
2. What is the difference between a queue and a priority queue?
3
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.
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. Write the equivalent postfix expressions for each of the following:
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, which is
either None or a node containing fields named left, right, and data. Assume
that the data in this tree are integers. Write a new method named treeSum. This
method expects no arguments (other than self) and returns the sum of the
4
integers in the tree. You should use a recursive strategy in your code, and assume
that an empty tree’s sum is 0.
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.
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
Download