Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

34
Binary Heap

Transcript of Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Page 1: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Binary Heap

Page 2: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Binary Heap

16

14 10

3 8 7 9

2 4 1

1

2 3

4 5 6 7

8 9 10

Page 3: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Binary Heap

• A binary heap is defined to be a binary tree with a key in each node such that:

1. All leaves are on, at most, two adjacent levels.

2. All leaves on the lowest level occur to the left, and all levels except the lowest one are completely filled.

3. The key in root is greater or equal to all its children, and the left and right sub trees are again binary heaps.

Page 4: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

Page 5: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

Page 6: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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)

Page 7: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Heapify (A,2)

16

4 10

3 14 7 9

2 8 1

16

14 10

3 4 7 9

2 8 1

Page 8: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Heapify (A,4)

16

14 10

3 4 7 9

2 8 1

16

14 10

3 8 7 9

2 4 1

Page 9: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Heapify (A,1)

• The node 3 is replaced with 16, then with 12, and then with 8.

3

16 9

5 7

0 6 8 2 4

12 10

Page 10: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

The Heap Property

• The ancestor relation in the heap defines a partial order on the heap elements

1.Reflexive: x is an ancestor of itself.

2.Anti-symmetric: if x is an ancestor of y and y is an ancestor of x, then x=y.

3.Transitive: if x is an ancestor of y and y is an ancestor of z, x is an ancestor of z

Page 11: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

Page 12: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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.

Page 13: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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.

logn n

log n

Page 14: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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)

Page 15: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Build-Heap {4,1,3,2,16,9,10,14,8,7}

4

1 3

10 2 16 9

14 8 7

4

1 3

10 2 16 9

14 8 7

Page 16: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Build-Heap {4,1,3,2,16,9,10,14,8,7}

4

1 3

10 2 16 9

14 8 7

4

1 3

10 14 16 9

2 8 7

Page 17: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Build-Heap {4,1,3,2,16,9,10,14,8,7}

4

1 3

10 14 16 9

2 8 7

4

1 10

3 14 16 9

2 8 7

Page 18: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Build-Heap {4,1,3,2,16,9,10,14,8,7}

4

1 10

3 14 16 9

2 8 7

4

16 10

3 14 7 9

2 8 1

Page 19: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Build-Heap {4,1,3,2,16,9,10,14,8,7}

16

14 10

3 8 7 9

2 4 1

4

16 10

3 14 7 9

2 8 1

Page 20: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

Page 21: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

12h

n

Page 22: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

log1

0

log

0

/ 2 ( )

( / 2 )

nh

h

nh

h

n O h

O n h

Page 23: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Build heap analysis

• From the formula

we assign x = ½

therefore the running time of build heap is bounded in linear time.

20 (1 )

k

k

xkx

x

20

1/ 22

2 (1 1/ 2)hh

h

log

0

( / 2 ) ( )n

h

h

O n h O n

Page 24: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Build-Heap Recursive version

• Build-Heap(A,i)if i > heapSize

returnBuild-Heap(A,2i)Build-Heap(A,2i+1) Heapify (A,i)

( ) 2 ( / 2) (log ) ( )T n T n O n n

Page 25: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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.

Page 26: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

Page 27: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

Page 28: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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.

Page 29: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Maximum

• The largest element in the heap is the root , therefore this method simply returns A[0].

Page 30: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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)

Page 31: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Insert(x)

• Insert (A, x)heapsize ++

A[heapSize] xfixup(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)

Page 32: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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

Page 33: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

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?

Page 34: Binary Heap. 16 14 10 3 8 79 2 41 1 2 3 45 6 7 89.

Exercises

1. Which of the following arrays have the heap property?

a. {88,66,44,33,55,77,33} b. {88,77,66,55,44,33,22}c. {88,44,77,22,33,55,66}d. {88,77,55,44, ,33,22}e. {88,66,77,22,33,44,55}f. {88,77,22,33,44,55,66}