Module III
Module III
Contents
1. TRANSFORM-AND-CONQUER .......................................................................................................1
1.1 BALANCED SEARCH TREES.........................................................................................................2
1.2 HEAPS AND HEAPSORT..................................................................................................................4
2. SPACE AND TIME TRADEOFFS:.....................................................................................................6
2.1 SORTING BY COUNTING..........................................................................................................7
2.2 TOPOLOGICAL SORTING........................................................................................................8
LECTURE 23:
There are three major variations of this idea that differ by what we transform a given instance
to :
Computer scientists have come up with two approaches to preserves the good properties of the
classical binary search tree are:
AVL Trees:
AVL trees were invented in 1962 by two Russian scientists, G. M. Adelson-Velsky and E. M.
Landis [Ade62], after whom this data structure is named.
An AVL tree is a binary search tree in which the balance factor of every node, which is defined as
the difference between the heights of the node’s left and right subtrees, is either 0 or +1 or −1. The
height of the empty tree is defined as −1.
For example, the binary search tree in Figure 6.2a is an AVL tree but the one in Figure 6.2b is not.
Review Questions:
An example of constructing an AVL tree for a given list of numbers is shown in the above figure.
If there are several nodes with the ±2 balance, the rotation is done for the tree rooted at the
unbalanced node that is the closest to the newly inserted leaf.
As with any search tree, the critical characteristic is the tree’s height. It turns out that it is bounded
both above and below by logarithmic functions.
[log 2 n] ≤ h < 1.4405 log 2(n + 2) − 1.3277.
Review Questions:
1. How to construct an AVL Tree?
2. What is the drawback of AVL trees?
3. What is the advantage of AVL trees?
Review Questions:
1. What is 2-node ?
2. Define 3-node.
3. What is the upper bound of 2-3 tree?
4. What is the lower bound of 2-3 tree?
5. What is the efficiency for insertion in 2-3 tree?
The data structure called the “heap” is, partially ordered data structure that is especially suitable for
implementing priority queues. Priority queue is a multiset of items with an orderable characteristic
called an item’s priority, with the following operations:
1. The shape property—the binary tree is essentially complete (or simply complete), i.e., all its
levels are full except possibly the last level, where only some rightmost leaves may be missing.
2. The parental dominance or heap property—the key in each node is greater than or equal to the
keys in its children.
For example, consider the trees of above figure. The first tree is a heap. The second one is not a
heap, because the tree’s shape property is violated. And the third one is not a heap, because the
parental dominance fails for the node with key 5.
Key values in a heap are ordered top down; i.e., a sequence of values on any path from the root to a
leaf is decreasing. However, there is no left-to-right order in key values; i.e., there is no relationship
1. There exists exactly one essentially complete binary tree with n nodes. Its height is equal to
[log2n]
2. The root of a heap always contains its largest element.
3. A node of a heap considered with all its descendants is also a heap.
4. A heap can be implemented as an array by recording its elements in the top-down, left-to-right
fashion. It is convenient to store the heap’s elements in positions 1 through n of such an array,
leaving H [0] either unused or putting there a sentinel whose value is greater than every element in
the heap. In such a representation,
a. the parental node keys will be in the first [n/2] positions of the array, while the leaf keys
will occupy the last [n/2] positions;
b. the children of a key in the array’s parental position i (1 ≤ i ≤ [n/2]) will be in positions
2i and 2i + 1, and, correspondingly, the parent of a key in position i (2 ≤ i ≤ n) will be in
position [i/2].
A heap can be defined as an array H [1..n] in which every element in position i in the first half of the
array is greater than or equal to the elements in positions 2i and 2i + 1, i.e.,
Review Questions:
1. What is priority queue?
2. Define Heap.
3. What element does the root of heap contain?
4. How is heap represented using an array?
There are two principal alternatives for constructing a heap for a given list of keys.
The first is the bottom-up heap construction algorithm as illustrated in figure
How efficient is this algorithm in the worst case? Assume, for simplicity, that n = 2 k − 1 so that a
heap’s tree is full, i.e., the largest possible number of nodes occurs on each level. Let h be the
height of the tree. According to the first property of heaps in the list at the beginning of the
The alternative algorithm constructs a heap by successive insertions of a new key into previously
constructed heap; To insert a new key K into a heap first, attach a new node with key K in it after
the last leaf of the existing heap. Then shift K up to its appropriate place in the new heap as
follows. Compare K with its parent’s key: if the latter is greater than or equal to K, stop otherwise,
swap these two keys and compare K with its new parent. This swapping continues until K is not
greater than its last parent or it reaches the root
Since the height of a heap with n nodes is about log 2 n, the time efficiency of insertion is in O(log
n).
Inserting a key
Deleting the root’s key from a heap can be done with the following algorithm, illustrated in figure
Review Questions
1. What is bottom up heap construction?
2. What is the total number of key comparisons in the worst case?
3. What is top down heap construction?
4. What is the time efficiency for insertion?
5. What is the time efficiency for deletion?
HEAPSORT
Heapsort is a two-stage algorithm that works as follows.
Stage 1 (heap construction): Construct a heap for a given array.
Stage 2 (maximum deletions): Apply the root-deletion operation n − 1 times to the remaining
heap.
As a result, the array elements are eliminated in decreasing order. But since under the array
implementation of heaps an element being deleted is placed last, the resulting array will be exactly
the original array sorted in increasing order. Heapsort is traced on a specific input in the figure
This means that C(n) ∈ O(n log n) for the second stage of heapsort. For both stages, we get O(n)
+ O(n log n) = O(n log n). The time efficiency of heapsort is, in fact, in θ(n log n) in both the
worst and average cases. Thus, heapsort’s time efficiency falls in the same class as that of
mergesort.