Heaps

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Heaps

The Heap Data Structure

 Def: A heap is an almost complete binary tree


with the following two properties:
 Structural property: all levels are full, except
possibly the last one, which is filled from left to right
 Order (heap) property: for any node x, Parent(x)
≥x
8 From the heap property, it
follows that:
7 4 “The root is the maximum
5 2
element of the heap!”

Heap

A heap is a binary tree that is filled in order


What is a Binary Heap?
 A binary heap is an almost complete binary tree with one (or both) of the
following heap order properties:
• MinHeap property: Each node must have a key that is less or equal to
the key of each of its children.
• MaxHeap property: Each node must have a key that is greater or
equal to the key of each of its children.
 A binary heap satisfying the MinHeap property is called a MinHeap.
 A binary heap satisfying the MaxHeap property is called a MaxHeap.
 A binary heap with all keys equal is both a MinHeap and a MaxHeap.
 Recall: A complete binary tree may have missing nodes only on the right
side of the lowest level.

All levels except the bottom one


must be fully populated with nodes

All missing nodes, if any, must be


on the right side of the lowest level
MinHeap and non-MinHeap examples
Violates MinHeap property
13 13
21>6
21 16 21 16

24 31 19 68 6 31 19 68

65 26 32 A MinHeap 65 26 32 Not a Heap

13 Not a Heap 13 Not a Heap

21 16 21 16

24 31 19 68 24 31 19

65 26 32 65 26 32
Violates heap structural property Violates heap structural property
MaxHeap and non-MaxHeap examples
Violates MaxHeap
68 68
property 65 < 67
65 46 65 46

24 32 23 25 67 32 23 25

15 20 31 A MaxHeap 15 20 31 Not a Heap

70
Not a Heap Not a Heap 30
50 40
21 16
24 31 19 38
19 18 10

15 20 25
2 5 15
Violates heap structural property Violates heap structural property
Array Representation of a Binary Heap
 A heap is a dynamic data structure that is represented and
manipulated more efficiently using an array.
 Since a heap is a complete binary tree, its node values can be stored
in an array, without any gaps, in a breadth-first order, where:
Value(node i+1) array[ i ], for i > 0

13
21 16

24 31 19 68
13 21 16 24 31 19 68 65 26 32
0 1 2 3 4 5 6 7 8 9
65 26 32

 The root is array[0]


 The parent of array[i] is array[(i – 1)/2], where i > 0
 The left child, if any, of array[i] is array[2i+1].
 The right child, if any, of array[i] is array[2i+2].
Mapping into an array
25

22 17

19 22 14 15

18 14 21 3 9 11

0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11

 Notice:
 The left child of index i is at index 2*i+1
 The right child of index i is at index 2*i+2
 Example: the children of node 3 (19) are 7 (18) and 8 (14)
Mapping into an array
26 24 20 18 17 19 13 12 14 11
0 1 2 3 4 5 6 7 8 9

Max-heap as a binary Max-heap as an


tree. 26 array.

24 20

18 17 19 13

12 14 11 Last row filled from left to right.


Mapping into an array
A few questions
Consider a maxheap containing 30 elements.
 At which array locations is 3
rd highest value

present in the maxheap?


 At which array locations is 4th highest value

present in the maxheap?


 At which array locations is smallest value present

in the maxheap? What if elements are 31?


 At which array locations is second smallest value

present in the maxheap? What if elements are 31?


A Programming Assignment

 Write a function check_heap(int a[], int n) to


check if the array a containing n elements
represents maxheap.
Constructing a heap (Step I)

 A tree consisting of a single node is automatically a


heap
 We construct a heap by adding nodes one at a time:
 Add the node just to the right of the rightmost node in
the deepest level
 If the deepest level is full, start a new level
 Examples:
Add a new Add a new
node here node here
Constructing a heap (Step II)
 Each time we add a node, we may destroy the heap
property of its parent node
 To fix this, we sift up
 But each time we sift up, the value of the topmost node in
the sift may increase, and this may destroy the heap
property of its parent node
 We repeat the sifting up process, moving up in the tree,
until either
 We reach nodes whose values don’t need to be swapped
(because the parent is still larger than both children), or
 We reach the root
Constructing a heap III

8 8 10 10

10 8 8 5

1 2 3

10 10 12

8 5 12 5 10 5

12 8 8
4
Other children are not affected

12 12 14

10 5 14 5 12 5

8 14 8 10 8 10

 The node containing 8 is not affected because its parent gets


larger, not smaller
 The node containing 5 is not affected because its parent gets
larger, not smaller
 The node containing 8 is still not affected because, although its
parent got smaller, its parent is still greater than it was originally
MinHeap Insertion Example
13 13
21 16 Insert 18 21 16

24 31 19 68 24 31 19 68

65 26 32 65 26 32 18

Percolate up

13 13
18 16 21 16
Percolate up
24 21 19 68 24 18 19 68

65 26 32 31 65 26 32 31
A sample heap

 Here’s a sample binary tree after it has been


heapified
25

22 17

19 22 14 15

18 14 21 3 9 11

 Notice that heapified does not mean sorted


 Heapifying does not change the shape of the binary
tree; this binary tree is balanced and left-justified
because it started out that way
A Programming Assignment

 Write a function insert_heap(int a[], int n,


int val) to insert val in a maxheap reprsented by
a.
Removal

 Removal is easy:
 When we remove from a heap, we always remove the
