Data Structures Heaps
heap child indicies formula
2 * i + 1, 2 * i + 2
max-heap
A binary tree that maintains the simple property that a node's key is greater than or equal to the node's children's keys A max-heap's root must have the maximum key.
min-heap
A min-heap is similar to a max-heap, but a node's key is less than or equal to its children's keys. Online tech support waiting lines low # = top priority
PushWithPriority
A push operation that includes an argument for the pushed item's priority. PushWithPriority(Object A, 3)
Max-heap remove
A remove from a max-heap is always a removal of the root, and is done by replacing the root with the last level's last node, and swapping that node with its greatest child until no max-heap property violation occurs. Because upon completion that node will occupy another node's location (which was swapped upwards), the tree height remains the minimum possible.
Max-heap insert
An insert into a max-heap starts by inserting the node in the tree's last level, and then swapping the node with its parent until no max-heap property violation occurs. Inserts fill a level (left-to-right) before adding another level, so the tree's height is always the minimum possible. The upward movement of a node in a max-heap is sometime called percolating.
Heap storage
Heaps are typically stored using arrays. Given a tree representation of a heap, the heap's array form is produced by traversing the tree's levels from left to right and top to bottom. The root node is always the entry at index 0 in the array, the root's left child is the entry at index 1, the root's right child is the entry at index 2, and so on.
Heapsort overview
Heapsort begins by heapifying the array into a max-heap and initializing an end index value to the size of the array minus 1. Heapsort repeatedly removes the maximum value, stores that value at the end index, and decrements the end index. The removal loop repeats until the end index is 0.
Heapsort
Heapsort is a sorting algorithm that takes advantage of a max-heap's properties by repeatedly removing the max and building a sorted array in reverse order. An array of unsorted values must first be converted into a heap. The heapify operation is used to turn an array into a heap. Since leaf nodes already satisfy the max heap property, heapifying to build a max-heap is achieved by percolating down on every non-leaf node in reverse order. switch max node (root) with smallest node remove max node (which is in small node's index) since it's not max heap now, do heapify repeat
Pop is worst-case O(logN) and Peek is worst-case O(1).
Pop is worst-case O(logN) and Peek is worst-case O(1).
A priority queue implementation that does not require objects to have a data member storing priority would implement the _____ function.
PushWithPriority
A priority queue is always implemented using a heap
false
heap parent index formula
floor((i - 1) / 2)
Largest internal node index
floor(N / 2) - 1
Given N nodes, what is the height of a max-heap?
logN
what is the worst-case complexity of an insert, complexity for removing root
o(logN)
A priority queue implementation that requires objects to have a data member storing priority would implement the _____ function.
push
When implementing a priority queue with a heap, no operation will have a runtime complexity worse than O(logN).
true