Questions 1 Through 25 Are Worth 2 Points Each. Choose One Best Answer For Each

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

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

d. doubly linked list


e. none of the above

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

11. Which data structure is used by the compiler to implement recursion? e


a. hash table
b. priority queue
c. queue

d. search tree
e. stack

12. The following method violates which rule(s) of recursion? d


static int recurse( int n )
{
if( n == 0 )
return 0;
else
return n + recurse( n/2 )+ recurse( n/2+1 );
}
a. No base case
b. Fails to make progress toward base case

c. Performs redundant work


d. (b) and (c)

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

14. Which of the following, as discussed in lecture, is a binary tree? d


a. Expression tree
b. Huffman coding tree
c. Unix file system

d. (a) and (b)


e. none of (a), (b), and (c)

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. (a) and (c)


e. none of (a), (b), and (c)

17. Which iterator traversal does not use a stack? b


a. inorder
d. preorder
b. level order
e. all of these traversals use a stack
c. postorder
18. How many N node binary trees with items 1, 2,..., N have identical preorder and inorder traversals? b
a. 0
b. 1
c. N

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

20. Which of the following traversals yields ABCDE? e


a. inorder
b. level order
c. postorder

d. preorder
e. (b) and (d)

21. Which output could be produced by an inorder traversal of the tree? e


a. ABCDE
b. ABDCE
c. BDECA

d. EDCBA
e. none of the above

22. The height of the tree is c


a. 0
b. 1
c. 2

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

24. Which of the following is not a red-black tree property? b


a. the root is black
b. all leaves are black
c. consecutive red nodes are disallowed
d. every path from a node to an external node must contain the same number of black nodes
e. all of the above are red-black tree properties
25. Every node in a binary min heap b
a. has two children
b. is no larger than its children
c. is no smaller than its children
d. has a smaller left child than right child
e. two or more of the above
26.

(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());
}

b) The number of nodes in T that have one non-null child.


Method in BinaryTree class:
public int countSolos(BinaryNode T)
{
if (T == null)
return 0;
if (T.getLeft()==null && T.getRight()==null)
return 0;
else
{
if (T.getLeft()==null && T.getRight()!=null)
return 1 + countSolos(getRight());
if (T.getLeft()!=null && T.getRight()==null)
return 1 + countSolos(getLeft())
return countSolos(getLeft())+countSolos(getRight());
}
}

You might also like