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

Lec-16-18-Heaps-HeapSort

The document provides an overview of special types of binary trees, specifically full and complete binary trees, and introduces the heap data structure with its properties and operations. It details how heaps can be represented as arrays, the differences between max-heaps and min-heaps, and the processes for adding and deleting nodes. Additionally, it covers the complexities of building a max-heap and the heapsort algorithm, emphasizing the efficiency of these operations.

Uploaded by

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

Lec-16-18-Heaps-HeapSort

The document provides an overview of special types of binary trees, specifically full and complete binary trees, and introduces the heap data structure with its properties and operations. It details how heaps can be represented as arrays, the differences between max-heaps and min-heaps, and the processes for adding and deleting nodes. Additionally, it covers the complexities of building a max-heap and the heapsort algorithm, emphasizing the efficiency of these operations.

Uploaded by

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

Design and Analysis of Algorithms

National University of Computer and Emerging Sciences,


Islamabad
Special Types of Trees
• Def: Full binary tree = a
4

binary tree in which each 1 3


node is either a leaf or has 2 16 9 10
degree exactly 2. 14 8 7
12
Full binary tree

• Def: Complete binary tree = 4


a binary tree in which all
leaves are on the same 1 3

level and all internal nodes 2 16 9 10

have degree 2.
Complete binary tree
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
• 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
Useful Properties

height

height

d +1
d
2 −1
n   2l = = 2d +1 − 1 Height of root = 3
l =0 2 −1 4

1 3
Height of (2)= 1 2 16 9 10 Level of (10)= 2
14 8
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
7 4
follows that:
“The root is the maximum
5 2
element of the heap!”
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
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]
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)
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
• Priority queues
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
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


Maintaining the Heap Property
• Assumptions: Alg: MAX-HEAPIFY(A, i, n)
• Left and Right 1. l ← LEFT(i)
subtrees of i 2. r ← RIGHT(i)
are max-heaps 3. if l ≤ n and A[l] > A[i]
• A[i] may be
4. then largest ←l
smaller than
its children 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)
MAX-HEAPIFY Running Time
• Intuitively:
- h
-
- 2h
- O(h)

• Running time of MAX-HEAPIFY is O(lgn)

• Can be written in terms of the height of the


heap, as being O(h)
• Since the height of the heap is lgn
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
1

4
Alg: BUILD-MAX-HEAP(A) 2 3

1. n = length[A] 4
1
5 6
3
7

2 16 9 10
2. for i ← n/2 downto 1
8 9 10

14 8 7

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

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
Complexity of BUILD MAX HEAP

Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
O(n)
2. for i ← n/2 downto 1
3. do MAX-HEAPIFY(A, i, n) O(lgn)

 Running time: O(nlgn)?


Complexity of Build-Max-Heap

1. Each call to Max-Heapify costs O(log n) time, there are O(n)


such calls. Thus, the complexity is O(n log n)?
2. Tighter analysis results in the complexity of O(n).
The time required by Max-Heapify when called on a node of
height h is O(h), and so we can express the total cost of Build-Max-
Heap as being bounded by

floor(lg n ) floor(lg n )
n h

h =0
ceiling( h +1 )O(h) = O(n  h )
2 h =0 2
Complexity of Build-Max-Heap
floor(lg n ) floor(lg n )
n h

h =0
ceiling( h+1 )O(h) = O(n  h )
2 h =0 2

1

k =0
x =
k

1− x
; for | x |  1, differentiating both sides

x

k =0
kx = k

(1 − x) 2
; if x = 1 / 2
 
k
 kxk = 
k =0 k =0 2
k
=2

floor(lg n ) floor(lg n )
n h
Thus, h =0
ceiling( h+1 )O(h) = O(n  h )
2 h =0 2
= O(n)
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
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)
Alg: HEAPSORT(A)

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

Running time: O(nlgn) O(lgn)

You might also like