CS 0445 Final
Full Tree Height
2^(h) - 1
Complete Binary Tree
2^(h-1) or 2^(h) - 1
A full binary tree of height h has how many nodes?
2^h-1
An Iterator for a class in Java is limited in that it is dependent on the public methods of the class and thus cannot be tailored to the implementation details of the class.
FALSE - Iterators are implemented so that they have access to the internal details of the classes.
The worst case scenario for Insertionsort is the same whether the items are in an array or in a linked list.
FALSE - for array it is reverse sorted data, for LL it is sorted data.
Deleting the root of a MaxHeap involves simply removing the root node and moving the greater of its children nodes up the root position.
FALSE - it is more complicated than this
The Simple Quicksort algorithm has both a worst case runtime and an average case runtime of O(N2).
FALSE - the average case is O(NlgN)
The Median-of-Three version of QuickSort eliminates the worst case behavior of the simple version.
False - it only makes the worst case less likely.
The 8-Queens Problem and the Regular Expression Matching method from Assignment 3 are examples of divide and conquer algorithms.
False - these are backtracking but not divide and conquer.
If we use the same operations listed in a) above, but with a LinkedList rather than an ArrayList as the data, will this be a good implementation? Explain why or why not in detail.
It depends on how the LinkedList is implemented. If the LinkedList has a Rear or Tail reference (or is circular or doubly-linked) both add() and remove(0) can be easily done in O(1) time and the implementation is good. However, if only a Front reference is available, the add() requires a traversal of the list, making it an O(N) operation.
The Java _____ interface simply requires that any implementary class have the standard iterator() method. This interface allows an implementary class to interate with a standard Java for loop
Iterable
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.
head n ass and n removeMax
N x lgN = O(NlgN)
A binary tree with n nodes has a maximum height of and a minimum height of (approximately)
N, log2N
Consider the power function (X^N) as discussed in lecture. The simple recursive algorithm for this problem will do ______ multiplications and the divide and conquer algorithm for this problem will do ______multiplications.
N, log2N
The get(i) method for the Java ArrayList runs in time ____and for the Java LinkedList runs in time ____
O(1), O(N)
________tries to improve on InsertionSort by sorting items that are K locations apart (where K gradually decreases down to 1).
ShellSort
Consider a Binary Search Tree with N nodes and the add() method (i.e. an Insert into the tree). Discuss in detail how long this method will take in terms of N in both the average and worst cases. Thoroughly explain your answers
The run-time for the add() method of the BST is dependent upon the height of the tree. This is because the algorithm starts at the root and proceeds to a null reference (after a leaf) before inserting the new node. In the average case, a BST with N nodes will be relatively balanced, having a height of O(log2N). However, in the worst case, the height can be linear (O(N)), thus causing a very poor run-time for add()
A full binary tree with a height of h will have 2h - 1 nodes.
True
Extra overhead is associated with the MergeSort algorithm because _______
a temporary array is used for merging, and the data must be copied back from the temporary array
priority queue sorted array
add in correct sport o(lgN) to find, o(n) to shift, peek(), remove() from end
priority queue unsorted array
add() at the end o(1), peek() o(n), remove O(n)
In the 8 Queens problem, if a queen cannot be placed onto column j, the algothrim will _______ to column j-1 to move the queen in that colum
backtrack
the "work" for Quicksort is done ____ the recursive call and the "the work for MergeSort is done _____ the recursive call
before, after
We discussed our divide and conquer sorting algorithms by answering two questions:
divide the problem into subproblems; use the subproblem solutions to determine the overall solution?
a disadvantage of iterators as we discussed is that we can only have a single active iterator on a list at a time
false; an advantage is that we can have multiple active iterators at a time
In the simplest quicksort algothrim, having previously sorted dated in the array is a best case solution
false; it is the worst case
An example divide and conquer approach to solving a problem of size n would require solving an identical problem of size n-1
false; the size must be reduced by some fraction of the original size
QuickSort is_____ and MergeSort is a _____sort
not a stable, stable
queue
o(1) best case
bst find insert delete
o(N) worst case. o(log2n) average
heap amortized
o(lgN)
heap add removeMax()
o(lgN) worst case
balanced Bst getentry, add, remove
o(log2N)
BST Search Time
o(log2n)
array find (binary search)
o(log2n) average
ll search time
o(n)
very balanced
o(n)
When a backtracking algorithm backtracks, an activation record is ____the run-time stack.
popped off
_________is a famous problem discussed in lecture that involves recursion and moving disks of decreasing size from one peg to another
the towers of hanoi problem
A Java iterator allows the implementation of an iteration to be tailored to the data structure upon which it is iterating.
true
Insertionsort, selectionsort, and bubblesort all have the worst and average case run time of O(n^2)
true
Binary Tree Min Height
O(log2N)
array add(), remove()
O(n)
A tree node that has a parent and at least one child is called a(n) _____ node.
interior
BubbleSort Big O
O(N^2)
Implementing a Priority Queue using a Heap allows the add() and remove() methods to be executed in _____time .
O(lgN)
based on our definitions from lecture, any full binary tree is called a complete binary tree
true
for any node V in a tree, the subtree rooted a v is v and all of its decendents
true
A Stack organizes data by _____ _________ and a Queue organizes data by ______
Last in first out, first in first out
One option we discussed was to use an ArrayList and simply do add() for enqueue and remove(0) for dequeue. Is this a good implementation? Explain why or why not in detail.
No, it is not, because remove(0) on an ArrayList requires the shifting of the remaining items to the right to fill the gap. This causes remove(0) to take O(N) time when it should only take O(1) time.
Selection Sort Big O
O(N^2)
MergeSort Big O
O(Nlg2N)
The simple QuickSort algorithm that we discussed (from Quick.java) has a best case Big O run-time of _______and a worst case run-time of ______
O(Nlog2N), O(N^2)
InsertionSort Big O
O(N^2) is the worst case and average case. O(N) is the base case
Quicksort Big O
O(N^2) worst case; O(Nlg2N) is expected
ShellSort Big O
O(N^3/2)