data structures and algorithms · a complete binary tree and array question: what is the relation...

32
Data Structures and Algorithms (CS210A) Semester I – 2014-15 Lecture 28: Heap : an important tree data structure Implementing some special binary tree using an array ! Binary heap 1

Upload: others

Post on 21-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures and Algorithms · 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

Data Structures and Algorithms (CS210A)

Semester I – 2014-15

Lecture 28: • Heap : an important tree data structure

• Implementing some special binary tree using an array !

• Binary heap

1

Page 2: Data Structures and Algorithms · 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

Data Structures

2

Lists: (arrays, linked lists)

Binary Search Trees Simplicity

Range of efficient functions

Binary Heap

Page 3: Data Structures and Algorithms · 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

Heap Definition: a tree data structure where :

value stored in a node ? value stored in each of its children.

3

11

4

18 23

47 21

7

71 9 13

37

43

19

<

Page 4: Data Structures and Algorithms · 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

Operations on a heap

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

Update Operations • CreateHeap(H)

• Insert(x,H)

• Extract-min(H)

• Decrease-key(p, ∆, H)

• Merge(H1,H2)

4

: Create an empty heap H.

: Insert a new key with value x into the heap H.

: delete the smallest key from H.

: decrease the value of the key p by amount ∆.

: Merge two heaps H1 and H2.

Page 5: Data Structures and Algorithms · 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

Why heaps when we can use a binary search tree ?

Compared to binary search trees, a heap is usually

-- much simpler and

-- more efficient

5

Page 6: Data Structures and Algorithms · 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

Existing heap data structures

• Binary heap

• Binomial heap

• Fibonacci heap

• Soft heap

6

Page 7: Data Structures and Algorithms · 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

Can we implement a binary tree using an array ?

7

Yes. In some special cases

Page 8: Data Structures and Algorithms · 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

fundamental question

Question: What does the implementation of a tree data structure require ?

Answer: a mechanism to

• access parent of a node

• access children of a node.

8

Page 9: Data Structures and Algorithms · 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

A complete binary tree

A complete binary of 12 nodes.

9

Page 10: Data Structures and Algorithms · 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

A complete binary tree

The label of the leftmost node at level 𝒊 = ??

The label of a node v occurring at 𝒌th place from left at level 𝒊 = ??

The label of the left child of v is= ??

The label of the right child of v is= ??

10

0 1

Level nodes

1 2

2 4

0

1 2

3 4 5 6

7 8 9 10 11

𝟐𝒊 − 𝟏

𝟐𝒊 + 𝒌 − 𝟐

𝟐𝒊+𝟏 + 𝟐𝒌 − 𝟑

𝟐𝒊+𝟏 + 𝟐𝒌 − 𝟐

𝟐𝒊+𝟏 − 𝟏+ ? 𝟐(𝒌 − 𝟏)

Can you see a relationship between label of a node

and labels of its children ?

Page 11: Data Structures and Algorithms · 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

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) = 11

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 12: Data Structures and Algorithms · 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

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.

12

41

17

57

91

9

1 33 70

37 25 88 35

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

Advantages ? The most compact representation.

Page 13: Data Structures and Algorithms · 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

Binary heap

13

Page 14: Data Structures and Algorithms · 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

Binary heap a complete binary tree

14

4

14

17

91

9

21 23 29

37 25 88 33

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

satisfying heap property at each node.

Page 15: Data Structures and Algorithms · 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

Implementation of a Binary heap

Let 𝒏 : the maximum number of keys at any moment of time,

then we keep

• H[]

• size

15

: an array of size 𝒏 used for storing the binary heap.

: a variable for the total number of keys currently in the heap.

Page 16: Data Structures and Algorithms · 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

Find_min(H) Report H[0].

16

4

14

17

91

9

21 23 29

37 25 88 33

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

Page 17: Data Structures and Algorithms · 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

Extract_min(H)

18

4

14

17

91

9

21 23 29

37 25 88 33

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

Page 18: Data Structures and Algorithms · 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

Extract_min(H)

19

33

14

17

91

9

21 23 29

37 25 88 4

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

Page 19: Data Structures and Algorithms · 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

