CS2420: Lecture 20 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS2420: Lecture 20

Description:

We need to make sure that the heap property is restored after every insertion and deletion. ... Maintaining the Property: Max Heap Example. 4. 8. 2. 7. 14. 4 ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 26
Provided by: Vladimir120
Category:
Tags: cs2420 | lecture | make

less

Transcript and Presenter's Notes

Title: CS2420: Lecture 20


1
CS2420 Lecture 20
  • Vladimir Kulyukin
  • Computer Science Department
  • Utah State University

2
Outline
  • HeapSort (Chapter 7)

3
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.

4
The Heap Property
P
P
X
X
The minimum heap property
The maximum heap property
5
Packing Heaps into Arrays
1
16
2
3
14
10
We can convert this heap into a one dimensional
array by doing a level-order traversal on it.
4
5
6
7
9
3
7
8
8
9
10
1
4
2
6
Packing Heaps into Arrays
1
16
We can convert this heap into an array by doing
a level-order traversal on it.
2
3
14
10
4
5
6
7
9
3
7
8
8
9
10
Note that the index count of the array starts
with 1.
1
4
2
1
2 3 4 5 6 7 8
9 10
0
7
8
14
10
16
9
3
2
4
1
7
Packing Heaps into Arrays
1
16
2
3
14
10
4
5
6
7
9
3
7
8
8
9
10
1
4
2
1
2 3 4 5 6 7 8
9 10
0
7
8
14
10
16
9
3
2
4
1
8
Interval Nodes and Leaves
1
16
2
3
14
10
4
5
6
7
9
3
7
8
8
9
10
1
4
2
1
2 3 4 5 6 7 8
9 10
0
7
8
14
10
16
9
3
2
4
1
Internal Nodes
Leaves
9
Leaves vs. Non-Leaves
10
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.

11
Maintaining the Property Max Heap Example
Heap Property is broken.
4
7
14
8
2
1
0 1 2 3 4 5
6 7
4
14
7
2
8
1
12
Maintaining the Property Example
4
We swap 4 with the maximum of 14 and 7.
7
14
8
2
1
0 1 2 3 4 5
6 7
4
14
7
2
8
1
13
Maintaining the Property Example
14
We swap 4 with the maximum of 2 and 8.
7
4
8
2
1
0 1 2 3 4 5
6 7
14
4
7
2
8
1
14
Maintaining the Property Example
14
7
The heap property is now restored at every node.
8
4
2
1
0 1 2 3 4 5
6 7
14
8
7
2
4
1
15
RestoreHeap Maintaining The Heap Property
RestoreHeap(A, i) int Max 0 int LCH
LeftChild(i) int RCH RightChild(i) If ( LCH
lt A.size() ALCH gt Ai ) Max LCH
Else Max i If ( RCH lt A.size()
ARCH gt AMAX ) Max RCH If ( Max
! i ) Swap(Ai, AMax)
RestoreHeap(A, Max)
16
RestoreHeap Asymptotic Analysis
17
RestoreHeap Asymptotic Analysis
18
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.

19
Building a Heap
20
Building a Max Heap Example
1
10
2
3
5
20
4
5
6
7
11
15
21
34
There are 7 nodes in the heap so the inner node
range is 1, 3. Thus, we start with node 3.
21
Building a Max Heap Example
1
10
RestoreHeap(3)
5
20
2
3
5
4
6
7
11
15
21
34
22
Building a Max Heap Example
1
10
RestoreHeap(2)
5
34
2
3
5
4
6
7
11
15
21
20
23
Building a Max Heap Example
RestoreHeap(1)
1
10
15
34
2
3
5
4
6
7
11
5
21
20
24
Building a Max Heap Example
1
34
RestoreHeap(3)
3
15
10
2
5
4
6
7
11
5
21
20
25
Building a Max Heap Example
1
34
3
15
21
2
5
4
6
7
11
5
10
20
Write a Comment
User Comments (0)
About PowerShow.com