0% found this document useful (0 votes)
9 views49 pages

Lecture 12 - Heaps

The document provides an overview of binary heaps, detailing their properties, array implementation, and operations such as insertion, deletion, and key adjustments. It explains the complexities associated with these operations, which generally are O(log N) for most actions. Additionally, it discusses two algorithms for building heaps, one with a complexity of O(N log N) and another with O(N).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views49 pages

Lecture 12 - Heaps

The document provides an overview of binary heaps, detailing their properties, array implementation, and operations such as insertion, deletion, and key adjustments. It explains the complexities associated with these operations, which generally are O(log N) for most actions. Additionally, it discusses two algorithms for building heaps, one with a complexity of O(N log N) and another with O(N).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Faculty of Engineering, Assumption University

CE2102: Data Lecture 12: Heaps


Structure & Algorithms
What is a Binary Heap?
❖ A binary heap is a partially sorted binary tree
❖ Property 1: An almost complete binary tree – the
bottommost is filled from left to right
❖ Property 2: Value of an element at a node is less
(greater) than or equal to that of its descendants
❖ The root is the minimum (maximum)
❖ Its height is O(log N)
Example: Binary Min Heap
3

25 10

29 41 21 47

31 33 44

0 1 2 3 4 5 6 7 8 9 10 11

3 25 10 29 41 21 47 31 33 44
Array Implementation

❖ Heap can be represented as an array


❖ The root is stored at position 1
❖ At any position k
❖ Left child is at position 2k
❖ Right child is at position 2k + 1
❖ Parent is at position k//2
Insertion (Percolate Up)

❖ Find the empty position and insert the element there


