Heap
how to dequeue (deleteMin or deleteMax)?
1. extract the element from the root 2. put the element from the last leaf in it's place 3. remove the last leaf 4. move down the element in the root
how does heap sort work?
1. heapifies through the list of numbers 2. the element at the beginning of the list is then swapped with the element at the end of the list. the list is made 1 element shorter. the new list is then heapified 3. the process continues until the entire list is sorted
implement movedown() using an array
Assume the height of the node is defined as steps it needs to reach the leaf level. - For a complete tree of height h (perfect with total nodes 2 h-1), the sum of the total heights of the nodes is 2 h-1-h. Example: h = 3, nodes 7 • Total heights: 2 + 1 + 1 = 4 (= 7-3) - Since every node at most moves down its height, the total number of swaps is linear to the total number of nodes! - It is an upperbound for trees that are not perferct
what does moveDown() do?
For i = index of the last nonleaf node down to 0 moveDown(data, i, n-1) it goes through the data structure
Where is moveDown() used?
Its used in the bottom-up approach (heapify)
what is the average case of the two approaches (bottom-up and top-down)?
O(n)
is bottom-up or top-down a linear algorithm?
The complexity of heapify an array in a bottom-up way is this type of algorithm with a worst case of O(n)
what is the time complexity of movedown()?
The worst-case is O(logN) . The average time is also O(logN).
Min Heap
a binary tree. The value of each node is less than the values stored in each of its children. the tree is perfectly balanced, and the leaves in the last level are all in the leftmost positions
Heap (max heap)
a binary tree. the value of each node is greater than or equal to the values stored in each of its children (heap-order property). the tree is perfectly balanced, and the leaves in the last level are all in the leftmost positions
Heap definition
a data structure that uses a priority queue
is bottom-up or top-down faster?
bottom-up is faster
how to enqeue (insert?)
into a max heap... Insert(e) - Put e at the end of heap - while e is not in the root ande > parent(e) • Swap e with its parent
bottom-up approach (heapify)
pretend it's a heap and fix the heap-order property. start from the last nonleaf node [n/2-1] to root (n is the number of nodes). Move down (i.e. check if it is less than one of its children and repeat until reach the leaf)
top-down approach (build heap)
repeatedly perform enqueue().
why use heap to implement a priority queue?
the heap data structure makes priority queue operations all very efficient. (efficiency)
heap-order property
the value of each node is greater than or equal to the values stored in each of its children
is bottom-up or top-down slower?
top-down is slower
heap sort complexity
worst case: O(NlgN) best case: O(N)
time complexity of moveDown() (utilizes bottom-up complexity)
worst case: O(logN) average time is also O(logN)
bottom-up complexity when (Complexity of heapify an array in bottom-up way)
worst case: O(n)
Top-Down complexity
worst case: O(nlgn) Enqueue() is O(logn) worst case. So: building heap: O(nlgn)