Chapter 21 Binary Heap. 2 Objective To learn Basic properties of the binary heap Insert and...

32
Chapter 21 Binary Heap

Transcript of Chapter 21 Binary Heap. 2 Objective To learn Basic properties of the binary heap Insert and...

Chapter 21Binary Heap

2

Objective

To learn Basic properties of the binary heap Insert and deletemin operations Linear time heap-construction algorithm Heapsort

3

Topics

Review and analyze operations on Heap data structure

Review definitions Tree, binary treedepthheight full binary treecomplete binary treeHeap property

4

Binary trees A tree is an acyclic, connected, undirected

graph.

Only one path exists between a pair of nodes

A leaf of a tree is a node with no children.

Binary tree - a tree where each node has 0,1 or 2 children

5

Depth and height of a tree Depth of a node is:

Depth of the root of a tree is 0.The depth of its parent +1

Depth of a tree is maximum depth of its leaves.

Height of a node is:Height of a leaf of a tree is 0.The maximum height of its children +1

Height of a tree is the height of the root.

6

Depth and height of a binary tree

0

11

2

2

1 0

0 0

Depth

The depth of the tree = height of the tree = 2

Height

7

A complete binary tree

A full binary tree (also called complete) is a binary tree such thatAll internal nodes have 2 childrenAll leaves have depth d

A complete binary tree (also called essentially complete) is a binary tree such thatAll internal nodes have 2 children except the last

internal node which may have only 1 child.All leaves have depth d or d -1Nodes with depth d are as far to the left as

possible.

8

Full binary tree Complete binary tree

n = 2h+1 -1 2h n 2h+1 -1

9

The height of a complete binary tree

The number of nodes n of a complete binary tree satisfies: 2h n (2h+1-1)

Taking the log base 2 we get:

h lg n and lg(n+1) h+1 or

lg(n+1)-1 h lg n

Since h is integer h = lg(n + 1)-1= lg n

10

The height of a complete binary tree

2h n (2h+1-1)

Full binary treeall leaves same depth

Complete binarytree with 1 node at depth h

11

Heap Definition

A heap is a complete binary tree that satisfies the heap property.

Minimum Heap Property: The value stored at each node is less than or equal

to the values stored at its children.

OR Maximum Heap Property: greater

12

Implementation of Heap For simplicity we assume the complete

binary tree is an array, and the root is stored at index 1.

For any element in array position i, its left child is at position 2i, and it’s right child is at 2i+1, its parent is at i/2 .

13

Last node in a heap

The last node of a heap is the rightmost internal node of on the last level

2

65

79

last node

14

Heap viewed as Binary tree implemented as an array.

3 2

8 7

18 14 9

29 6

1

23

4 5 6 7

8 9 10

1

1 3 2 8 7 29 6

1 2 3 4 5 6 7 8 9

18 14 9

10

last

15

insert(v )

Item inserted as last item in the heapHeap property may

be violated

Do percolate to restore heap property

2

65

79

insertion node

2

65

79 1

z

z

16

Percolate up After the insertion of a new key k, the heap-order property

may be violated Algorithm percolate up restores the heap-order property by

swapping k along an upward path from the insertion node percolate up terminates when the key k reaches the root or a

node whose parent has a key smaller than or equal to k Since a heap has height O(log n), upheap runs in O(log n)

time

2

15

79 6z

1

25

79 6z

17

deleteMin()

Save root object O(1)

Remove last element and store in root O(1)

siftDown(1)

30 20

80

1

2 3

4

10

30 20

1

2 3

80

30 80

1

2 3

20

10

last

18

4 3

8 17

12 14 19

6 13

1

23

4 5 6 7

8 9 10

9siftDown(1)New value at root.

Satisfy the Heap property.

Right Child is smallerExchange root and right child

19

4 9

8 17

12 14 19

6 13

1

23

4 5 6 7

8 9 10

3

ParentLeft Child is smallerExchange parent and left child

20

4 6

8 17

12 14 19

9 13

1

23

4 5 6 7

8 9 10

3

The worst case run time to do siftDown(index) isO(h(index)) where h(index) is the height of node indexWhen index=root=1, O(lg n)

21

Build heap---linear time//Build-heap

1. for i (last /2)downto 1

2. do siftDown( i )

Why we can start siftDown at last/2 ?

because we :need to siftDown only parents the rest of the nodes are leaves and leaves satisfy the heap property

22

5 8 12 9 7 10 21

1 2 3 4 5 6 7 8 9

6 14 4

10

8 12

9 7

6 14 4

10 21

1

23

4 5 6 7

8 9 10

5

siftDown makes it a min heap

23

8 12

9 4

6 14 7

10 21

1

23

4 5 6 7

8 9 10

5

i = 4

siftDown makes this into heap

this is a heap

24

8 12

6 4

9 14 7

10 21

1

23

4 5 6 7

8 9 10

5

These are heaps

i = 3

siftDown makes heap

25

1

siftDown

i = 2

10

12 21

3

6 7

5

8

6 4

9 14 7

2

4 5

8 9 10

4

6 8

9 14 7

2

4 5

8 9 10

4

6 7

9 14 8

2

4 5

8 9 10

26

4 10

6 7

9 14 8

12 21

1

23

4 5 6 7

8 9 10

5i = 1

10

6 7

9 14 8

12 21

1

23

4 5 6 7

8 9 10

4

5

27

Running time

n/2 nodes For each node, at most O(log(n)) The running time is O(nlgn)

28

Heap-Sort

Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time

The resulting algorithm is called heap-sort

Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort

29

97

5953

412658 31

16 3621 97 53 59 26 41 58

1 2 3 4 50

31 16 21 36

6 7 8 9 10

59 53 58 26 41 36

1 2 3 4 50

31 16 21 97

6 7 8 9 10

59

5853

412636 31

1621 97

30

HeapSort

//build heap

for i (last /2)downto 1

do siftDown( a, i, last) //shiftDown from i to last

for j last-1 downto 1

swap(a[0], a[j]) //deleteMax

do siftDown(a, 0, j) // shiftDown from 0 to j

31

Common errors (from book, page 786) For heapsort data begins in position 0, so

the children of the I are in positions 2i+1 and 2i+2.

STL priority queue is a max heap, not a min heap.

More errors are defined in the book.

32

In class exercises

21.1 Describe the structure and ordering properties of the

binary heap 21.2

In a binary heap, for an item in position I where are the parent, left child, and right child located?

21.3 Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15,

3, 9, 7, 4, 11, 13, and 2, one at a time, in an initially empty heap. Then show the result of using the linear-time buildHep algorithm instead