Priority queues/heaps (messaging queues)
peek vs poll
peek: returns value poll: returns and removes
heap with priority
priority = ANYTHING THAT IS COMPARABLE (not just doubles/ints) updatePriority uses HashMap
heap implements
priority queue (each heap node represents a priority of queue item)
how to build a heap from array
put first value, insert next in leftmost bottommost spot, bubble up...continue for all elements (O(nlogn), n elements, logn bubble up time) OR put first element as root and bubble down, this is O(n) because less operations overall
Kafka
queue system real time data feeds
how does heap peek work
return root (complexity is O(1))
heap complexity
methods size and peek take time O(1) for a heap of size n, while add and poll take time O(log n). **no linear time operations(better than lists) *peek is constant time, better than balanced trees
class MaxHeap add
/** Add e to the heap */ public void add(int e) { b[n]= e; n= n + 1; bubbleUp(n - 1); }
bubble down in MaxHeap
/** Bubble b[k] down to its heap position. Pre: b[0..n-1] is a heap except perhaps k */ private void bubbleDown(int k) { int h = 2k+1; //invariant: b[0...n-1] is a heap except perhaps k, and h is k's left child while (h < n) {//b[k] has a child b[h] //set uc to k's LARGEST child if b[k] >= b[uc] return; swap b[k] and b[uc] k = uc; h = 2k+1; } } **whereas for minheap, you would swap with smallest child!
class MaxHeap bubbleup
/** Bubble element #k up to its position. * Pre: heap inv holds except maybe for k */ private void bubbleUp(int k) { int p= (k-1)/2; // inv: p is parent of k and heap invariant // holds except maybe for k while (k > 0 && b[k] > b[p]) { swap b[k] and b[p] k = p; p = (k - 1)/2; } }
poll() in MaxHeap
/** Remove and return the largest element * (return null if list is empty) */ public int poll() { if (n == 0) return null; int v= b[0]; // largest value at root n= n - 1; // move last b[0]= b[n]; // element to root bubbleDown(0); return v;
peek() in MaxHeap class
/** Return largest element * (return null if list is empty) */ public int poll() { if (n == 0) return null; return b[0]; // largest value at root. }
heapsort algorithm
// Make b[0..n-1] into a max-heap (in place) // inv: b[0..k] is a heap, b[0..k] <= b[k+1..], b[k+1..] is sorted for (k= n-1; k > 0; k= k-1) { b[k]= poll - i.e., take max element out of heap. }
how heapsort works
1. turn array into heap (heapify) **using bubble up as you add left to right values from array into heap, note that it is a MAX heap 2. poll (placing polled values in array right to left, so the sorted result is ascending order left to right) IT IS INSITU (in place sorting!)
heap has
iterator, so for each loop works
priority of a priority queue must be
Comparable (interface)
priority queue
Each item in a priority queue has a priority. New items are simply added to the list, along with their priority. When removal is performed, the item with the HIGHEST priority is removed(if implemented with maxheap) or SMALLEST priority(if implemented with min heap).
The worst-case complexity of inserting an item into a heap containing N items is
O(log(N))
The worst-case complexity of removing an item from a heap containing N items is
O(log(N))
heapsort complexity
O(nlogn)
relationship between node k and its parent/children
The parent of node k is node (k -1)/2 (using Java's int division) The children of node k are nodes 2k+1 and 2k+2. **note that tree nodes are numbered top down, left to right, starting from 0 this is how you put nodes into arrays: node #0 goes in index 0
heap
a TREEEEEEEEEEEEEEEEEE(implemented as an ARRAY) that maintains a bag of integers with O(log n) insertion and deletion times for a heap of size n... A binary tree with 2 properties: 1. complete(so height is O(log(n)) 2. Heap-order invariant: For a max-heap, the value in every node <= ITS PARENT(not root). For a min-heap, the value in every node >= parent. **NOTE: that doesn't mean every level is less than or greater than other levels!!
backing heap with hashmap
a heap as an array is unsorted, but you can get its index in O(1) with a hashmap
queue
a list that supports insertion at the end and removal from the beginning. It's a First-In-First-Out, or FIFO, list. (remove oldest)
stack
a list that supports insertion on and removal from its top. It's a Last-In-First-Out, or LIFO, list. (remove newest)
adding value to heap
add to available spot to keep complete, then "bubble up" the value to the correct spot (takes O(log n))
heap primary operations
add(e): add a new element to the heap poll(): delete the max element and return it peek(): return the max element
pub-sub system
async communication tool publishers publish to a queue or some other system subscribers can read from it
priority queue methods (times below are IF priority queue is implemented with a heap)
boolean add(E e) //insert e, expected: O(log n)= height of heap...best case is O(1), worst: O(n), if u have to check that e isn't already in there boolean add(e, p) // add item e with priority p E peek() // return item with minimum priority E (constant time) E poll() // remove AND return item with min priority Ologn...if backed with hashmap, poll may be O(n) worst case (in case all hashes are the same so linked list...) void clear() // remove all items boolean contains(E e) //linear time ... array form boolean remove(E e) //linear time ... array form because u have to SEARCH for element O(n) then bubble up so O(log n) which in total is still O(n) void updatePriority(e, p) // change e's priority to p: expected is O(log n)*to bubble up and best is O(1)...worst case may be O(n) if backed by hashmap (cuz you get a value, hashmap may have degraded to linked list) int size() //constant time
class MaxHeap constructor
class MaxHeap { int[] b; // heap is b[0..n-1] int n; /** Create max-heap with max size m */ public Heap(int m) { b= new int[m]; // n == 0, so heap invariant holds // (completeness & heap-order) } }
sorting heap as an array
do breadth order (root is index 0, then at each level, left to right: 1, 2, ...etc)
polling heap
getting max or min value, with steps: (1) save the root value in a variable. (2) Swap the root with the last node and remove the last node from the tree. 3) bubbling down (to the largest of left or right subtree values!) Takes O(log n) time
It is possible to change the priority of any node in a heap (as implemented in A5) in expected time O(log n)
true (note that it says EXPECTED time)
Class PriorityQueue
uses a heap and no other data structure. Therefore, methods contains and remove take time proportional to the size of the heap. **Also, method add(E e) has no parameter for priority because Type E has to implement interface Comparable, and its method compareTo is used to determine the ordering of items in the queue; OR (2) When creating a PriorityQueue object, one can give the comparator as an argument of the call to the constructor.
can a heap have duplicates
yes