m2-summer2017
m2-summer2017
I pledge on my honor that I have not given or received any unauthorized assistance on this
examination.
Instructions
1. Multiple Choice / 25
2. Short Answer / 45
3. Programming / 30
Total /100
1
1. [25 pts] Multiple Choice
Identify the choice that best completes the statement or answers the question. Circle
your answer.
1) [2] The cost of inserting or removing an element to/from a heap is log(N), where N is the
total number of elements in the heap. The reason for that is:
a) Heaps keep their entries sorted
b) Heaps are balanced BST.
c) Heaps are balanced binary trees.
d) Heaps are a version of Red-Black trees
a) 4
b) 8
c) 16
d) Runtime Error
2
5) [2] A priority queue can be efficiently implemented using which of the following data
structures?
a) Array
b) Linked List
c) Binary Heaps
d) None of the above
a) 10, 8, 7, 5, 3, 2, 1
b) 10, 8, 7, 2, 3, 1, 5
c) 10, 8, 7, 1, 2, 3, 5
d) 10, 8, 7, 3, 2, 1, 5
7) [2] What is the worst-case time complexity for insert and remove operations in a Binary
Heap?
a) 2k and 2k-1
b) 2k+1 and 2k+2
c) 2k and 2k+1
d) 2(k+1) and 2(k+1)+1
9) [3] The following numbers are inserted into an empty binary search tree in the given order:
10, 12, 3, 5, 14, 2, 11, 18. What is the height of the binary search tree (the height is the
maximum distance (number of edges) of a leaf node from the root, height of a tree with one
node is 0.)?
a) 5
b) 3
c) 4
d) 2
3
10) [3] Here is an INCORRECT pseudo code for the algorithm which is supposed to determine
whether a sequence of parentheses is balanced:
Which of these unbalanced sequences does the above code think is balanced?
a) ((())
b) (()()))
c) ())(()
d) (()))()
a) O(log n)
b) (n)
c) (1)
d) (n log n)
12) What is the number of swaps that occur after inserting 20 into the following heap:
a) 0
b) 1
c) 2
d) 3
4
2. [45 pts ] Short Answer
1) [6] Write the preorder, inorder, and postorder traversal of the following binary tree:
preOrder
inOrder
postOrder
int foo(Node n) {
if (n == null) return 0;
if (n.left == null && n.right == null) return 1;
return foo(n.left) + foo(n.right);
}
Answer:
3) [3] Suppose that you do binary search for the key 39 in the following sorted array of size
15:
10 11 25 31 36 39 53 55 56 64 68 75 78 82 87
Give the sequence of keys in the array that are compared with 39 Answer:
5
4) [3] If the given preOrder traversal for a binary search tree is {10, 3,1, 7, 15, 20, 25},
construct the binary search tree.
5) [4] Given the following contents of an array implementation of a stack, where 50 is at the
top of the stack.
0 1 2 3 4 5
20 30 40 50
Show the contents of the stack and the location of top after doing the following
stack.pop();
stack.push(60);
stack.push(70);
stack.push(80);
stack.pop();
stack.push(90)
0 1 2 3 4 5
6
6) [4] Given the following contents of a circular array implementation of a queue
0 1 2 3 4 5
20 30 40
first last
Show the contents of the queue and locations of first and last after doing the following:
Queue.dequeue();
Queue.dequeue();
Queue.enqueue(50);
Queue.enqueue(60);
Queue.enqueue(70);
Queue.dequeue();
Queue.enqueue(80);
0 1 2 3 4 5
0 1 2 3 4 5 6 7 8 9 10
-- 16 14 10 8 7 9 3 2 4 1
8) [4] Show the contents of the heap after the value 15 is inserted.
0 1 2 3 4 5 6 7 8 9 10 11
--
7
9) [3] Draw the Binary Search Tree after you delete key 8.
10) [5]Construct a left-leaning red-black tree when the following elements are inserted in this
order: 20, 10, 15, 18, 30. Show your steps for each number. Use dashed line for red edge.
8
11) Given a 2-3-4 tree as follows
a) [3] Draw the 2-3-4 after inserting 44, 84,86 into this tree
b) [3] Draw the tree after deleting 94 from the original 2-3-4 tree
9
3. [30 pts] Programming questions
Use the following Node definition for questions 1)—4)
class Node{
int key;
Node left, right;
}
1) [6] Write a method “int sum(Node r)”, which returns the sum of all keys in a given
binary tree. Calling sum on the following tree will return 51. Sum of an empty tree is 0.
2) [8] Write a method “boolean find(Node r, int key)”, which receives a Binary
Search Tree r and a key as arguments, and returns if the key exists in the tree
10
3) [8] Write a method “int getMin(Node r)”, which returns the minimum key in the given
Non-Empty Binary Tree. Calling getMin on the following tree returns 1.
4) [8] write the method “Node mirror(Node r)”, which returns the mirror of the given
binary tree. For example, calling mirror on the original tree, will return the mirror tree shown
below.
11