C949 Heaps
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.
Heapsort overview
1. Heapifying array into a max-heap and initializing an end index value to the size(array) - 1 2. Heapsort repeatedly removes max value, stores value at end index, and decrements the end index. 3. Removal loop repeats until the end index is 0. End index value = size of the array minus 1 o Heapsort repeatedly: → until the end index is 0 ▪ removes the maximum value ▪ stores that value at the end index ▪ decrements the end index
Max-Heap: Insert
1. Inserts the node in the tree's last level 2. Swap node w/parent (left-right) until no property violation Complexity: O(logN) [log2N]
Max-Heap Percolate Down Algorithm
Accesses array at nodeIndex in second statement, so index must be < array size. - All children checked before any child key is moved into parent. → If child > parent's key, max child key is moved up into parent.
Max-Heap: Remove
Always a removal of root 1. Replacing root w/last level's last node 2. Percolate down: swapping node w/greatest child until no max-heap property violation occurs -BC upon completion node will occupy another node's location (swapped upwards), tree height remains min as possible Complexity: O(logN) [log2N] - Root replaced by a last-level node, then downward swaps may occur, limited by height,
Max-Heap Percolate Up Algorithm
Percolating up on the root is a valid call - Alg. terminates immediately once node index is checked in first statement. - Node index int value - Parent index computed as (nodeIndex - 1) / 2 → implying that parent index will always be >= 0.
Max-Heap
Binary tree where node's key >= to the node's children's' keys (execute in order of max priority) - Can be any tree, commonly binary tree - Root has max key in the entire tree - Fast access to and removal of max item - Used to sort array (sorted array may not be max-heap)
Python: Heaps and MaxHeap Class
Max-heap tree grows left to right until entire level full - Non node class is used parent_index = (node_index - 1) // 2 → (//: decimal portion, if any, is dropped.) left_child_index = 2 * node_index + 1 right_child_index = 2 * node_index + 2
Python: Heap Percolate Down
MaxHeap methods insert() and remove() make use of percolate_down() method
Python: Heap Percolate Up
MaxHeap methods insert() and remove() make use of percolate_up() method
Heap Implementations: Parent and Child Indices
Must check if a node is leaf node before using child index formula. - Formula always gives appropriate child indices, but check to see if such children exist in heap must be done before utilizing the formula.
Heap Operations Runtime Complexity
O(logN) or log2N
Parent and Child Index Formulas for a Heap
Parent Index: floor((i - 1) / 2) - Does not work on root node Child Indices: 2 * i + 1, 2 * i + 2 - Does work on root node *Round down
Min-Heap
Similar to a max-heap, but a node's key is <= to its children's keys Webpage cache → Provides fast way to find page w/oldest timestamp (serves as the key) - Newer pages > older pages, Since timer's output value keeps increasing - Node w/min time value is sought for oldest page - Oldest dumped if the cache is full
Heapsort
Sorting algorithm takes adv of a max-heap's properties by repeatedly removing max and building a sorted array in reverse order - end index value = size(array) - 1 Removes the max value ▪ stores that value at the end index ▪ decrements the end index o worst-case runtime is O(N log N).
Heapsort Algorithm
Sorting algorithm that inserts the values to be sorted into a heap
Heap Storage Example
Traversing heap B level-by-level, top to bottom and left to right across each level gives: 57, 19, 42, 13, 6, 7, 15.
Heap Storage
Typically stored using arrays.; Array form produced by traversing tree's levels from left-right and top-bottom - Uses index instead of nodes or pointers The root node: index 0 in the array ▪ root's left child: index 1 ▪ root's right child: index 2
Percolating
Upward movement of a node in a max-heap
Heapify operation
Used to turn an array into a heap. →Each internal node is percolated down, from highest node index to lowest. Starts on the internal node with the largest index and continues down to root node at index 0. Largest internal node index: floor(N / 2) - 1 Ex: Array w/7 nodes, # of percolate-down ops to heapify the array? 3 (in 2, 1 and 0)