DAST Tirgul 4 Definitions Binary tree A tree
DAST Tirgul 4
Definitions • Binary tree – A tree in which every node has either 0, 1 or 2 children.
Definitions • Full binary tree – A binary tree in which every node has either 0 or 2 children. No node has one child.
Definitions • Complete binary tree – A full binary tree in which all leafs are in the same depth.
Definitions • Nearly Complete binary tree – A binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Attributes • The number of leaf nodes in a complete tree of height h is 2 h. • Proof: In exercise 3.
Attributes • The number of internal nodes in a complete tree of height h is 2 h-1. • Proof: In exercise 3.
Attributes • The number of nodes in a complete binary tree of height h is 2 h+1 -1. • Proof: the number of leaf nodes is 2 h, the number of internal nodes is 2 h-1. Thus the number of nodes is 2 h+2 h-1=2 h+1 -1
Heap - Definition • A nearly complete binary tree that holds the max heap property. • The max heap property: the value of every node is larger or equal to the value of its children.
A max-heap viewed as a binary tree 1 and an array 16 3 2 14 4 8 2 8 9 4 5 7 1 6 9 10 7 3 10 What operation execute? 16 14 10 8 can a 7 heap 9 help 3 us 2 4 1
HEAP-EXTRACT-MAX(A) Removes and returns element with largest key 1. if heap-size < 1 error "heap underflow" 2. 3. 4. 5. 6. max ← A[1] ← A[heap-size] heap-size ← heap-size - 1 MAX-HEAPIFY(A, 1) return max Fixes the heap. Why does it need fixing?
MAX-HEAPIFY • When MAX-HEAPIFY is called, it is assumed that the binary trees rooted at LEFT(k) and RIGHT(k) are max-heaps, but that A[k] may be smaller than its children, thus violating the max-heap property. • The function of MAX-HEAPIFY is to let the value at A[ k ] "float down" in the maxheap so that the subtree rooted at index k becomes a max-heap.
MAX-HEAPIFY(A, k) 1. l ← LEFT(k) 2. r ← RIGHT(k) 3. if l ≤ heap-size[A] and A[l] > A[k] largest ← l 4. else largest ← k 5. if r ≤ heap-size[A] and A[r] > A[largest] largest ← r 6. if largest ≠ k exchange A[k] ↔ A[largest] 7. MAX-HEAPIFY(A, largest)
Max 1 Heapify Example 16 3 2 4 4 8 2 14 9 8 5 7 1 6 9 10 7 3 1 10 16 3 2 4 8 2 4 14 9 8 5 7 1 10 6 9 10 7 3
Max Heapify Example 1 16 3 2 4 8 2 8 4 14 9 4 8 5 7 1 10 6 9 10 7 3
HEAP-EXTRACT-MAX • The running time of HEAP-EXTRACTMAX is O(log n), since it performs only a constant amount of work on top of the O(log n) time for MAX-HEAPIFY. • We will give a correctness proof of heap max extract given the correctness of maxheapify (which you should prove in the exercise).
Correctness proof of EXTRACT-MAX • We need to prove two things: – The maximum is returned – The new heap remains valid • The root is the maximum – heap property. • After moving A[heap-size ] to A[1] and decreasing heap-size by 1, we have the following state: – both subtrees of the root’s children are legal heaps. WHY? – The only possible violation is at the root. • What This is exactly the assumption under which we algorithm can a heap help us execute? are allowed to call max-heapify !
Heap. Sort • Build a max-heap from the given array. • Starting with the root (the maximum element), the algorithm places the maximum element into the correct place in the array by swapping it with the element in the last position in the array. • “Discard” this last node (knowing that it is in its correct place) by decreasing the heap size, and calling MAX-HEAPIFY on the new (possibly incorrectly-placed) root. • Repeat this “discarding” process until only one node (the smallest element) remains, and therefore is in the correct place in the array.
Heap. Sort • • O(n log n) worst case—like merge sort. Sorts in place—like insertion sort. HEAPSORT(A, n) O(n) 1. BUILD-MAX-HEAP(A, n) 2. for i ← n downto 2 O(1) exchange A[1] ↔ A[i ] O(logn) 3. MAX-HEAPIFY(A, 1, i − 1) Total running time: O(nlogn)
Heapsort Example 1 16 3 2 14 4 8 2 8 9 4 5 7 1 10 6 7 1 3 9 14 10 3 2 8 4 8 2 4 9 1 5 7 16 6 9 10 7 3
Heapsort Example 1 9 10 3 2 2 8 3 9 7 6 We will see other uses of heap towards the end of the course 44 8 2 4 5 7 10 1414 1616 1 2 3
INCREASE-KEY • INCREASE-KEY increases the value of element x's key to the new value k, which is assumed to be at least as large as x's current key value. • increasing the key of A[ i ] may violate the max-heap property, the procedure then, traverses a path from this node towards the root to find a proper place for the newly increased key. • During this traversal, it repeatedly compares an element to its parent, exchanging their keys and continuing if the element's key is larger, and terminating if the element's key is smaller, since the max-heap property now holds.
HEAP-INCREASE-KEY(A, i, key) 1. if key < A[i] error "new key smaller than current key" 2. A[ i ] ← key 3. while i > 1 and A[PARENT(i)] < A[ i ] exchange A[ i ] ↔ A[PARENT(i)] 4. i ← PARENT(i)
Increase Key Example 1 16 3 2 14 4 8 2 8 9 4 5 7 1 6 9 10 7 3 10 1 16 3 2 4 8 2 8 14 9 15 5 7 1 10 6 9 10 7 3
Increase Key Example 1 1 16 3 2 4 8 2 15 8 14 99 15 8 5 7 1 10 6 9 10 7 3
Increase Key Example 1 1 16 3 2 44 8 2 14 15 9 88 5 7 1 10 6 9 10 7 3
Increase-Key vs. Max-Heapify • Increase-key deals with the situation in which the key of an element is larger than that of its parent. • Max-heapify deals with the problem in which the key of a parent is smaller than that of its children. • The two sound the same! • Can't we use just one of the two algorithms Both increase-key and max-heapify whenever we use the other one? solve some local problem in the heap, what exactly is the difference?
Increase-Key vs. Max-Heapify • The situation in max-heapify is always equivalent to: • there was a max-heap A, and one of its nodes was replaced by a smaller key. All other nodes remain the same. Correctness of max-heapify means that calling to it on that node will fix the heap. • The situation of increase key is different: we are given a heap, and for one node the key was replaced by something bigger. Correctness of increase key means that when we call to increase key on that node, this fixes the heap. • And so the two handle opposite scenarios: essentially, max-heapify can be viewed as 'decrease key". In the previous examples, if one of the procedures fixes the heap, the other cannot !
- Slides: 28