Heap Sort - 2
Heap Sort - 2
of Algorithms
Lecture 9
Instructor: Yusra Mansoor
Email: [email protected]
Sorting
Problem Definition:
Root node
Leaf nodes
Some Terminologies
Path
Length
◦ number of edges on the path
Depth of a node
◦ length of the unique path from the root to that node
◦ The depth of a tree is equal to the depth of the deepest leaf
Height of a node
◦ length of the longest path from that node to a leaf
◦ all leaves are at height 0
◦ The height of a tree is equal to the height of the root
Binary Tree
The depth of an “average” binary tree is considerably smaller than N, even
though in the worst case, the depth can be as large as N – 1.
Types of Binary Trees
Perfect- A perfect binary tree is a type of binary tree in which every
internal node has exactly two child nodes and all the leaf nodes are
at the same level.
Full - A full Binary tree is a special type of binary tree in which every
parent node/internal node has either two or no children.
Complete - A complete binary tree is a binary tree in which all the
levels are completely filled except possibly the lowest one, which is
filled from the left.
Balanced - A balanced binary tree, also referred to as a height-
balanced binary tree, is defined as a binary tree in which the height
of the left and right subtree of any node differ by not more than 1.
LST height – RST height <= 1
Complete or Not?
Balanced binary trees
Recall:
◦ The depth of a node is its distance from the root
◦ The depth of a tree is the depth of the deepest node
A binary tree of depth d is balanced if all the nodes at depths 0 through d-2
have two children
d-2
d-1
d
Balanced Balanced Not balanced
Why study Heapsort?
It is a well-known, traditional sorting
algorithm you will be expected to know
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
What is a “heap”?
Definitions of heap:
1. A large area of memory from which the programmer can
allocate blocks as needed, and deallocate them (or allow
them to be garbage collected) when no longer needed
2. A balanced, left-justified binary tree in which no node has a
value greater than the value in its parent
These two definitions have little in common
Heapsort uses the second definition
Outline
First, how to represent an array as a binary tree.
second, we will learn how to turn a binary tree into a
heap
Next, we will learn how to turn a binary tree back into a
heap after it has been changed in a certain way
Finally we will see how to use these ideas to sort an array
Heap Sort
The heap property
A node has the heap property (or heap condition) if the value
in the node is as large as or larger than the values in its
children
12 12 12
8 3 8 12 8 14
Blue node has Blue node has Blue node does not
heap property heap property have heap property
All leaf nodes automatically have the heap property
A binary tree is a heap if all nodes in it have the heap property
Types of Heaps
Mapping into an array
25
22 17
19 22 14 15
18 14 21 3 9 11
1 2 3 4 5 6 7 8 9 10 11 12 13
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
◦ The right child of index i is at index 2*i+1
◦ Example: the children of node 3 (19) are 7 (18) and 8 (14)
Exercise
Is the array with values {23; 17; 14; 6; 13; 10; 1; 5; 7; 12}
a max-heap?
EXTRACT-MAX and INSERT procedures, allow the heap data structure to be used
as a priority queue
HEAPIFY
Given a node that does not have the heap property, you can
give it the heap property by exchanging its value with the
value of the larger child
12 14
8 14 8 12
Blue node does not Blue node has
have heap property heap property
Notice that the child may have lost the heap property
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
HEAPIFY(A,2)
BUILD-HEAP
22 17
19 22 14 15
18 14 21 3 9 11
22 17
19 22 14 15
18 14 21 3 9
11 17
19 22 14 15
18 14 21 3 9
22 17
19 11 14 15
18 14 21 3 9
22
22 17
19 21 14 15
18 14 11 3 9
1 2 3 4 5 6 7 8 9 10 11 12 13
25 22 17 19 22 14 15 18 14 21 3 9 11
1 2 3 4 5 6 7 8 9 10 11 12 13
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 12 (9)
Reheap and repeat
Reheap the root node (index 0, containing 11)...
1 2 3 4 5 6 7 8 9 10 11 12 13
11 22 17 19 22 14 15 18 14 21 3 9 25
1 2 3 4 5 6 7 8 9 10 11 12 13
22 22 17 19 21 14 15 18 14 11 3 9 25
1 2 3 4 5 6 7 8 9 10 11 12 13
9 22 17 19 22 14 15 18 14 21 3 22 25
...And again, remove and replace the root node
Remember, though, that the “last” array index is changed
Repeat until the last becomes first, and the array is sorted!
Algorithm
Algorithm & Analysis