20
Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 25 Heap : an important tree data structure Implementing some special binary tree using an array ! Binary heap 1

Lecture-25-CS210-2012 (1).pptx

Embed Size (px)

Citation preview

Page 1: Lecture-25-CS210-2012 (1).pptx

Data Structures and Algorithms(CS210/ESO207/ESO211)

Lecture 25• Heap : an important tree data structure• Implementing some special binary tree using an array !• Binary heap

1

Page 2: Lecture-25-CS210-2012 (1).pptx

HeapDefinition: a tree data structure satisfying the following property:• value stored in a node is smaller than the value stored in its children.

2

11

4

18 23

4721

7

71 913

37

43

19

Page 3: Lecture-25-CS210-2012 (1).pptx

Operations on a heap

Query Operations• Find-min: report the smallest key stored in the heap.

Update Operations• CreateHeap(H): Create an empty heap H.• Insert(x,H): Insert a new key with value x into the heap H.• Extract-min(H): delete the smallest key from H.• Decrease-key(p, ∆, H): decrease the value of the key p by amount ∆. • Merge(H1,H2): Merge two heaps H1 and H2.

3

Page 4: Lecture-25-CS210-2012 (1).pptx

Why heaps when we can use a binary search tree ?

Compared to binary search trees, a heap is usually -- much simpler and -- more efficient

Applications: -- Priority queues -- Applications in various algorithms (Shortest paths, Minimum spanning trees)

4

Page 5: Lecture-25-CS210-2012 (1).pptx

Existing heap data structures

• Binary heap

• Binomial heap

• Fibonacci heap

• Soft heap

5

Page 6: Lecture-25-CS210-2012 (1).pptx

Implementing a special binary tree using an array

6

Page 7: Lecture-25-CS210-2012 (1).pptx

An important question(one must strive to answer it on his own)

What does the implementation of a tree data structure require ?

Answer: a mechanism to access parent and children of any node.

7

Page 8: Lecture-25-CS210-2012 (1).pptx

A complete binary tree

A complete binary of 12 nodes.

8

Page 9: Lecture-25-CS210-2012 (1).pptx

A complete binary tree

The label of the leftmost node at level = ?? The label of a node v occurring at kth place from left at level = ?? The label of the left child of v is= ??The label of the right child of v is= ??

9

0 1

Level nodes

1 2

2 4

0

1 2

3 4 5 6

7 8 9 10 11 -1

+k-2+2k-3

+2k-2

Now can you find a relationship between label of

a node and the labels of its children ?

Page 10: Lecture-25-CS210-2012 (1).pptx

A complete binary tree

Let v be a node with label .Label of left child(v) = Label of right child(v) = Label of parent(v) = 10

0 1

Level nodes

1 2

2 4

0

1 2

3 4 5 6

7 8 9 10 11

2 +1

2+2

⌊( 𝒋−1)/2 ⌋

Page 11: Lecture-25-CS210-2012 (1).pptx

A complete binary tree and array

Question: What is the relation between a complete binary trees and an array ?Answer: A complete binary tree can be implemented by an array.

Exercise: State the advantages of this array based implementation over the link based implementation for compete binary trees?

11

41

17

57

91

9

133 70

37 25 88 35

41 17 9 57 33 1 70 91 37 25 88 35

Page 12: Lecture-25-CS210-2012 (1).pptx

Binary heap

12

Page 13: Lecture-25-CS210-2012 (1).pptx

Binary heapa complete binary tree satisfying heap property at each node.

13

4

14

17

91

9

2123 29

37 25 88 33

4 14 9 17 23 21 29 91 37 25 88 33H

Page 14: Lecture-25-CS210-2012 (1).pptx

Implementation of a Binary heap

14

If n is the maximum number of keys in the binary heap at any moment of time, then we keep • H : an array of size n used for storing the binary heap.• size: a variable for the total number of keys currently in the heap.

Page 15: Lecture-25-CS210-2012 (1).pptx

Find_min(H)Report H[0].

15

4

14

17

91

9

2123 29

37 25 88 33

4 14 9 17 23 21 29 91 37 25 88 33H

Page 16: Lecture-25-CS210-2012 (1).pptx

Extract_min(H)Think hard on designing efficient algorithm for this operation.The challenge is: how to preserve the complete binary tree structure as well as the heap property ?

16

4

14

17

91

9

2123 29

37 25 88 33

4 14 9 17 23 21 29 91 37 25 88 33H

Page 17: Lecture-25-CS210-2012 (1).pptx

Extract_min(H)

17

33

14

17

91

9

2123 29

37 25 88 4

33 14 9 17 23 21 29 91 37 25 88 H 4

Page 18: Lecture-25-CS210-2012 (1).pptx

Extract_min(H)

18

9

14

17

91

33

2123 29

37 25 88

9 14 33 17 23 21 29 91 37 25 88 H

Page 19: Lecture-25-CS210-2012 (1).pptx

Extract_min(H)We are done.The no. of operations performed is of the order of no. of levels in binary heap.= O(log n) …show it as an exercise.

19

9

14

17

91

21

3323 29

37 25 88

9 14 21 17 23 33 29 91 37 25 88 H

Page 20: Lecture-25-CS210-2012 (1).pptx

Points to ponder

• Write a neat pseudocode for Extract-min.

• With the inspiration from the algorithm for Extract-min, try to design an algorithm for Insert.

• Try to design an O(n) time algorithm to build a binary heap on n keys.

In the next class, we shall discuss the above questions and many more.

20