0% found this document useful (0 votes)
38 views

Data Structures Lab 11 AVL Trees

Uploaded by

doravivek6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Data Structures Lab 11 AVL Trees

Uploaded by

doravivek6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Data Structures Lab 11 - Efficient

Binary Search Trees - AVL Trees


Balanced Binary Search Trees
 The worst case time complexities of Binary Search trees can be reduced by
imposing restrictions on the height.
 A height balanced tree is represented with HB(K), where K is the difference
between the height of left sub-tree and the height of right sub-tree.
 K is called as Balance Factor.
 In HB(k) if k=0, then we call such binary search trees as Full Balanced Binary
Search Trees.
 Example:
AVL (Adelson-Velskii and Landis) Tree
 For any node in a Binary Search Tree T, In HB(k), if k = -1, 0, 1, then it is known
as an AVL tree.
 An AVL tree is binary search tree with a balance condition and it is a self-
balancing tree.
 Properties of AVL Trees: A binary tree is said to be an AVL tree if:
 It is a binary search tree
 For any node X, the difference between heights of left and right sub-trees can be -1, 0 or 1
AVL (Adelson-Velskii and Landis) Tree
AVL (Adelson-Velskii and Landis) Tree
 If there are n nodes in AVL tree, minimum height of AVL tree is floor(log 2n).
 If there are n nodes in AVL tree, maximum height can’t exceed 1.44*log 2n.
 If height of AVL tree is h, maximum number of nodes can be 2h+1 – 1.
 Minimum number of nodes in a tree with height h can be represented as:
N(h) = N(h-1) + N(h-2) + 1 for n>2 where N(0) = 1 and N(1) = 2.
 The complexity of searching, inserting and deletion in AVL tree is O(log n).

Why AVL Trees?


 Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h)
time where h is the height of the BST.
 The cost of these operations may become O(n) for a skewed Binary tree.
 If we make sure that height of the tree remains O(Log n) after every insertion and
deletion, then we can guarantee an upper bound of O(Log n) for all these operations.
 The height of an AVL tree is always O(Log n) where n is the number of nodes in the tree
AVL Tree Representation
 AVL tree can be implemented using the linked representation, but the
structure of the node would be:

 Finding the height of an AVL Tree


Rotations
 When the tree structure changes [i.e. with insertion or deletion], we need to modify the tree to
restore the AVL tree property.
 This can be done using single rotations or double rotations.
 Since an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the
height of a sub-tree by 1.
 So, if the AVL tree property is violated at a node X, it means that the heights of left(X) and
 right(X) differ by exactly 2.
 This is because, if we balance the AVL tree every time, then at any point, the difference in heights of
left(X) and right(X) differ by exactly 2.
 Rotations is the technique used for restoring the AVL tree property. This means, we need to apply the
rotations for the node X.
Observation:
 One important observation is that, after an insertion, only nodes that are on the path
from the insertion point to the root might have their balances altered, because only those nodes
have their sub-trees altered.

To restore the AVL tree property, we start at the insertion point and keep going to the root of the tree.
While moving to the root, we need to consider the first node that is not satisfying the AVL
property. From that node onwards, every node on the path to the root will have the issue.

Also, if we fix the issue for that first node, then all other nodes on the path to the root will automatically
satisfy the AVL tree property. That means we always need to care for the first node that is not satisfying the
AVL property on the path from the insertion point to the root and fix it.
Types of Violations
 Let us assume the node that must be rebalanced is X. X is the first node on the path from new node to
root, which has its balance factor violated

 Since any node has at most two children, and a height imbalance requires that X’s two sub-tree heights
differ by two, we can observe that a violation might occur in four cases:

1. An insertion into the left subtree of the left child of X (LL case) – Single Rotation
2. An insertion into the right subtree of the left child of X. (LR case) – Double Rotation
3. An insertion into the left subtree of the right child of X (RL case) – Double Rotation
4. An insertion into the right subtree of the right child of X (RR case) – Single Rotation

 Cases 1 and 4 are symmetric and easily solved with single rotations.
 Similarly, cases 2 and 3 are also symmetric and can be solved with double rotations (needs two single
rotations).
Case – 1 Left Left Rotation (LL rotation – Right Rotate)
 Node X is unbalanced because of an insertion into its left child’s left sub-tree
 Perform a Right rotation - LL rotation at X i.e. make X’s left child - W as its parent, and X becomes W’s
right child

 Example : 9 has become unbalanced after insertion of 7, hence perform one LL rotation-right rotate at 9

unbalanced

LL rotate at 9

Right rotate at 9
Case – 1 Left Left Rotation (LL rotation – Right Rotate)
 ALGORITHM:
struct Node *LLRotation(struct Node *p)
{
struct Node *pl=p->lchild;
struct Node *plr=pl->rchild;

pl->rchild=p;
p->lchild=plr;
p->height=NodeHeight(p);
pl->height=NodeHeight(pl);

if(root==p)
root=pl;
return pl;
}
We can also call this function as : Node* RotateRight(Node *x)
Case – 4 Right Right Rotation (RR rotation – Left Rotate)
 Node X is unbalanced because of an insertion into its right child’s right sub-tree
 Perform a Left rotation - RR rotation

 Example : 15 has become unbalanced after insertion of 29, hence perform one RR rotation-Left rotate at
