Module 4.5(AVL Trees)
Module 4.5(AVL Trees)
Consider this figure and insert a new node with Note that there are three nodes in the tree that have
the value 71 and the new tree will be….. their balance factors 2, –2, and –2, thereby
disturbing the AVLness of the tree. So, here comes the
need to perform rotation.
To perform rotation:
• first task is to find the critical node. Critical node is the nearest ancestor node on
the path from the inserted node to the root whose balance factor is neither –1, 0,
nor 1. In the tree given above, the critical node is 72. The
• second task in rebalancing the tree is to determine which type of rotation has to
be done.
The four categories of rotations are:
LL rotation The new node is inserted in the left sub-tree of the left sub-tree of the
critical node.
RR rotation The new node is inserted in the right sub-tree of the right sub-tree of the
critical node.
LR rotation The new node is inserted in the right sub-tree of the left sub-tree of the
critical node.
RL rotation The new node is inserted in the left sub-tree of the right sub-tree of the
critical node
LL rotation
• Tree (a) is an AVL tree. In tree (b), a
new node is inserted in the left sub-tree
of the left sub-tree of the critical node A
(node A is the critical node because it is
the closest ancestor whose balance
factor is not –1, 0, or 1), so we apply LL
rotation as shown in tree (c).
1 3-1=2 0
2-1=1
45 45 37 63
0 1-2=-1 0 1
0
36 63 36 63 39
36
0
0 0-1=-1 0
0 37 27
0 37
27 27
1st : Left shift of right subtree
0
39
0
37
1
0 Now it’s a Balanced tree, this LR rotation have two
36 45 rotation so it is also called as double rotation
0
0 39 0
27 39
RL rotation
Tree (a) is an AVL tree. In tree (b), a
new node is inserted in the left sub-
tree of the right sub-tree of the
critical node A.
so we apply RL rotation as shown in
tree (c). Note that the new node has
now become a part of tree T2 . While
rotation, node C becomes the root,
with A and B as its left and right
children. Node A has T1 and T2 as its
left and right sub-trees and T3 and T4
become the left and right sub-trees of
node B.
Insert node with the value 92 for the given tree.
Example : Construct an AVL tree by inserting the following elements in the given order. 63,
9, 19, 27, 18, 108, 99, 81.
Example: Construct an AVL tree by inserting the following elements in the
given 14, 17, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20.
Create an AVL tree using the following sequence of data: 16, 27, 9, 11, 36, 54,
81, 63, 72.
Deleting a Node from an AVL Tree
• Deletion of a node in an AVL tree is similar to that of binary search trees.
• Deletion may disturb the AVLness of the tree, so to rebalance the AVL tree,
we need to perform rotations.
• There are two classes of rotations that can be performed on an AVL tree
after deleting a given node. These rotations are R rotation and L rotation.
• Let B be the root of the left or right sub-tree of A (critical node). R0 rotation is applied if the
balance factor of B is 0. This is illustrated in Fig.
• Tree (a) is an AVL tree. In tree (b), the node X is to be deleted from the right sub-tree of the
critical node A.
• Since the balance factor of node B is 0, we apply R0 rotation as shown in tree (c). During the
process of rotation, node B becomes the root, with T1 and A as its left and right child. T2 and T3
become the left and right sub-trees of A.
Example : Consider the AVL tree given in Fig. and delete 72 from it.
R1 Rotation
1. Create a binary search tree with the input given below: 98, 2, 48, 12, 56, 32, 4, 67, 23,
87, 23, 55, 46
(a) Insert 21, 39, 45, 54, and 63 into the tree
(b) Delete values 23, 56, 2, and 45 from the tree
Binary Heap
• A binary heap is a complete binary tree in which every node satisfies the heap
property which states that:
• This implies that elements at every node will be either greater than or equal to the
element at its left and right child. Thus, the root node has the highest key value in
the heap. Such a heap is commonly known as a max-heap.
• elements at every node will be either less than or equal to the element at its left
and right child. Thus, the root has the lowest key value. Such a heap is called a min-
heap.
It follows the same rules as that of a complete binary tree. That is, if an
element is at position i in the array, then its left child is stored at position 2i
and its right child at position 2i+1.
Being a complete binary tree, all the levels of the tree except the last level
are completely filled.
The height of a binary tree is given as log2 n, where n is the number of
elements.
Heaps (also known as partially ordered trees) are a very popular data
structure for implementing priority queues
Inserting a New Element in a Binary Heap
• Consider a max heap H with n elements. Inserting a new value into the heap is done in
the following two steps:
1. Add the new value at the bottom of H in such a way that H is still a complete binary
tree but not necessarily a heap.
2. Let the new value rise to its appropriate place in H so that H now becomes a heap as
well.
• To do this, compare the new value with its parent to check if they are in the correct order.
• If they are, then the procedure halts, else the new value and its parent’s value are
swapped and Step 2 is repeated
Example : Consider the max heap given in Fig. and insert 99 in it.
Example : Build a max heap H from the given set of numbers: 45, 36, 54, 27,
63, 72, 61, and 18.
Memory representation of the heap.
Algorithm to insert an element in a max heap
We assume that H with n elements is stored in
array HEAP. VAL has to be inserted in HEAP. The
location of VAL as it rises in the heap is given by
POS, and PAR denotes the location of the parent of
VAL.
Note that this algorithm inserts a single value in
the heap. In order to build a heap, use this
algorithm in a loop. For example, to build a heap
with 9 elements, use a for loop that executes 9
times and in each pass, a single value is inserted.
The complexity of this algorithm in the average
case is O(1). This is because a binary heap has
O(log n) height. Since approximately 50% of the
elements are leaves and 75% are in the bottom
two levels, the new element to be inserted will
only move a few levels upwards to maintain the
heap.
In the worst case, insertion of a single value may take
O(log n) time and, similarly, to build a heap of n
elements, the algorithm will execute in O(n log n) time
Deleting an Element from a Binary Heap
Consider a max heap H having n elements. An element is always deleted from the root of the
heap. So, deleting an element from the heap is done in the following three steps:
1. Replace the root node’s value with the last node’s value so that H is still a complete binary tree
but not necessarily a heap.
3. Sink down the new root node’s value so that H satisfies the heap property. In this step,
interchange the root node’s value with its child node’s value (whichever is largest among its
children).
Here, the value of root node = 54 and the value of the last node = 11. So, replace 54 with 11 and
delete the last node.
Example : Consider the max heap H shown in Fig. and delete the root node’s
value
Algorithm to delete the root element from a max heap