Final Exam Flashcards
How are items inserted and removed in a Stack?
LIFO
What methods does the Stack ADT support?
push() pop() isEmpty() top() size()
pro/cons of Array-based Stack
O(1) per operation, but size must have an upper bound
How are items inserted/removed in a queue?
FIFO
What methods does queue support?
enqueue() dequeue() isEmpty() front() size()
Pro/cons for array implementation of the queue
O(1) per operation
needs to have a fixed size
Methods that Index Based List supports
get(index)
set(index, element)
add(index, element)
remove(index)
Running times of array based index-based list
get - O(1)
set - O(1)
add - O(n)
remove - O(n)
Running times of various linked list methods
first() - O(1) last() - O(1) before() - O(1) after() - O(1) element() - O(1) remove - O(1)
Steps of MergeSort
1) Divide: sequence into 2 sequences
2) Recur: until all sequences have zero or one element
3) Conquer: merge the sequences all back together
Steps of QuickSort
1) Pick a pivot element
2) Sort list into 2 new lists less than pivot and greater than pivot
3) recur
4) conquer
What is a Priority Queue?
A container of elements, each with a key which determines the priority used to remove elements
Pros/Cons of unsorted vs sorted Priority Queue?
Unsorted: insert takes O(1) but remove takes O(n)
Sorted: insert takes O(n) time but remove takes O(1)
How to use a priority queue to sort elements?
Insert comparable elements one by one, and then remove them
PQ-sort where the priority queue is implemented with an unsorted sequence is like…
Selection sort!
PQ-sort where the priority queue is implemented with a sorted sequence is like….
Insertion-Sort!
A heap is what kind of tree
A complete binary tree
What is the Heap-Order Property?
for every node v other than the root, the key stored at v is geq than the key stored at v’s parent
What is a complete binary tree
A tree that fills from the left to the right
General idea of inserting into Heap
insert at first available spot, and then bubble up the tree
removing min element from a heap
remove root, select last element and put it at root position, and bubble down to restore tree
What is the height of a balanced tree?
log(n)
What is the worst case time of insertion sort?
O(n^2)
What is the worst case time of bubblesort?
O(n^2)