15

unbalanced

RR rotate at 15

Left rotate at 15
Case – 4 Right Right Rotation (RR rotation – Left Rotate)
 ALGORITHM:
struct Node *RRRotation(struct Node *p)
{
struct Node *pr=p->rchild;
struct Node *prl=pr->lchild;

pr->lchild = p;
p->rchild = prl;

// Update height
p->height = NodeHeight(p);
pr->height = NodeHeight(pr);

// Update root
if (root == p){
root = pr;
}
return pr;
}
We can also call this function as : Node* RotateLeft(Node *x)
Case – 2 Left Right Rotation (LR rotation)
 Node Z is unbalanced because of an insertion into its left child’s right subtree (Double Rotate)
 Perform a RR rotation(left rotate) at X(left child of Z) and then a LL rotation(right rotate) at Z
unbalanced
Case – 2 Left Right Rotation (LR rotation)
 Example : 8 has become unbalanced after insertion of 7,
 7 has been inserted into node 8’s left child’s – Right sub-tree
 One RR (left) Rotation at node 5 ( left child of 8)
 One LL(right) Rotation at node 8
unbalanced

LL rotate at the
RR rotate at left node - 8
child - 5

right rotate at 8
Left rotate at 5
Case – 3 Right Left Rotation (RL rotation)
 Node X is unbalanced because of an insertion into its right child’s left subtree (Double Rotate)
 Perform a LL rotation(right rotate) at Z(Right child of X) and then a RR rotation(left rotate) at X

unbalanced
Case – 3 Right Left Rotation (RL rotation)
 Example : 4 has become unbalanced after insertion of 5,
 5 has been inserted into node 4’s Right child’s – Left sub-tree
 One LL (right) Rotation at node 7 ( right child of 4)
 One RR(left) Rotation at node 4
unbalanced

RR rotate at the
LL rotate at
node - 4
right child - 7

Left rotate at 4
Right rotate at 7
AVL Insertion Example 1
• Given an AVL Tree:

• Insert 7 : Still an AVL


AVL Insertion Example 1
• Insert 2: Not an AVL

unbalanced

• Case 1 : LL violation because 2 has been inserted into the left subtree of 8’s left child
• LL Rotate ( Right rotate) at 8
AVL Insertion Example 2
• Given an AVL Tree:

• Insert 7 : node 10 becomes unbalance( BF=2) , because of insertion into its left child’s right
sub-tree ( Case 2 : LR Rotation) – Double rotations
i) Perform a RR rotation(left rotate) at left child of 10 i.e 5
ii) Perform a LL rotation(right rotate) at node 10
AVL Insertion Example 2

Step ii - LL rotation at 10 Step i – RR rotation at 5


Steps for deletion from AVL Tree
1. Perform normal BST deletion
2. The current node must be one of the ancestors of the deleted node. Update
the height of the current node.
3. Get the balance factor of the current node
4. a. If the balance factor is greater than 1, the current node is unbalanced.
Find the type of violation, perform rotations to balance the tree.
– Get the balance factor (BF) of the node’s left sub-tree
» If the BF is >=0 the its left-left case
» Else it is left- right case

b.If the balance factor is less than -1,then the current node is unbalanced
Find the type of violation, perform rotations to balance the tree.
– Get the balance factor (BF) of the node’s right sub-tree
» If the BF is <=0 the its right-right case
» Else it is right- left case
Example 1

Delete node 32

Balance factor of node 44 < -1


Balance Factor of right sub-tree(78) = 1

Therefore, Right-Left Case :


i) Left-Left(right) rotation at 78
ii) Right-Right(left) rotation at 44
Example 2

Delete node 12 Balance factor of node 11 > 1


Balance Factor of left sub-tree(3) = 0

Therefore, Left-Left Case :


i) Left-Left(right) rotation at 11
Time Complexities

Algorithm Average Worst case


Search O(log n) O(log n)

Insert O(log n) O(log n)

Delete O(log n) O(log n)


Applications of AVL Trees
AVL trees are applied in the following situations:
 There are very few insertion and deletion operations
 Short search time is needed
 Input data is sorted or nearly sorted

Example Situation: Trains in a railway system


newer trains come up very few every year and the inserted
information remains constant for a good period of time, an AVL
implementation of this would be better than any other tree for
searching.
Exercise
1) Implementation of operations on AVL Tree.
a) Create b) Insert c) Delete d) Traverse
Pre Lab Questions
• Define AVL tree.
• Given an empty AVL tree, how would you
construct AVL tree when a set of numbers
are given without performing any rotations?
• Why we need to a binary tree which is
height balanced?
• What is the maximum height of an AVL tree
with p nodes?
• Give the procedure to restore the AVL
property after inserting a element.

You might also like