❖ If necessary, interchange elements at k and k//2, k//2
with (k//2)//2, … until the root is reached
❖ The complexity is O(log N)
Example: Insert 5
3

25 10

29 41 21 47

31 33 44

0 1 2 3 4 5 6 7 8 9 10 11

3 25 10 29 41 21 47 31 33 44
Example: Insert 5
3

25 10

29 41 21 47

31 33 44 5 Violate heap property

0 1 2 3 4 5 6 7 8 9 10 11

3 25 10 29 41 21 47 31 33 44 5
Example: Insert 5
3

25 10
Violate heap property

29 5 21 47

31 33 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 25 10 29 5 21 47 31 33 44 41
Example: Insert 5
3
OK
5 10

29 25 21 47

31 33 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 5 10 29 25 21 47 31 33 44 41
DeleteMin (Percolate Down)

❖ Delete the root


❖ Put the last node as the root
❖ Compare the new root with its two children
❖ Select the smallest to be the root
❖ Continue down the tree
❖ Complexity is O(log N)
Example: DeleteMin
3

5 10

29 25 21 47

31 33 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 5 10 29 25 21 47 31 33 44 41
Example: DeleteMin

5 10

29 25 21 47

31 33 44 41

0 1 2 3 4 5 6 7 8 9 10 11

5 10 29 25 21 47 31 33 44 41
Example: DeleteMin
41

5 10

29 25 21 47

31 33 44

0 1 2 3 4 5 6 7 8 9 10 11

41 5 10 29 25 21 47 31 33 44
Example: DeleteMin
5

41 10

29 25 21 47

31 33 44

0 1 2 3 4 5 6 7 8 9 10 11

5 41 10 29 25 21 47 31 33 44
Example: DeleteMin
5

25 10

29 41 21 47

31 33 44

0 1 2 3 4 5 6 7 8 9 10 11

5 25 10 29 41 21 47 31 33 44
Increase Key

❖ Increase the value of element


❖ Compare with its children, interchange with the lesser
of its children if it is greater than any of its children
❖ Continue until a leaf is reached
❖ Complexity is O(log N)
Decrease Key

❖ Decrease the value of element


❖ Compare with its parent, interchange if it is smaller than
its parent
❖ Continue until the root is reached
❖ Complexity is O(log N)
Delete

❖ Delete the node at position P


❖ Decrease the value at this node to -infinity (now it is the
smallest)
❖ It will become the root
❖ DeleteMin
❖ Complexity is O(log N)
Example: Delete 33
3

5 10

29 25 21 47

31 33 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 5 10 29 25 21 47 31 33 44 41
Example: Delete 33
3

5 10

29 25 21 47

31 -inf 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 5 10 29 25 21 47 31 -inf 44 41
Example: Delete 33
3

5 10

-inf 25 21 47

31 29 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 5 10 -inf 25 21 47 31 29 44 41
Example: Delete 33
3

-inf 10

5 25 21 47

31 29 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 -inf 10 5 25 21 47 31 29 44 41
Example: Delete 33
-inf

3 10

5 25 21 47

31 29 44 41

0 1 2 3 4 5 6 7 8 9 10 11

-inf 3 10 5 25 21 47 31 29 44 41
Example: Delete 33

3 10

5 25 21 47

31 29 44 41

0 1 2 3 4 5 6 7 8 9 10 11

3 10 5 25 21 47 31 29 44 41
Example: Delete 33
41

3 10

5 25 21 47

31 29 44

0 1 2 3 4 5 6 7 8 9 10 11

41 3 10 5 25 21 47 31 29 44
Example: Delete 33
3

41 10

5 25 21 47

31 29 44

0 1 2 3 4 5 6 7 8 9 10 11

3 41 10 5 25 21 47 31 29 44
Example: Delete 33
3

5 10

41 25 21 47

31 29 44

0 1 2 3 4 5 6 7 8 9 10 11

3 5 10 41 25 21 47 31 29 44
Example: Delete 33
3

5 10

29 25 21 47

31 41 44

0 1 2 3 4 5 6 7 8 9 10 11

3 5 10 29 25 21 47 31 41 44
Build Heap: Algorithm 1

❖ Insert nodes one by one


❖ The complexity is O(N.log N)
Build Heap: Algorithm 2

❖ Put N elements into the tree


❖ Interchange some elements to obtain the heap property

for (i = N/2; i > 0; i--)

PercolateDown(i);
❖ Complexity is O(N)
Example: Build Heap
15

8 4

3 1 7 11

10 2 9 6 5 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 3 1 7 11 10 2 9 6 5 12 14 13
Example: Build Heap
15

8 4

3 1 7 11

10 2 9 6 5 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 3 1 7 11 10 2 9 6 5 12 14 13
Example: Build Heap
15

8 4

3 1 7 11

10 2 9 6 5 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 3 1 7 11 10 2 9 6 5 12 14 13
Example: Build Heap
15

8 4

3 1 5 11

10 2 9 6 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 3 1 5 11 10 2 9 6 7 12 14 13
Example: Build Heap
15

8 4

3 1 5 11

10 2 9 6 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 3 1 5 11 10 2 9 6 7 12 14 13
Example: Build Heap
15

8 4

3 1 5 11

10 2 9 6 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 3 1 5 11 10 2 9 6 7 12 14 13
Example: Build Heap
15

8 4

2 1 5 11

10 3 9 6 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 2 1 5 11 10 3 9 6 7 12 14 13
Example: Build Heap
15

8 4

2 1 5 11

10 3 9 6 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 2 1 5 11 10 3 9 6 7 12 14 13
Example: Build Heap
15

8 4

2 1 5 11

10 3 9 6 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 8 4 2 1 5 11 10 3 9 6 7 12 14 13
Example: Build Heap
15

1 4

2 8 5 11

10 3 9 6 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 1 4 2 8 5 11 10 3 9 6 7 12 14 13
Example: Build Heap
15

1 4

2 6 5 11

10 3 9 8 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 1 4 2 6 5 11 10 3 9 8 7 12 14 13
Example: Build Heap
15

1 4

2 6 5 11

10 3 9 8 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

15 1 4 2 6 5 11 10 3 9 8 7 12 14 13
Example: Build Heap
1

15 4

2 6 5 11

10 3 9 8 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 15 4 2 6 5 11 10 3 9 8 7 12 14 13
Example: Build Heap
1

2 4

15 6 5 11

10 3 9 8 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 2 4 15 6 5 11 10 3 9 8 7 12 14 13
Example: Build Heap
1

2 4

3 6 5 11

10 15 9 8 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 2 4 3 6 5 11 10 15 9 8 7 12 14 13
Example: Build Heap
1

2 4

3 6 5 11

10 15 9 8 7 12 14 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 2 4 3 6 5 11 10 15 9 8 7 12 14 13
Applications

❖ Heap sort
❖ Priority queues
❖ Graph algorithms
Assignments

❖ Build heap from following numbers: 10, 12, 1, 14, 6, 5, 8,


15, 3, 9, 7, 4, 11, 13, and 2. Show all steps.
❖ Insert the above numbers one at a time into an initially
empty binary heap. Show all steps.
❖ Delete the min from the above heap. Show all steps.
Assignments

❖ Write a percolate up function


❖ Write a percolate down function
❖ Write a delete min function
❖ Write a linear-time build heap function

You might also like