CSS 445 pt 2
Which node do we need to search for in order to remove the element at index i? A i B i-1 C i+1 D All of the above
B i-1
Use average-case analysis to determine the average runtime of ArrayList's remove(int pos), assuming each index is equally likely to be removed. A O(1) B O(lg n) C O(n) D O(n^2)
C O(n) choices x probability(constant)
What is the worst-case runtime of QuickSort using Median of Threes? A O(n) B O(n lg n) C O(n^2)
C O(n^2)
A node in a tree with no parent is called a(n): (a) sibling (b) leaf (c) orphan (d) root
D
full trees have how many nodes
2^k - 1
Which method will be asymptotically slower in LinkedList than in ArrayList? A add(T entry) B add(int pos, T entry) C remove(int pos) D contains(T entry)
A add(T entry)
In which scenarios will our simple Quicksort (pivot = last element) have worst case runtime? Multiple answers:You can select more than one option A All scenarios B Reverse sorted array C Random order array D Sorted array
B D
After iteration has ended, an Iterator's next method: A returns false B returns null C throws an exception D cannot be called
C throws an exception
All leaves in a complete binary tree are on the same level.
F
For some problems, there is no iterative solution, and recursion is the only way to attain a solution.
F
The entries in a list of size n are numbered from 0 to n.
F
QuickSort summary
(practically speaking) fast sorting algorithm ▸Average/Best case: O(n log n) The array is split in half (or nearly in half) at each step ▸Worst case: O(n^2) poor pivot choice (max/min element), which makes a recursive call on an input size only 1 element smaller
mergeSort procedure
1. Divide array in half continuously into smaller and smaller arrays. 2. At the lowest level, these arrays are sorted 3. then merged together after sorting in the reverse order they were divided apart in.
Depth first - Post-Order Traversal
1. Process left child. 2. Process right child. 3. Process self. left most leaf first ------> Root last dot on right
Depth first - Pre-Order Traversal
1. Process self. 2. Process left child. 3. Process right child. Root first ------> right most leaf dot on left
What is the behavior of thehasNext()method when the list is empty?(a) returns false (b) returns null (c) throws NoSuchElementException (d) returns 0
A
True/False: A new element (i.e., not a replacement) is always added to a BST as a leaf A True B False
A True
You can create different binary search trees from the same data. A True B False
A True
Which of the following describe the preceding merge sort algorithm? Select all that apply. Multiple answers:You can select more than one option A Recursive B Divide & conquer C Tail recursive D Generic
A B D
why are trees "recursive" defined?
A tree can be defined in terms of itself , each node has children which are themselves trees
Which of the following sorting algorithms cannot be easily made stable? A Insertion sort B Bubble sort C Selection sort D Shell sort
B Bubble sort
Why didn't we need a removeGap method for ArrayBag? A Duplicates are not allowed in Bag B Elements aren't ordered in Bag C Elements aren't required to stay contiguous in ArrayBag D Bag does not support removal of a specific element
B Elements aren't ordered in Bag
In a circular linked implementation of a queue, insertion and deletion of entries is accomplished using modulo arithmetic. A True B False
B False modulo is used in array based queues
in a linked chain implementation of a queue, the enqueue operation requires a traversal to the end of the chain to insert a new entry. A True B False
B False there is a back reference
What type of traversal makes the most sense for a BST? A Pre-order B In-order C Post-order D Level-order
B In-order
What is the runtime of merge sort's merge() routine given n elements? A O(lg n) B O(n) C O(n lg n) D O(n^2)
B O(n)
Which of the following methods are linear-time in ArrayList? Multiple answers:You can select more than one option A add(T entry) B add(int pos; T entry) C remove(int pos) D contains(T entry) E set(int pos, T entry)
B C D (others are constant time) B ----> not overwrite, shift to make room
Which of the following are true of (practical implementations of) Quicksort? Multiple answers:You can select more than one option A It has O(n lg n) worst case runtime B It is in-place C It is stable D It has O(n lg n) average case runtime E It is usually faster than merge sort in practice
B D E
In merge sort, after merging the two sorted halves of the array, the next step is to: (a) copy the rest of the first array elements to the temporary array (b) copy the original array to the temporary array (c) copy the rest of the second array elements to the temporary array (d) copy the merged array back to the original array
D
In the makeRoom method of the array-based implementation of the ADT list: (a) entries are shifted toward the end of the array, starting with the first entry (b) entries are shifted toward the beginning of the array, starting with the last entry (c) entries are shifted toward the beginning of the array, starting with the first entry (d) entries are shifted toward the end of the array, starting with the last entry4/
D
How can we define the height of a subtree recursively, based on the height of its children? A max(height(leftChild), height(rightChild)) B 1 + min(height(leftChild), height(rightChild)) C 1 + height(leftChild) + height(rightChild) D 1 + max(height(leftChild), height(rightChild))
D 1 + max(height(leftChild), height(rightChild))
What is a complete tree? A Every non-leaf has two children B Every level is filled left to right C Every non-leaf has two children, all leaves on the same level D Every level is filled except the last, which is filled left to right
D Every level is filled except the last, which is filled left to right
What is the runtime of heapsort with n elements? A O(1) B O(lg n) C O(n) D O(n lg n) E O(n^2) F O(n^2 lg n)
D O(n lg n)
What is the total runtime of iterating through a LinkedList using a for() loop? A O(1) B O(n) C O(n lg n) D O(n^2)
D O(n^2) need for loop and node.next()'s
Any node, X, along with all of X's __, form the subtree rooted at X. A Parents B Siblings C Ancestors D Children E Descendants
E Descendants
free node
first available node. always keep one in queue
Depth first - In Order traversal
1. Process left child. 2. Process self 3. Process right child Root last dot on bottom
Heap Properties
root is larger than both subs heaps are complete binary trees
define stability in sorting. Which divide & conquer sorting algorithm has this property?
stability - tied items do not "jump over" each other (stay in original RELATIVE order)
depth first
traverse full left tree before traversing the right tree
major difference btw trees and data structures so far
trees are non linear ( hierarchical ) trees are not sequential ( recursive )
level order iterator
uses queue
Heap: advantage
asymptotic performance of tree low storage overhead of a array fast indexing of an array
Merge Sort runtime
at each level cut in half uses auxiliary array O (log(n)) levels O (nlog(n)) total
sorting runtimes two final
average, worst case quickSort - O(nlogn), O(n2) mergeSort - always O(nlogn)
sorting runtimes mid review
average, worst case selection - O (n2) bubble - O(n2) , O(n) insertion - O(n2) , O(n) shell - depends on gap size O(n1.333)
queue
collection, first in first out add to back, remove from front keep references for first and last nodes
Heap remove idea
if deleting root, delete last leaf and move its value to the root, push the node down the tree until it is in a valid spot
Merge vs Quick Sort
merge - always nlogn , NOT in place ( memory O(n) ) , IS stable( if two objects are equal they remain in relative order after sort) quick - worst case O(n^2), IS in place ( memory O(1) ), NOT stable
list vs bag major differences
order matters for lists
quickSort procedure
pivot is last element, assign high and low pointer 1.compare pointers until both are in wrong position then swap 2. if one is in correct position then move pointer closer toward center 3. once pointer cross over, swap pivot with left pointer
deQueue()
remove element from front
Array Queue
reuses first indices w/ back ref wrap around if back >= array.length
enQueue()
add element to back moves queue node forward
What is the runtime of ArrayList's add(int, E) method? A O(1) B O(lg n) C O(n) D O(n lg n) E O(n^2)
C O(n)
A sorting algorithm that requires less than O(n) memory overhead is called: (a) sublinear (b) linear (c) stable (d) in-place
D
A tree is a data structure that utilizes organization. (a) logarithmic (b) quadratic (c) linear (d) hierarchical
D
how do you instantiate an iterator ?
Iterator myIterator = list.iterator();
Why do we need to create a new class for Iterator rather than allowing the collection to implement the operations itself? A To simplify the code for the collection B To compartmentalize code for different purposes C So that multiple iterators can exist with different states
C So that multiple iterators can exist with different states
Why do we make the Iterator class an inner class of the collection? A So that other code in the class can access the iterator's private data members B So that the Iterator class can be non-generic C So that the Iterator has access to the collection's private data members D So that the Iterator class can use less memory
C So that the Iterator has access to the collection's private data members
In complexity, problem size is defined as: (a) the amount of memory an algorithm uses (b) the overall complexity of an algorithm (c) the number of items an algorithm processes (d) the amount of execution time an algorithm takes
C
The average-case performance of merge sort for n items is: (a)O(n) (b)O(n2) (c)O(nlogn) (d)O(logn)
C
What is the result of Quicksort's partition routine on the following array? 10 45 35 29 42 6 12 30 A 10 12 6 30 42 35 45 29 B 6 10 12 29 30 35 42 45 C 10 12 6 29 30 35 45 42 D 12 6 42 29 35 45 10 30
C
Which method(s) does the interface Iterable declare? (a)hasNext() (b)next() (c)iterator() (d) both A and B
C
Which of the following is not a valid way to determine if a list is empty? (a) check if is Empty() returns true (b) check if get(0) throws an exception (c) check if replace() returns null (d) check if size() returns 0
C
List vs Linked List: get(pos) differences
List: index into array... O(1) Linked: need to traverse runtime depends on input worst case: O(n)
Consider the MergeSort and QuickSort sorting algorithms. Give the Big-O run-times for both algorithms (average and worst cases). In real terms, which algorithm has the faster runtime and why? Be specific
MergeSort average and worst case: O(Nlog2N) QuickSort average case: O(Nlog2N); QuickSort worst case: O(N2 ) Despite its possible poor worst case performance, QuickSort tends in general to be faster than MergeSort due to extra overhead incurred in the MergeSort algorithm. This overhead is due to the merge algorithm, which requires the data to be merged into a new, temporary array before being copied back into the original array. This extra copying requires extra time. QuickSort, on the other hand, sorts the data in place, and does not require an extra array.
binary search tree operations runtime
O(height) if balanced, height in log AVG case: O(logn) Very unblanced: O(n)
Array List ops: add(E entry)
add(E entry) - O(1) get(int pos) - O(1) set(int pos, E entry) - O(1) add(int pos, E entry) - O(n) - makeRoom() shift remove(int pos) - O(n) - closeGap() shift contains(E entry) - O(n) - traverse
single Linked list op runtimes
add(E entry) - O(n) - shift nodes get(int pos) - O(n) - getNodeAt(int pos)) set(int pos) - O(n) - getNodeAt(int pos) remove(int pos) - O(n) - getNodeAt(int pos) contains(E entry) - O(n) - traverse
Why are iterators private inner classes
allows linkedListIterator to access all of linked lists private instance variables
Which divide & conquer sorting algorithm, as it is typically implemented, has a worst-case runtime that is asymptotically higher thanO(nlogn)? Explain why this is.
QuickSort: if you get the min or max for pivot each time, the runtime is O(n^2)
A client of the list ADT can use the list operations without knowing how the operations are implemented in the list.
T
A variable can hold a reference to an object if the object's type is a subclass of the variable's type.
T
An iterator implemented as an inner class can be more efficient than one implemented as a separate, external class.10.
T
During each iteration of merge sort, it divides the input array in half.e
T
Replacing an entry in an array implementation of a list is asymptotically faster than in a linked representation.
T
The partition procedure in quicksort places the pivot in the position that it will occupy in the final sorted array.
T
ll subtrees in a full binary tree are full binary trees.
T
BST add
always added to a leaf