Heaps
๐ for Heap definition
๐refresher on Binary Trees
Binary Trees
- Trees where each node only has up to 2 children
- "Complete" tree = every level filled; last level filled from left to right
- "Full" tree = No node has 1 child.
- "Perfect" = Full + Complete
Binary Search Trees
- All left descendents <= node value < all right descendents
Did you know
- Heaps are used to execute Javascript!
- Async calls are inserted on to the heap and popped off based on priority
- Read about JS Memory Leaks
- and profiling the JS Heap
- React 16's performance also derives from moving from a stack to a pqueue
React 16
React 15
JS Heap in Chrome
Heap Definition
- Complete Binary Trees
- A type of priority queue
- Satisfies the heap property, either a Min-Heap or Max-Heap.
- In a Max-Heap, if P is a parent node of C
- then P >= C
- compare vs BSTs:
- No idea if C1 < C2
- Top node is max/min value
- There are non-binary heaps
๐ for Heap methods
๐ vs other Data Structures
BSTs
- Insert: O(lg N) avg
- Contains: O (lg N) avg
- Remove: O(lg N) avg
- getMin: O(lg N)
- in/post/preordered traversal
Heaps
- Insert: O(lg N) avg
- Contains: O (lg N) avg
- Remove: O(lg N) avg
- getMin: O(1)
- ordered traversal not possible
PQueues (Linked List)
ย
- Insert: O(N)
- Contains: O(N) avg
- Remove: O(1) avg
- getMin: O(1)
- basically insertion sort
Heaps
- Insert: O(lg N) avg
- Contains: O (lg N) avg
- Remove: O(lg N) avg
- getMin: O(1)
What else?
I can't do the full comparisons justice
Key Methods
(and their Complexity)
- peek: O(1)
- remove: O(lg N)
- insert: O(lg N)
- Internal Methods:
- Swap(i, j): O(1)
- SiftDown(i): O(lg N) - used by remove
- SiftUp(i): O(lg N) - used by insert
๐ Practice Time!
๐ pseudocode for methods
Sift-Up
Example:
- Given a valid Max-Heap, insert 79 at the end (of the array/heap)
- Compare to 79's parent
- Swap up if 79 > 79's parent
- repeat until 79 < parent
- You now have a new, valid Max-Heap with 79 in it
Sift-Down
Example:
- Given a valid Max-Heap, remove the max (91) and pull the last node (24) into the root
- Now the Heap is invalid, 24 is less than 76 or 85
- Swap down 24 with the bigger of the two children (85, then 53)
- Repeat until you have a valid Max-Heap again, with 91 popped off.
Practice!
It's tricky the first time
๐ Why Use A Heap
Review: Why Use A Heap
- O(1) Retrieval of Max or Min
- O(1) Space complexity (same as Trees)
- O(lg N) Insertion/Removal
Used in these Algorithms:
- Heap Sort (great big O properties)
- no O(n^2) worst case eg QuickSort
- O(1) space compared to MergeSort O(n)
- Continuous Median
- (if sparse) Dijkstra's Algorithm
๐ for End of Slides/More Resources
๐Heap Sort and Continuous Median
Heap Sort
Given an unsorted array, produce a sorted array using heap sort
ย
First try doing it on top of a complete Heap Sort implementation like here:
https://repl.it/@swyx/Heaps-Basic-Solution
Then practice writing from scratch
Solution
- Subdivide into unsorted (A) and an empty (B)
- Put A into a Max-Heap
- While A is not empty, pop off the root of A and unshift it into B
- B is the sorted array
ย
Notes
- Sorting done in-place
- Video
Continuous Median
Write a class that can support the following two functionalities: 1) the continuous insertion of numbers and 2) the instant (O(1) time) retrieval of the median of the numbers that have been inserted thus far.
Solution
- Class has a maxHeap, a minHeap, and a median
-
insert by comparing with max of minHeap or min of maxHeap
- then rebalance
- then update median
-
rebalance by comparing lengths
- e.g. minHeap > maxHeap
- x = minHeap.remove()
- maxHeap.insert(x)
-
update median
- if equal length, get average
- if unbalanced, peek longer
Dijkstra's Algo
- Find the shortest path between a and b.
- It picks the unvisited vertex with the lowest distance
- calculates the distance through it to each unvisited neighbor
- updates the neighbor's distance if smaller.
- Use a pQ to keep track of vertex with lowest distance
- LList if not sparse
- Heap if E << Vยฒ / logV
- Especially Fibonacci Heap
- see wiki for pseudocode
Other Notes
- Here
-
DecreaseKey is tricky
- either write it
- or keep a hash table
More Resources
- Priority Queue Reacto (all Reactos)
- algoexpert.io (code: 'impostor')
- More Heap Presentations
- Gayle McDowell (CTCI Author) on Heaps
- Heap Variants on Wikipedia
- Related DSes
- Red-Black Trees (self balancing BSTs)
Heaps - The Data Structure and How They Are Used
By Shawn Swyx Wang
Heaps - The Data Structure and How They Are Used
Heaps - The Data Structure and How They Are Used
- 2,987