L11 Heap
L11 Heap
8
From the heap property, it follows that:
7 4 “The root is either the maximum or the
5 2 minimum element of the heap!”
A[2] A[4]
A[4] A[9]
2 3
1 3
4 5 6 7
8
2 9 10
16 9 10
14 8 7
A: 4 1 3 2 16 9 10 14 8 7
1 1 1
i=5 i=4 i=3
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
8
2 9 10
16 9 10 8 2 9 10
16 9 10 8 14 9 10
16 9 10
14 8 7 14 8 7 2 8 7
1 1 1
i=2 i=1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
8
14 9 10
16 9 3 14 7 9 3 8 7 9 3
8 9 10 8 9 10
2 8 7 2 8 1 2 4 1
1
i=5
4
2 3
1 3
4 5 6 7
8
2 9 10
6 9 10
14 8 7
O(lgn) O(n)
h1 = 3 (lgn)
h2 = 2
h3 = 1
h4 = 0