2 - 5 AVL Trees and Heaps
2 - 5 AVL Trees and Heaps
2 - 5 AVL Trees and Heaps
CIC – 209:
DATA STRUCTURES
Unit – II
AVL Trees and Heaps
AVL Trees
■ AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named
AVL in the honour of its inventors.
■ AVL Tree can be defined as height balanced binary search tree in which each node is
associated with a balance factor which is calculated by subtracting the height of its
right sub-tree from that of its left sub-tree.
■ Tree is said to be balanced if balance factor of each node is in between -1 to 1,
otherwise, the tree will be unbalanced and need to be balanced.
■ An AVL tree is a binary search tree in which:
1. the heights of the right subtree and left subtree of the root differ by at most 1
2. the left subtree and the right subtree are themselves AVL trees
3. A node is said to be
■ left-high (/ or 1) - if the left subtree has greater height
■ right-high (\ or -1) - if the right subtree has greater height
■ Equal (- or 0) - if the heights of the LST and RST are the same
2
1
11/3/2022
AVL Rotations
■ The rotation in AVL tree shall be performed only in case if Balance Factor is
other than -1, 0, and 1.
■ There are basically four types of rotations which are as follows:
1. LL rotation: Inserted node is in the left subtree of left subtree of A
2. RR rotation: Inserted node is in the right subtree of right subtree of A
3. LR rotation: Inserted node is in the right subtree of left subtree of A
4. RL rotation: Inserted node is in the left subtree of right subtree of A
■ Where node A is the node whose balance Factor is other than -1, 0, 1.
■ The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations.
■ For a tree to be unbalanced, minimum height must be at least 2.
2
11/3/2022
■ In above example, node A has balance factor -2 because a node C is inserted in the
right subtree of A right subtree. Therefore, RR rotation is performed.
3
11/3/2022
4
11/3/2022
Example: T1, T2 and T3 are subtrees of the tree rooted with y (on the left side) or x (on
the right side)
y x
/ \ Right Rotation / \
x T3 -------> T1 y
/\ <------- /\
T1 T2 Left Rotation T2 T3
Keys in both of the above trees follow the following order
keys(T1) < key(x) < keys(T2) < key(y) < keys(T3)
So BST property is not violated anywhere.
9
10
5
11/3/2022
6
11/3/2022
13
Insertions Examples
14
7
11/3/2022
Insertions Example - 1
15
Insertions Example - 2
16
8
11/3/2022
Insertions Example - 3
17
Insertions Example - 4
18
9
11/3/2022
20
10
11/3/2022
22
11
11/3/2022
Deletion Scenario - 1
R0 Rotation is to be performed If the node B has 0 balance factor, and the balance factor of node
A disturbed upon deleting the node X, then the tree will be rebalanced by rotating tree using Right
rotation. The critical node A is moved to its right and the node B becomes the root of the tree with
T1 as its left sub-tree. The sub-trees T2 and T3 becomes the left and right sub-tree of the node A.
the process involved in right rotation is shown in the following image.
23
Deletion Example - 1
Problem: Delete the node 30 from the AVL tree shown in the following image.
24
12
11/3/2022
Deletion Example - 1
Solution: In this case, the node B has balance factor 0, therefore the tree will be
rotated by using R0 rotation as shown in the following image. The node B(10)
becomes the root, while the node A is moved to its right. The right child of node B
will now become the left child of node A.
25
Deletion Scenario - 2
R1 Rotation is to be performed if the balance factor of Node B is 1. In R1 rotation, the critical
node A is moved to its right having sub-trees T2 and T3 as its left and right child respectively. T1 is
to be placed as the left sub-tree of the node B. The process involved in R1 rotation is shown in the
following image.
26
13
11/3/2022
Deletion Example - 2
Problem: Delete Node 55 from the AVL tree shown in the following image.
27
Deletion Example - 2
Solution: Deleting 55 from the AVL Tree disturbs the balance factor of the node
50 i.e. node A which becomes the critical node. This is the condition of R1
rotation in which, the node A will be moved to its right (shown in the image below).
The right of B is now become the left of A (i.e. 45). The process involved in the
solution is shown in the following image.
28
14
11/3/2022
Deletion Scenario - 3
R-1 rotation is to be performed if the node B has balance factor -1. This case is treated in the same way as
LR rotation. In this case, the node C, which is the right child of node B, becomes the root node of the tree
with B and A as its left and right children respectively. The sub-trees T1, T2 becomes the left and right sub-
trees of B whereas, T3, T4 become the left and right sub-trees of A. The process involved in R-1 rotation is
shown in the following image.
29
Deletion Example - 3
Problem: Delete the node 60 from the AVL tree shown in the following image.
30
15
11/3/2022
Deletion Example - 3
Solution: In this case, node B has balance factor -1. Deleting the node 60,
disturbs the balance factor of the node 50 therefore, it needs to be R-1 rotated.
The node C i.e. 45 becomes the root of the tree with the node B(40) and A(50) as
its left and right child.
31
32
16
11/3/2022
Heap
■ Heap is a data structure, which permits one to insert elements into a set and
also to find the largest element efficiently.
■ Heaps (occasionally called as partially ordered trees) are a very popular data
structure for implementing priority queues.
■ A data structure, which provides these two operations, is called a priority
queue.
■ A heap is either a min-heap or a max-heap.
■ A min-heap supports the insert and deletemin operations while a max-heap
supports the insert and deletemax operations.
■ Heaps could be binary or d-ary.
■ Binary heaps are special forms of binary trees while d-ary heaps are a special
class of general trees.
■ Binary heaps were first introduced by Williams in 1964.
34
17
11/3/2022
35
36
18
11/3/2022
37
38
19
11/3/2022
Example
■ Form a heap using the above algorithm for the data: 40, 80, 35, 90, 45, 50, 70.
40
20
11/3/2022
Example
41
42
21
11/3/2022
43
44
22
11/3/2022
Example
■ Here the root node is 99. The last node is 26, it is in the level 3. So, 99 is replaced by
26 and this node with data 26 is removed from the tree. Next 26 at root node is
compared with its two child 45 and 63. As 63 is greater, they are interchanged. Now,
26 is compared with its children, namely, 57 and 42, as 57 is greater, so they are
interchanged. Now, 26 appears as the leave node, hence re-heap is completed.
45
46
23
11/3/2022
Example
47
48
24