Wk 3 Heaps, Priority Queues, Heapsort Flashcards
Why does a data structure exist?
It exists to support operations. They aren’t created in isolation.
Does the complexity of a data structure matter? Why?
Yes, essentially a data structure’s operations are an algorithm to access the data
What kind of data (dynamic or static) is best suited to a data structure?
Dynamic. If you’re not changing anything much, who cares how you’re storing and accessing. The concept of a data structure implies you need to operate on it.
What are some ways to access data in a priority queue?
Time of Arrival (stack, queue) but could also be some key that is the priority.
How does whether or not a list is sorted affect the insert and extract operations complexity? Which is better for which? What is the average overall.
A sorted list is constant extract, but linear insert.
An unsorted list is constant insert, but linear extract.
Both of these overall, assuming equal numbers of add and remove is Theta(n).
Why would we want a lg n algorithm for a priority structure?
We take a hit on either the insert or delete, but overall we go to lg n. Heapsort does this.
What op’s does heapsort support?
insert, extract max, delete
When would I NOT want to use a heap?
If we never need to extract min/max and we know we are predisposed to do more inserts or more removes. In that case, sorted/unsorted array/linked list would make sense
What exactly is a heap. How is it implemented?
a heap is a binary tree, implemented as an arrary. For each node, the parent is larger than the children. Siblings have no relationship. The last level is left-filled and other levels are full.
What is a full, complete binary tree?
full = every layer filled to the bottom. complete = last layer full. Heap is full, but not necessarily complete ??–i think….
What is the height of a binary tree?
lg n
How do I find the left, right, and parent of a heap implemented as an array?
L = 2i R = 2i+1 P = i/2 (floor)
How does Heapify work? How long does it take? Why does it take that long?
Heapify assumes left is a heap and right is a heap and that the violator is the root. It swaps the root with the greater of the children, then recurses down to fix that child recursively. This cost is lg n. In the worst case, we have to walk the entire height of the tree, which is lg n.
How do we run extract max from a heap? How long does it take?
We pop the root off, since we know its the max. Then we put the last element at the root and run heapify. Taking the root is constant, and heapify takes lg n, so overall this is a lg n algorithm.
How does heapsort compare to quicksort? What are the differences? Why do we like one versus the other?
Both are n lg n. The difference is that heapsort is always n lg n whereas quicksort is n^2 in the worst case. We like quicksort because it’s avg performance is better than heapsort (less constant time?) but heapsort will never worst case surprise you.