node with the largest key.
 Hence, removal is quite easy and has index 0 of the
heap array.
 maxNode = heapArray[0];
 But tree is then not complete:
 But once root is gone, tree is not complete and we
must fill this cell.
 Now this becomes interesting… 19
Removal of “maxNode”

 Move ‘last node’ to root.


 Start by moving the ‘last node’ into the root.
 The ‘last’ node is the rightmost node in the lowest
occupied level of the tree.
 This also corresponds to the last filled cell in the array
(ahead).

 Trickle-down:
 Then trickle this last node down until it is below a larger
node and above a smaller one.
11/2/2016 20
Removal (Delete)
100

Note: NOT binary search tree!


90 80

30 60 50 70

20 10 40 55 Last node added


45 5

Last node added is 5.


Delete Algorithm: heapArray[0] = heapArray [n-1]
n--; // size of array is decreased by one.

Heap is represented logically as a tree.


11/2/2016
(Tree is organized to represent a priority queue. Easy to visualize.)21
Heap is physically implemented (here) using an array.
Example Delete (Removal)
95
(Different tree) Trickle Down

82 51

63 70 37 10

43 27 53 34
30 Last node added

Trickle down swapping node w/larger child until node >= children

As we trickle down, the nodes swap out always swapping the larger node
with the node we are trickling down (so as to maintain the larger nodes above…)

We trickle down selecting the largest child with which to swap.


We must compare, but always swap with the larger of the two.
Note: we very well may NOT trickle down to a leaf node
11/2/2016 22
82

70 51

63 53 37 10

43 27 30 34

The new arrangement via a delete would be as above.


Node 95 (the largest) is deleted.
Go through this… Tree remains balanced after delete
and the rules for the heap are preserved.
(95 removed; compare 30 with 82; 82 selected and moved to root;
Compare 30 with 70. 70 moves up.
Compare 30 with 53; 53 moves up. 30 is a leaf node. )
23
MinHeap Delete Example
13 Delete min 31
element
18 19 delete last node 18 19

24 21 23 68 24 21 23 68

65 26 32 31 65 26 32
Replace by value of last node Percolate down

18 18
Percolate down
21 19 31 19

24 31 23 68 24 21 23 68

65 26 32 65 26 32
Removing and replacing the root

 The “root” is the first element in the array


 The “rightmost node at the deepest level” is the last
element
 Swap them...
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11

0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25

 ...And pretend that the last element in the array no longer


exists—that is, the “last index” is 11 (9)
Heapsort
 Possible to use a heap to sort an array
 Place array items into a maxheap
 Then remove them
 Items will be in descending order
 Place them back into the original array and they will be in
order
 Heapsort is always O(n log n)
 Quicksort is usually O(n log n) but in the worst case
slows to O(n2)
 Quicksort is generally faster, but Heapsort is better in
time-critical applications
Heapsort

 Sort by maintaining the as yet unsorted elements as a max-


heap.
 Start by building a max-heap on all elements in A.
 Maximum element is in the root, A[0].
 Move the maximum element to its correct final position.
 Exchange A[0] with A[n-1].
 Discard A[n-1] – it is now sorted.
 Decrement heap-size[A].
 Restore the max-heap property on A[0..n–1].
 Call MaxHeapify(A, 0).
 Repeat until heap-size[A] is reduced to 2.
Example: Convert the following array to a heap

16 4 7 1 12 19

Picture the array as a complete binary tree:

16

4 7

1 12 19
16 16

4 7 4 19
swap

12 19 1 12 7
1

16 19
swap

12 19 12 16
swap

4 7 1 4 7
1
EXAMPLE OF HEAP SORT
Take out biggest
19

12 16
Move the last element
to the root

1 4 7

Sorted:
Array A

12 16 1 4 7 19
7
swap
HEAPIFY()
12 16

1 4

Sorted:
Array A

7 12 16 1 4 19
16

12 7

1 4

Sorted:
Array A

16 12 7 1 4 19
Take out biggest
16
Move the last element
to the root
12 7

1 4

Sorted:
Array A

12 7 1 4 16 19
4

12 7

Sorted:
Array A

4 12 7 1 16 19
swap 4

HEAPIFY()
12 7

Sorted:
Array A

4 12 7 1 16 19
12

4 7

Sorted:
Array A

12 4 7 1 16 19
Take out biggest
12
Move the last
element to the
root 4 7

Sorted:
Array A

4 7 1 12 16 19
1
swap

4 7

Sorted:
Array A

1 4 7 12 16 19
7

4 1

Sorted:
Array A

7 4 1 12 16 19
Take out biggest
7
Move the last
element to the
4 1 root

Sorted:
Array A

1 4 7 12 16 19
swap 1

HEAPIFY()
4

Sorted:
Array A

4 1 7 12 16 19
Take out biggest
Move the last 4
element to the
root
1

Sorted:
Array A

1 4 7 12 16 19
Take out biggest
1

Sorted:
Array A

1 4 7 12 16 19
Sorted:

1 4 7 12 16 19
Heapsort

A trace of heapsort (a – c)
Heapsort

A trace of heapsort (d – f)
Heapsort

A trace of heapsort (g – i)
Heapsort

A trace of heapsort (j – l)

You might also like