Download presentation
1
Binary Heap
2
Binary Heap 1 16 3 2 14 10 6 4 5 7 8 7 9 3 8 9 10 2 4 1
3
Binary Heap A binary heap is defined to be a binary tree with a key in each node such that: All leaves are on, at most, two adjacent levels. All leaves on the lowest level occur to the left, and all levels except the lowest one are completely filled. The key in root is greater or equal to all its children, and the left and right sub trees are again binary heaps.
4
Binary Heap A heap will often be implemented using an array. Using such implementation requires an additional field, the heap size which is at most the array size. Using this implementation we should be able to find the parent, left and right sons of a given node
5
Parent, left and right son
If a node is at index n it’s parent will be indexed n/2 If a node is at index n it’s left son would be indexed at 2n If a node is at index n it’s right son would be indexed at 2n + 1
6
The Heap Property The value of each node is at least the value of it’s sons. Therefore the largest value of the heap is the root of the heap Every sub tree in the heap is also a heap The maximal height of a heap is log(n)
7
Heapify (A,2) 16 16 4 10 14 10 14 7 9 3 4 7 9 3 2 8 1 2 8 1
8
Heapify (A,4) 16 16 14 10 14 10 4 7 9 3 8 7 9 3 2 8 1 2 4 1
9
Heapify (A,1) The node 3 is replaced with 16, then with 12, and then with 8.
10
The Heap Property The ancestor relation in the heap defines a partial order on the heap elements Reflexive: x is an ancestor of itself. Anti-symmetric: if x is an ancestor of y and y is an ancestor of x, then x=y. Transitive: if x is an ancestor of y and y is an ancestor of z, x is an ancestor of z
11
The Heap Property The partial order defined by the heap structure is weaker than that of the total order, therefore 1. A heap is easier to build. 2. A heap is less useful than sorting (but still very important
12
Constructing a Heap Heaps can be constructed incrementally, by inserting new elements into the left-most open spot in the array. If the new element is greater than its parent, swap their positions and recur. (propagate up) Since at each step, we replace the root of a sub tree by a larger one, we preserve the heap order.
13
Constructing a Heap Doing n such insertions takes , since the last n/2 insertions require time each. Note that all the levels except the last must be full.
14
Constructing a heap Another way to construct a heap, if all items are known in advance is using the build-heap algorithm. Build-Heap(A) heap-size[A] length[A] for i length[A]/2 down to 1 do heapify (A,i)
15
Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 4 1 3 1 3 2 16 9 10 2 16 9 10 14 8 7 14 8 7
16
Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 4 1 3 1 3 2 16 9 10 14 16 9 10 14 8 7 2 8 7
17
Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 4 1 3 1 10 14 16 9 10 14 16 9 3 2 8 7 2 8 7
18
Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 4 1 10 16 10 14 16 9 3 14 7 9 3 2 8 7 2 8 1
19
Build-Heap {4,1,3,2,16,9,10,14,8,7} 16 4 14 10 16 10 8 7 9 3 14 7 9 3 2 4 1 2 8 1
20
Constructing a heap The build heap algorithm is a merge algorithm, whenever heapify on a node is called, both its sub trees are already heaps Rough Analysis of build heap produces a running time of O(nlogn). However careful analysis will show that in fact it is a linear algorithm
21
Build heap analysis A heap of size n has nodes at height h
So there are n/2 nodes which are leaves (height 0) n/4 nodes of height 1 and finally a single node of height log(n)-1
22
Build heap analysis Obviously, the lower the height, the less time the heapify will execute. At each height h, heapify runs in time O(h). So the total running time of build heap is
23
Build heap analysis From the formula we assign x = ½
therefore the running time of build heap is bounded in linear time.
24
Build-Heap Recursive version
Build-Heap(A,i) if i > heapSize return Build-Heap(A,2i) Build-Heap(A,2i+1) Heapify (A,i)
25
HeapSort The largest element in the heap is at the the root of the heap. We can place it in its final location by exchanging it with A[n]. If we delete the largest element from the heap (and decrease the heap size), the children of the root remain heaps, but the root may violate the heap property. We correct with heapify and continue with a smaller heap.
26
HeapSort HeapSort(A) Build-Heap(A) for ilength[A] downto 2
do exchange A[1] A[i] heapSize - - heapify(A,1) Heapsort is an O(nlogn) sorting algorithm
27
Heap VS Array Both selection sort and heap sort do the same operations: For i = 1 to n 1. Find the largest element 2. Pull it out and place it in its final location Selection sort takes O(n) time for step 1 and O(1) for step 2, while using heaps both operations take O(logn) time
28
Priority Queue A priority queue is a data structure that supports the following operations: Insert(S, x) - insert x into set S Maximum(S) - return the largest key in S ExtractMax(S) - return and remove the largest key in S These operations can be easily supported using a heap.
29
Maximum The largest element in the heap is the root , therefore this method simply returns A[0].
30
ExtractMax Move the last leaf to be the root, decrease the heap size, and heapify the root. Extract-Max(A) A[1] A[heapSize] heapSize-- heapify(A,1)
31
Insert(x) Insert (A, x) heapsize ++ A[heapSize] x fixup(A, heapSize)
Fixup (A, i) while(A[parent(i)] != null and A[parent(i)] < A[i]) swap (A[parent(i)],A[i]) i = parent(i)
32
Example Insert the element 101 into the heap {100,15,99,7,3,98,1,4,5,1,2,70} Delete the maximum element from the resulting heap
33
Exercises Is an array that is in reverse sorted order a heap?
Where in the heap might the smallest element be? What is the minimum and maximum numbers of elements in a heap of height h?
34
Exercises Which of the following arrays have the heap property?
{88,66,44,33,55,77,33} {88,77,66,55,44,33,22} {88,44,77,22,33,55,66} {88,77,55,44, ,33,22} {88,66,77,22,33,44,55} {88,77,22,33,44,55,66}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.