Extract_min(H)

20

9

14

17

91

33

21 23 29

37 25 88

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

Page 20: Data Structures and Algorithms · 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

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.

21

9

14

17

91

21

33 23 29

37 25 88

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

Page 21: Data Structures and Algorithms · 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

Insert(x,H)

22

9

14

17

71

21

33 23 29

37 25 88 41 52 32 76

98 85 47 57 11

9 14 21 17 23 33 29 71 37 25 88 41 52 32 76 98 85 47 57 H 11

Page 22: Data Structures and Algorithms · 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

Insert(x,H)

23

9

14

17

71

21

33 23 29

37 25 88 41 52 32 76

98 85 47 57 11

9 14 21 17 23 33 29 71 37 25 88 41 52 32 76 98 85 47 57 H 11

Page 23: Data Structures and Algorithms · 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

Insert(x,H)

24

9

14

17

71

21

33 23 29

37 11 88 41 52 32 76

98 85 47 57 25

9 14 21 17 23 33 29 71 37 11 88 41 52 32 76 98 85 47 57 25 H

Page 24: Data Structures and Algorithms · 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

Insert(x,H)

25

9

14

17

71

21

33 11 29

37 23 88 41 52 32 76

98 85 47 57 25

9 14 21 17 11 33 29 71 37 23 88 41 52 32 76 98 85 47 57 25 H

Page 25: Data Structures and Algorithms · 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

Insert(x,H)

26

9

11

17

71

21

33 14 29

37 23 88 41 52 32 76

98 85 47 57 25

9 11 21 17 14 33 29 71 37 23 88 41 52 32 76 98 85 47 57 25 H

Page 26: Data Structures and Algorithms · 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

Insert(x,H)

Insert(x,H)

{ 𝒊 size(H);

H(𝒊) x;

size(H) size(H) + 1;

While( ?? and ?? )

{

??

??

}

}

Time complexity: O(log n)

27

H(𝒊) < H( (𝒊 − 1)/2 )

H(𝒊) ↔ H( (𝒊 − 1)/2 );

𝒊 > 0

𝒊 (𝒊 − 1)/2 ;

Page 27: Data Structures and Algorithms · 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

The remaining operations on Binary heap

• Decrease-key(p, ∆, H): decrease the value of the key p by amount ∆. – Similar to Insert(x,H).

– O(log 𝒏) time

– Do it as an exercise

• Merge(H1,H2): Merge two heaps H1 and H2. – O(𝒏) time where 𝒏 = total number of elements in H1 and H2

(This is because of the array implementation)

28

Page 28: Data Structures and Algorithms · 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

Other heaps

Fibonacci heap : a link based data structure.

29

Binary heap Fibonacci heap

Find-min(H) O(1)

Insert(x,H) O(log 𝑛)

Extract-min(H) O(log 𝑛)

Decrease-key(p, ∆, H) O(log 𝑛)

Merge(H1,H2) O(𝑛)

Excited to study Fibonaccci heap ? We shall study it during CS345.

O(1)

O(log 𝑛)

O(1)

O(1)

O(1)

Page 29: Data Structures and Algorithms · 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

Building a Binary heap

30

Page 30: Data Structures and Algorithms · 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

Building a Binary heap

Problem: Given 𝒏 elements {𝑥0, …, 𝑥𝑛−1}, build a binary heap H storing them.

Trivial solution:

CreateHeap(H);

For( 𝒊 = 0 to 𝒏 − 𝟏 )

Insert(𝑥𝑖,H);

31

(Building the Binary heap incrementally)

Page 31: Data Structures and Algorithms · 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

Building a Binary heap incrementally

32

9

11

17

71

21

33 14 29

37 23 88 41 52 32 76

98 85 47 57 25

9 11 21 17 14 33 29 71 37 23 88 41 52 32 76 98 85 47 57 25 H

Page 32: Data Structures and Algorithms · 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

Time complexity of incrementally building a Binary heap

Theorem: Time complexity of building a binary heap incrementally is O(𝒏 log 𝒏).

Those who love algorithm should ponder over it

33

A binary heap can be built in O(𝒏).