0% found this document useful (0 votes)
10 views21 pages

Unit4 Heap

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Unit4 Heap

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

HEAP SORT

SPECIAL TYPES OF TREES


 Def: Full binary tree = a binary tree 4
in which each node is either a leaf or
has degree exactly 2. 1 3
2 16 9 10
14 8 7
12
Full binary tree
 Def: Complete binary tree = A
binary tree T with n levels is 4
Complete if all levels except
possibly the last are completely full, 1 3
and the last level has all its nodes to
2 16
the left side.
Complete binary tree 2
DEFINITIONS
 Height of a node = the number of edges on the longest
simple path from the node down to a leaf
 Level of a node = the length of a path from the root to
the node (Root is at level 0)
 Height of tree = height of root node

4 Height of root = 3

1 3
Height of (2)= 1 2 16 9 10 Level of (10)= 2
14 8
3
USEFUL PROPERTIES
 There are at most 2^l nodes at level l of binary tree
 A Binary tree with depth (height) d has at most 2^ d+1-1 node
(Height of leaf node is 0)

height

4 Height of root = 3

1 3
Height of (2)= 1 2 16 9 10 Level of (10)= 2
14 8
4
THE HEAP DATA STRUCTURE
 Def: A heap is a nearly 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!”
5
Heap

A heap is a binary tree that is filled in order


ARRAY REPRESENTATION OF HEAPS
 A heap can be stored as an array
A.
 Root of tree is A[1]
 Left child of A[i] = A[2i]
 Right child of A[i] = A[2i + 1]
 Parent of A[i] = A[ i/2 ]
 Heapsize[A] ≤ length[A]
 The elements in the subarray
A[(n/2+1) .. n] are leaves.
 Here n=10 so 6 to 10 are leaves.

6
HEAP TYPES
 Max-heaps (largest element at root), have the max-heap
property:
 for all nodes i, excluding the root:
A[PARENT(i)] ≥ A[i]

 Min-heaps (smallest element at root), have the min-heap


property:
 for all nodes i, excluding the root:
A[PARENT(i)] ≤ A[i]
7
ADDING/DELETING NODES
 New nodes are always inserted at the bottom level (left to
right)
 Nodes are removed from the bottom level (right to left)

8
OPERATIONS ON HEAPS
 Maintain/Restore the max-heap property
 MAX-HEAPIFY

 Create a max-heap from an unordered array


 BUILD-MAX-HEAP

 Sort an array in place


 HEAPSORT

9
MAINTAINING THE HEAP PROPERTY
 Suppose a node is smaller than a child
 Left and Right subtrees of i are max-heaps
 To eliminate the violation:
 Exchange with larger child
 Move down the tree
 Continue until node is not smaller than
children

10
EXAMPLE
MAX-HEAPIFY(A, 2, 10)

A[2]  A[4]

A[2] violates the heap property A[4] violates the heap property

A[4]  A[9]

Heap property restored


11
MAINTAINING THE HEAP PROPERTY
 Assumptions: Alg: MAX-HEAPIFY(A, i, n)
 Left and Right
1. l ← LEFT(i) //Position
subtrees of i are
max-heaps 2. r ← RIGHT(i)
 A[i] may be smaller 3. if l ≤ n and A[l] > A[i]
than its children 4. then largest ←l
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] ↔ A[largest]
10. MAX-HEAPIFY(A, largest, n)
12
BUILDING A HEAP
 Convert an array A[1 … n] into a max-heap (n = length[A])
 The elements in the subarray A[(n/2+1) .. n] are leaves
Apply MAX-HEAPIFY on elements between 1 and n/2

14

Alg: BUILD-MAX-HEAP(A) 1

4
1. n = length[A]
2 3

2. for i ← n/2 downto 1 4


1
5 6
3
7

3. do MAX-HEAPIFY(A, i, n) 8
2 9 10
16 9 10
14 8 7

A: 4 1 3 2 16 9 10 14 8 7
4 1 3 2 16 9 10 14 8 7
EXAMPLE: A
i=5 i=4 i=3
1 1 1

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

i=2 i=1
1 1 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 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
15
RUNNING TIME OF BUILD MAX HEAP
Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← n/2 downto 1
O(n)
3. do MAX-HEAPIFY(A, i, n) O(lgn)

 Running time: O(nlgn)

16
PROBLEMS

1. Create Max heap and Min heap for Input → 35 33 42 10


14 19 27 44 26 31

2. Convert Max Heap = [9,4,7,1,-2,6,5] to Min Heap

3. Demonstrate, step by step, the operation of Build-Heap


on the array
A=[5, 3, 17, 10, 84, 19, 6, 22, 9]

17
SOLUTION OF PROBLEM 1.

Min Heap 18
Max Heap
RESULT OF PROBLEM 2

result = [-2,1,5,9,4,6,7]

19
HEAPSORT
Goal:
 Sort an array using heap representations
 Idea:
 Build a max-heap from the array
 Swap the root (the maximum element) with the last element in the
array
 “Discard” this last node by decreasing the heap size
 Call MAX-HEAPIFY on the new root
 Repeat this process until only one node remains

20
EXAMPLE: A=[7, 4, 3, 1, 2]

MAX-HEAPIFY(A, 1, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2)

MAX-HEAPIFY(A, 1, 1)

21
ALG: HEAPSORT(A)

1. BUILD-MAX-HEAP(A)
2. for i ← length[A] downto 2
3. do exchange A[1] ↔A[i]
4. MAX-HEAPIFY(A, 1, i - 1)

 Running time: O(nlgn)

22

You might also like