• No results found

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

N/A
N/A
Protected

Academic year: 2021

Share "Questions 1 through 25 are worth 2 points each. Choose one best answer for each."

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

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 c. advance

b. retreat 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 d. 3 next, 3 prev

b. 1 next, 1 prev e. none of the above

c. 2 next, 2 prev

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) d. O(1)

b. O(N

2

) e. None of the above.

c. O(N log N)

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 d. doubly linked list

b. singly linked list with header and trailer nodes e. none of the above

c. circular list

(2)

8. Which of the following algorithms requires the most extra space when implemented as in the text? b

a. insertion sort d. shellsort

b. mergesort e. all use only constant extra space

c. quicksort

9. Which of the following algorithms runs in N log N average time but quadratic worst-case time? c

a. insertion sort d. shellsort

b. mergesort e. none of the above

c. quicksort

10. If the array is already sorted, which algorithm will perform best? a

a. insertion sort d. quicksort

b. selection sort e. All will take same amount of time c. merge sort

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

a. hash table d. search tree

b. priority queue e. stack

c. queue

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 c. Performs redundant work

b. Fails to make progress toward base case 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 ) d. O( N

2

)

b. O( N ) e. none of the above

c. O( N log N )

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

a. Expression tree d. (a) and (b)

b. Huffman coding tree e. none of (a), (b), and (c) c. Unix file system

15. Which of the following traversals requires more than linear time in the worst case? e

a. inorder d. preorder

b. level order e. all of these traversals are linear time

c. postorder

(3)

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

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

c. min heap

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 d. N!

b. 1 e. none of the above

c. N

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 d. 7

b. 3 e. none of the above

c. 4

The next four questions (20 through 23) relate to the tree below:

20. Which of the following traversals yields ABCDE? e

a. inorder d. preorder

b. level order e. (b) and (d)

c. postorder

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

a. ABCDE d. EDCBA

b. ABDCE e. none of the above

c. BDECA

22. The height of the tree is c

a. 0 d. 3

b. 1 e. none of the above

A

B C

D E

(4)

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 d. A, B, and C

b. A and B only e. none of the above

c. A and C only

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: 12345679 Preorder listing: 31246597 Postorder lising: 21579643 Second tree Inorder listing: 1245679 Preorder listing: 4126597 Postorder lising: 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: 10

Tree 2: 10 12

Tree 3: 1 12 10

Tree 4: 1 12 10 14

Tree 5: 1 6 10 14 12

Tree 6: 1 6 5 14 12 10

Tree 7: 1 6 5 14 12 10 8

Tree 8: 1 6 5 14 12 10 8 15

Tree 9: 1 3 5 6 12 10 8 15 14

Tree 10: 1 3 5 6 9 10 8 15 14 12

Tree 11: 1 3 5 6 7 10 8 15 14 12 9

Tree 12: 1 3 4 6 7 5 8 15 14 12 9 10

Tree 13: 1 3 4 6 7 5 8 15 14 12 9 10 11

Tree 14: 1 3 4 6 7 5 8 15 14 12 9 10 11 13

Tree 15: 1 3 2 6 7 5 4 15 14 12 9 10 11 13 8

(5)

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

}

}

References

Related documents

Lukas and Rüttinger (2016, p.III) in their German Ministry of Foreign Affairs-funded analyses of the links between climate change and Non-State Armed Groups, also emphasise

This aircraft’s ICE (Will Cost) average unit price grew from $50 million Average Unit Procurement Cost (APUC) when the program began (in 2002 dollars, when the program was

Subsection III Operational Certification Criteria of the Land Ambulance Service Certification Standards (k) states, all reasonable measures are taken to ensure that each

If the student does not accept the decision of the appropriate Department Chairperson, Dean of Instruction, or the Vice President for Instruction and Student Development, the

The addition of carbidopa to L-dopa in the treatment of Parkinson's disease causes a DECREASE in all the following EXCEPT.. incidence of

oxygenated→ Next through the pulmonary veins (they carry oxygenated blood)→ It then goes to the left atrium → to the left ventricle (the big bad pump)→ It is then pumped

The primary outcome parameter was the reduction in hemoglobin concentrations due to delivery, and the secondary outcome parameters were changes in hemoglobin of more than 3 g/dL

Duck and Foie Gras ‘en terrine’, Autumn Salad, Quince paste Grilled Seabass, Market Vegetables, Cornish Mussel and.