Midterm 2 CS 2420
(True/False) For a sparse graph, an adjacency matrix representation is an efficient use of space/memory.
False
For a balanced BST, what is the behavior of insertion in the average case?
O(log N)
Assume this class definition: class Node { public char data; public Node next; public Node() { this.data = '\0'; this.next = null; } public Node (char data, Node next) { this.data = data; this.next = next; } } Which of the following single Java statements has the same effect as this sequence of Java statements: Node newNode = new Node(); newNode.data = 'n'; newNode.next = curr.next; curr.next = newNode; - Node newNode = new Node('n', curr.next); - curr = new Node('n', curr); - curr.next = new Node('n', curr.next); - curr.next = new Node(); - none of the above
curr.next = new Node('n', curr.next);
Evaluate this postfix expression: 4 5 7 2 + - *
-16
Which is a valid topological sort of the graph represented by the following DOT specification? (Select all that apply.) digraph D { 0 -> 000 00 -> 000 } 0 00 000 0 000 00 000 00 0 00 0 000 00 000 0 000 0 00
0 00 000, 00 0 000
Evaluate this postfix expression: 1 2 3 2 ^ * +
19
Which of these algorithms solves the weighted, single-source shortest path problem? (Select all that apply.) Breadth-first search Depth-first search Dijkstra's algorithm Topological sort
Dijkstra's algorithm
Level order traversal of a binary tree uses which of the following? FIFO Queue priority queue stack none of the above
FIFO queue
(True/False) Dijkstra's algorithm is guaranteed to find the shortest weighted path in a graph for which some edge weights are negative values.
False
(True/False) In a BST, if we are removing a node with two children, we can replace it with the smallest item from its left subtree and it will still be a BST.
False
(True/False) The graph represented by the following DOT specification is a DAG (i.e., directed acyclic graph). digraph D { 1 -> 2 2 -> 3 3 -> 4 4 -> 5 5 -> 3 1 -> 6 6 -> 7 7 -> 5 2 -> 7 }
False
Which of the following snippets of Java code does not belong in a method to traverse a linked list? (Select all that apply.) - Node start = temp; - Node temp = start; - start = start.next; - temp = temp.next; - temp.next = temp; - while(temp != null)
Node start = temp; start = start.next; temp.next = next;
Consider a left-heavy BST (i.e., all right children are null). What is the running-time behavior of last()? Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior.
O(1)
Java's LinkedList class represents a doubly-linked list. What is the Big-O behavior of its addFirst method for a list of size N?
O(1)
What is the Big-O behavior of the add method in Java's ArrayList for a list of size N, when the value of index is N (i.e., when adding to the end of the array/list)? You should assume that, in general, the backing array does not need to grow during a call to add.
O(1)
What is the Big-O behavior of the addLast method in Java's LinkedList class for a list of size N?
O(1)
What is the Big-O behavior of the get(int index) method in Java's ArrayList for a list of size N, when the value of index is N/2?
O(1)
Consider a left-heavy BST (i.e., all right children are null). What is the running-time behavior of first()? Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior.
O(N)
For any BST, what is the behavior of insertion in the worst case?
O(N)
Java's ArrayList class represents a basic array. As a convenience for the user, when the capacity of the backing array is exceeded, the class handles creating a new larger array and copying over the existing items. Its add(int index, E element) method inserts the specified element at the specified position. What is the Big-O behavior of this method for a list of size N, when the value of index is 0 (i.e., when adding to the beginning of the array/list)?
O(N)
What is the Big-O behavior of the get(int index) method in Java's LinkedList class for a list of size N, when the value of index is N/2?
O(N)
Consider any valid BST. What is the running-time behavior of add(x), where x is an item smaller than all of the items currently contained in the BST? Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior.
O(h)
Consider a balanced BST. What is the running-time behavior of add(x), where x is an item smaller than all of the items currently contained in the BST? Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior.
O(log N)
What data structure uses FIFO (first in, first out) ?
Queue
What data structure uses LIFO (last in, first out) ?
Stack
(True/False) The pop operation for a stack has the same Big-O behavior as the dequeue operation for a queue.
True
(True/False) When a queue is implemented with a basic array, the Big-O behavior of the enqueue operation is the same as when it is implemented with a linked list.
True
(True/False) When a stack is implemented with a basic array, the Big-O behavior of the push operation is the same as when it is implemented with a linked list.
True
The breadth-first search algorithm structure makes use of which abstract data type?
queue
The depth-first search algorithm structure makes use of which abstract data type?
stack