Professional Documents
Culture Documents
Questions 1 Through 25 Are Worth 2 Points Each. Choose One Best Answer For Each
Questions 1 Through 25 Are Worth 2 Points Each. Choose One Best Answer For Each
Questions 1 Through 25 Are Worth 2 Points Each. Choose One Best Answer For Each
1. For the singly linked list implementation of the queue, where are the enqueues and dequeues
performed? c
a. Enqueue in front of the first element, dequeue the first element
b. Enqueue after the last element, dequeue the last element
c. Enqueue after the last element, dequeue the first element
d. Enqueue in front of the first element, dequeue the last element
e. Enqueue after the first element, dequeue the first element
2. Which operation is not supported in constant time by a double-ended queue (deque)? d
a. Insertion at the front or rear item
b. Access to the front or rear item
c. Deletion of the front or rear item
d. Access and deletion of the minimum item
e. All of the above are supported
3. Which of the following operations is not efficiently supported by a singly linked list? c
a. accessing the element in the current position
b. insertion after the current position
c. moving to the position immediately prior to the current position
d. moving to the position immediately following the current position
e. all of the above are efficiently supported
4. What operation is supported in constant time by the doubly linked list, but not supported in constant
time by the singly linked list? b
a. first
b. retreat
c. advance
d. all are constant time on singly linked list
5. Insertion of a node into a doubly linked list requires how many changes to next and prev references? c
a. no changes
b. 1 next, 1 prev
c. 2 next, 2 prev
d. 3 next, 3 prev
e. none of the above
6. What is the running time for merging two singly linked lists into a single singly linked list? a (d only
if there was a tail pointer on one of the lists)
a. O(N)
b. O(N 2)
c. O(N log N)
d. O(1)
e. None of the above.
7. Consider an operation in which you need to return the elements immediately preceding and following
a given item x. Which variation of lists would be most efficient? d
a. singly linked list
b. singly linked list with header and trailer nodes
c. circular list
8. Which of the following algorithms requires the most extra space when implemented as in the text? b
a. insertion sort
b. mergesort
c. quicksort
d. shellsort
e. all use only constant extra space
9. Which of the following algorithms runs in N log N average time but quadratic worst-case time? c
a. insertion sort
b. mergesort
c. quicksort
d. shellsort
e. none of the above
10. If the array is already sorted, which algorithm will perform best? a
a. insertion sort
b. selection sort
c. merge sort
d. quicksort
e. All will take same amount of time
d. search tree
e. stack
13. A recursive algorithm works by solving two half-sized problems recursively, with an additional
linear-time overhead. The total running time is most accurately given by c
a. O( log N )
b. O( N )
c. O( N log N )
d. O( N2 )
e. none of the above
15. Which of the following traversals requires more than linear time in the worst case? e
a. inorder
b. level order
c. postorder
d. preorder
e. all of these traversals are linear time
16. A node with key 8 has a left child with key 10. Which of the following objects could this node be
found in? c
a. binary search tree
b. max heap
c. min heap
d. N!
e. none of the above
19. The following items are inserted into a binary search tree: 3, 6, 5, 2, 4, 7, 1. Which node is the
deepest? c
a. 1
b. 3
c. 4
d. 7
e. none of the above
The next four questions (20 through 23) relate to the tree below:
A
B
C
D
d. preorder
e. (b) and (d)
d. EDCBA
e. none of the above
d. 3
e. none of the above
23. Suppose a postorder iteration is performed on the tree. At the time that D is output, what symbols are
still on the stack? c
a. A only
b. A and B only
c. A and C only
d. A, B, and C
e. none of the above
(10 pts) Show the result of inserting 3, 1, 4, 6, 9, 2, 5, and 7 in an initially empty binary search
tree. Then show the result of deleting the root. You should draw 2 separate trees to answer this
question.
First tree
Inorder listing:
Preorder listing:
Postorder lising:
12345679
31246597
21579643
Second tree
Inorder listing:
Preorder listing:
Postorder lising:
1245679
4126597
2157964
27. (20 pts) Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2, one at a time,
into an initially empty binary min heap Either draw each intermediate heap or give a level order
listing of the nodes in each intermediate heap for full credit (15 trees or 15 listings total). 2 points
extra credit for both trees and level order listings!!
The following are level-order listings of the trees:
Tree 1:
Tree 2:
Tree 3:
Tree 4:
Tree 5:
Tree 6:
Tree 7:
Tree 8:
Tree 9:
Tree 10:
Tree 11:
Tree 12:
Tree 13:
Tree 14:
Tree 15:
10
10 12
1 12 10
1 12 10 14
1 6 10 14 12
1 6 5 14 12 10
1 6 5 14 12 10 8
1 6 5 14 12 10 8 15
1 3 5 6 12 10 8 15 14
1 3 5 6 9 10 8 15 14 12
1 3 5 6 7 10 8 15 14 12 9
1 3 4 6 7 5 8 15 14 12 9 10
1 3 4 6 7 5 8 15 14 12 9 10 11
1 3 4 6 7 5 8 15 14 12 9 10 11 13
1 3 2 6 7 5 4 15 14 12 9 10 11 13 8
28. (20 pts) Write methods that take a reference to a binary tree root T, compute, and return the quantities
specified in parts (a) and (b) below. Assume T is a reference to a BinaryNode object and that each
node in the linked tree contains a key and pointers to left and right BinaryNode children. Include the
code for any helper methods you use.
a) The number of leaves in T.
Method in BinaryTree class:
public int countLeaves(BinaryNode T)
{
if (T == null)
return 0;
if (T.getLeft() == null && T.getRight() == null)
return 1;
return countLeaves(T.getLeft())+countLeaves(T.getRight());
}