Download Heapsort Algorithm: Understanding Heaps, Balanced Binary Trees, and Heapsort Technique and more Slides Design and Analysis of Algorithms in PDF only on Docsity!
Heapsort
Docsity.com
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(n^2 )
Quicksort is generally faster, but Heapsort is better in
time-critical applications
Heapsort is a really cool algorithm!
Docsity.com
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 n is balanced if all the nodes at
depths 0 through n-2 have two children
Balanced Balanced Not balanced
n- n- n
Docsity.com
Left-justified binary trees
A balanced binary tree of depth n is left-
justified if:
it has 2
n
nodes at depth n (the tree is “full”), or
it has 2
k
nodes at depth k, for all k < n, and all
the leaves at depth n are as far left as possible
Left-justified Not left-justified
Docsity.com
The heap property
A node has the heap property if the value in the
node is as large as or larger than the values in its
children
All leaf nodes automatically have the heap property
A binary tree is a heap if all nodes in it have the
heap property
Blue node has
heap property
Blue node has
heap property
Blue node does not
have heap property
Docsity.com
siftUp
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
This is sometimes called sifting up
Notice that the child may have lost the heap property
Blue node has
heap property
Blue node does not
have heap property
Docsity.com
Constructing a heap 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
Docsity.com
Constructing a heap III
1 2 3
4
Docsity.com
A sample heap
Here’s a sample binary tree after it has been heapified
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
Docsity.com
Removing the root (animated)
Notice that the largest number is now in the root
Suppose we discard the root:
How can we fix the binary tree so it is once again balanced
and left-justified?
Solution: remove the rightmost leaf at the deepest level and
use it for the new root
Docsity.com
The reHeap method II
Now the left child of the root (still the number 11 ) lacks
the heap property
We can siftUp() this node
After doing this, one and only one of its children may have
lost the heap property
Docsity.com
The reHeap method III
Now the right child of the left child of the root (still the
number 11 ) lacks the heap property:
We can siftUp() this node
After doing this, one and only one of its children may have
lost the heap property —but it doesn’t, because it’s a leaf
Docsity.com
Sorting
What do heaps have to do with sorting an array?
Here’s the neat part:
Because the binary tree is balanced and left justified, it can be
represented as an array
Danger Will Robinson: This representation works well only with balanced , left-justified binary trees
All our operations on binary trees can be represented as
operations on arrays
To sort:
heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node; } Docsity.com
Mapping into an array
Notice:
The left child of index i is at index 2*i+
The right child of index i is at index 2*i+
Example: the children of node 3 (19) are 7 (18) and 8 (14)
0 1 2 3 4 5 6 7 8 9 10 11 12
Docsity.com