Quiz 4B
Which of the following statements about binary search trees is FALSE?
- When removing an element from a Binary Tree, the root node will always change - A Binary Tree will always be the same for any given set of elements, regardless of the order in which the elements are inserted. - Binary search runs on Binary Trees in constant time, since the tree is already binary itself.
Which statements are TRUE about the time-complexities of heap operations on a properly constructed heap stored as a tree in an array? A. The poll() (or remove) operation will have worst-case time-complexity Θ(log n). B. The push() (or insert) operation will have best-case time-complexity Θ(1). In other words, it's possible that push will only have to do a constant number of operations for some values. C. The push() (or insert) operation will have expected-case time-complexity Θ(log n).
A and B
Which of the following statements A-C are TRUE statements about storing a heap in an array? A. When an element is removed, the last element stored in the array replaces the first value and then percolate_down() is called on that element. B. With each value we also store the location of the value's parent in the tree (by storing the value and locations in a node in the heap). C. The heap order property lets us find if a value x is in the heap in logarithmic time.
A only
Which of the following statements is most true about the number of tree rotations an AVL Tree might do when inserting a node?
An AVL Tree may not rotate at all when inserting, but if it does it won't be more than one single or one double rotation per insert.
A HashSet is implemented using a specific type of which Abstract Data Type?
Hash Table
Suppose we implement a priority queue, but instead of a Heap, we use a SORTED vector. We will sort this vector such that the minimum (next element to be deleted) is at the highest index. Which of the following best describes the worst-case runtimes for peek(), push(), and poll() given this implementation.
Θ(1), Θ(n), Θ(1)
Suppose we implement a priority queue, but instead of a Heap, we use an UNSORTED vector. Which of the following best describes the worst-case runtimes for peek(), push(), and poll() given this implementation?
Θ(n), Θ(1), Θ(n)
Suppose you have a hash table with a capacity of 5, and you are inserting integers into this table. The hash function that will be used is: hash(int key) = (3*key + 1) Operations: Insert the following elements into the table: 3, 9, 1, 12, 7. What index in the table does the 7 end up being inserted into? For your collision resolution strategy, you should use Linear Probing as described in class. Key: 3 9 1 12 7 hash(key): 10 28 4 37 22
Index 1
Suppose you have a hash table with a capacity of 10, and you are inserting integers into this table. The hash function and second hash function that will be used are: hash(int key) = key % 10 hash2(int key) = 1 + (key % 3) Insert the following elements into the table: 5, 15, 8, 18, 11, 35. What index in the table does the 35 end up being inserted into? For your collision resolution strategy, you should use Double Hashing where hash1(k)+ n * hash2(k) as n increments by 1, starting at 0.
Index 4
Suppose we implement a priority queue using an array (or ArrayList) of queues (arrpriority_queue) where the number of priorities is fixed. What is the worst case Big O for adding an element onto an arrpriority_queue?
O(1) - Constant time
What is the worst-case runtime of inserting into a hash table with separate chaining if we do not want to store items with duplicate keys in a bucket's list.
O(n) - Linear time
If you ever want to put your own objects (e.g., Cats, Students, etc.) in a Binary Search Tree (BST), you must make sure that your class (e.g., Cat class or Student class, etc.)... Select from this list the item or items you must make sure your class has or does: 1. implements the Collections interface 2. implements the Comparable interface 3. has an inOrder() tree traversal method correctly implemented (written) 4. has a compareTo() method correctly implemented (written) 5. has a compare() method correctly implemented (written)
Options 2 and 4 only
Which of the following statements about binary search trees is TRUE?
Search for a Binary Tree is O(n) (worst case) because potentially all nodes need to be searched, but usually runs in logarithmic time.
Suppose you are given a Binary Tree (that is NOT a Binary Search Tree...so items are NOT sorted in any order in the tree). Also suppose that you are trying to implement the find( ) method on this data structure (find whether or not a given element is in the tree). Which of the following statements is MOST true about the worst-case runtime of this method.
The find() will be Θ(n). It might terminate early if we find the element, but the worst-case will always be that we examine every element in the tree.
Consider the following implementation of the find function within a Binary Tree class, which may not work as intended. Which of the following best explains the error, if any? private boolean find(T data, TreeNode<T> curNode) { if(curNode == null) return false; else if (curNode.data == data) return true; else if (data.compareTo(curNode.data) < 0) return find(data, curNode.left); else if (data.compareTo(curNode.data) > 0) return find(data, curNode.right); return false; }
The method uses == to compare the data for equality.
When using a Map data structure, all of the key objects must be unique.
True
