25
CS2420: Lecture 20 Vladimir Kulyukin Computer Science Department Utah State University

CS2420: Lecture 20 Vladimir Kulyukin Computer Science Department Utah State University

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

CS2420: Lecture 20

Vladimir KulyukinComputer Science Department

Utah State University

Outline

• HeapSort (Chapter 7)

The Heap Property

• A Heap is a Complete Binary Tree. All levels are filled except possibly the last one.

• The Heap Property: – Minimal heap property: for every node X

with parent P, the key in P is smaller than or equal to the key in X.

– Maximum heap property: for every node X with parent P, the key in P is greater than or equal to the key in X.

The Heap Property

XP

P

X

P

X

PX

The minimum heapproperty

The maximum heapproperty

Packing Heaps into Arrays

16

14

142

9 3

10

78

1

2 3

4 5 6 7

8 9 10

We can convert this heapinto a one dimensional arrayby doing a level-order traversal on it.

Packing Heaps into Arrays

16

14

142

9 3

10

78

1

2 3

4 5 6 7

8 9 10

7814 1016 9 3 2 4 1

1 2 3 4 5 6 7 8 9 100

Note that the index count of thearray starts with 1.

We can convert this heapinto an array by doing alevel-order traversal on it.

Packing Heaps into Arrays

16

14

142

9 3

10

78

1

2 3

4 5 6 7

8 9 10

7814 1016 9 3 2 4 1

1 2 3 4 5 6 7 8 9 10

.12)(RightChild

.2)(LeftChild

.2

)(Parent

ii

ii

ii

0

Interval Nodes and Leaves

16

14

142

9 3

10

78

1

2 3

4 5 6 7

8 9 10

7814 1016 9 3 2 4 1

1 2 3 4 5 6 7 8 9 10

.12)(RightChild

.2)(LeftChild

.2

)(Parent

ii

ii

ii

0

Internal Nodes Leaves

Leaves vs. Non-Leaves

].,12

[ :Range Leaf

].2

,1[ :Range Node Internal

NN

N

Maintaining the Heap Property

• A heap is a container so items can be inserted and deleted at any time.

• The problem is that if we insert/delete an item, the heap property may be broken after the insertion/deletion.

• We need to make sure that the heap property is restored after every insertion and deletion.

Maintaining the Property: Max Heap Example

4

82

714

4 14 7 2 8 1

0 1 2 3 4 5 6 7

1

Heap Property is broken.

Maintaining the Property: Example

4

82

714

4 14 7 2 8 1

0 1 2 3 4 5 6 7

1

We swap 4 with the maximumof 14 and 7.

Maintaining the Property: Example

14

82

74

14 4 7 2 8 1

0 1 2 3 4 5 6 7

1

We swap 4 with themaximum of 2 and 8.

Maintaining the Property: Example

14

42

78

14 8 7 2 4 1

0 1 2 3 4 5 6 7

1

The heap propertyis now restored atevery node.

RestoreHeap: Maintaining The Heap Property

RestoreHeap(A, i) {int Max = 0;int LCH = LeftChild(i);int RCH = RightChild(i);If ( LCH <= A.size() && A[LCH] > A[i] ) { Max = LCH; }

Else { Max = i; }If ( RCH <= A.size() && A[RCH] > A[MAX] )

{ Max = RCH; }If ( Max != i ) {

Swap(A[i], A[Max]); RestoreHeap(A, Max);

}}

RestoreHeap: Asymptotic Analysis

.log11log

11log2log1log

.21

:equation theof side

each of logs by taking for solvecan We

.1212

122

that know We.height of ebinary tre

complete ain nodes ofnumber thebe Let

1

1

1

0

1

NOhN

hNN

N

h

N

h

N

h

h

hh

j

hj

RestoreHeap: Asymptotic Analysis

. node ofheight theis

where,log),(pRestoreHea

ih

NOhOiA

Building a Heap

• Given the dimensions of the array A, discard all the leaves. The leaf indices are [N/2 + 1, N].

• Start from the Rightmost Inner (Non-Leaf) Node and go up the tree restoring the heap property at every inner node until you reach and restore the heap property at the root node.

Building a Heap

}

}

); ,p(RestoreHea

{ 1 Downto 2

.size() From For

{ )BuildHeap(

iA

Ai

A

Building a Max Heap: Example

10

5 20

11 15 21 34

1

2 3

4 5 6 7

There are 7 nodes in the heap; so the inner node range is [1, 3]. Thus, we start with node 3.

Building a Max Heap: Example

10

5 20

11 15 21 34

RestoreHeap(3)

1

2 3

4 5 6 7

Building a Max Heap: Example

10

5 34

11 15 21 20

1

2 3

4 5 6 7

RestoreHeap(2)

Building a Max Heap: Example

10

15 34

11 5 21 20

1

2 3

4 5 6 7

RestoreHeap(1)

Building a Max Heap: Example

34

15 10

11 5 21 20

1

2

3

4 5 6 7

RestoreHeap(3)

Building a Max Heap: Example

34

15 21

11 5 10 20

1

2

3

4 5 6 7