PRESENTATION OF THE VLADIMIR REGION PRESENTATION OF THE VLADIMIR REGION.
CS2420: Lecture 20 Vladimir Kulyukin Computer Science Department Utah State University.
-
date post
21-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of CS2420: Lecture 20 Vladimir Kulyukin Computer Science Department Utah State University.
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.
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
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
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 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.