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

AVL Tree

This document discusses binary search trees and AVL trees. It covers the advantages and disadvantages of binary search trees. It then explains how to calculate the height of a binary search tree and defines balance factors. The document introduces AVL trees as self-balancing binary search trees and describes their operations, including searching, inserting, and deleting nodes through rotations to maintain balance.

Uploaded by

R Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views

AVL Tree

This document discusses binary search trees and AVL trees. It covers the advantages and disadvantages of binary search trees. It then explains how to calculate the height of a binary search tree and defines balance factors. The document introduces AVL trees as self-balancing binary search trees and describes their operations, including searching, inserting, and deleting nodes through rotations to maintain balance.

Uploaded by

R Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Contents

• Advantages of BST
• Disadvantages of BST
• Calculate Height of a BST
• Height of left sub tree
• Height of right sub tree
• Balance Factor
• AVL Tree
• AVL Tree Operations
• AVL Tree Rotations
Advantages of Binary Search Tree
1. Binary Search only works for a fixed data set,
but Binary Search Trees can be used for
dynamic data sets also.
2. It can be used to find the sorted order of a
dynamic data set.
3. If elements are inserted/deleted randomly , it
is actually faster to do it in a binary search
tree than linked lists and arrays.
Disadvantages of Binary Search Trees
• The overall shape of the tree depends on the order
of insertion. If data is inserted in a sorted order or
the tree becomes heavier in one direction, the
insert/search operations become expensive.
• For storing left and right child of each node a lot
of space is required.
• The implementation of Binary Search Tree is not
much used since it cannot guarantee logarithmic
complexity. Hence, we have to use self balancing
implementations.
Calculating Height of a Tree
• Example:
8

10 15

20 25 30 45

40
• The height of a node is the number of edges present in
the longest path connecting that node to a leaf node.
• The Height of node 25 is 1.
• The Height of node 10 is 2.
• The Height of node 8 is 3.
Height of left sub tree
• Root of this tree is 8.
• Height of the left sub
Tree for node 8 is 3.
• Height of the left sub
Tree for node 10 is 1.
Height of right sub tree
• Root node 8.
• Height of right sub tree
For node 8 is 2.
• Height of right sub-tree
For node 15 is 1.
• Height of right sub-tree
For node 10 is 2.
Balanced Binary Search Tree
• Balanced BST, i.e. height of left and right sub trees are equal or not
much differences at any node.
• Example: A full binary tree of n nodes is balanced tree.
• The search in can be done in
log(n) time, O(log n).
Depth of recursion is O(log n)
Time complexity O(log n)
Space complexity O(log n)

In general a Binary Search Tree (BST)


is not balanced.
Balance factor
• The balance factor of a node is calculated by
subtracting the height of its right sub-tree from
the height of its left sub-tree.
• Balance factor = Height (left sub tree) – Height (right
sub tree)
Balance factor
• If the balance factor of a node is 1, then it means that the
left sub-tree of the tree is one level higher than that of the
right sub-tree. Such a tree is called Left-heavy tree.

• If the balance factor of a node is 0, then it means that the


height of the left sub-tree is equal to the height of its right
sub-tree.

• If the balance factor of a node is -1, then it means that the


left sub-tree of the tree is one level lower than that of the
right sub-tree. Such a tree is called Right-heavy tree.

• A node is unbalanced if its balance factor is not -1, 0, or 1.


Examples of Balance Factor

45

45
36 63
36
63
27
39 54 72
27 39
54 72
18

45

36 63

27 72
39 54

70
Height Balanced Tree
• A binary search tree in which every node has a
balance factor of -1, 0 or 1 is said to be height
balanced.
In another word: at any node, the difference of heights
of two sub-trees is at most 1

vProperty : height of balanced tree of n nodes is


O(log n).
Height Balanced Tree
45

45
36 63
36
63
27
39 54 72
27 39
54 72

18

45
45

36 63
36 63
27
27 72 39
39 54

42
70
AVL Trees
• AVL tree is a self-balancing binary search tree in which the
heights of the two sub-trees of a node may differ by at most
one.
• AVL tree is a height-balanced tree
• Self-balancing : re-balance after BST inserting or deleting
• AVL is named by its inventor G.M.Adelson-Velskii and
E.M.Landis
– provided the height balance property and insertion and deletion
operations that maintain the property in time complexity of
O(log n)
Operations on AVL Tree

• The structure of an AVL tree is same as that of


a binary search tree but with a little difference.
In its structure, it stores an additional variable
called the Balance Factor.
• Searching for a Node in an AVL Tree
• Inserting a Node in an AVL Tree
• Delete a Node from an AVL Tree
Searching for a Node in an AVL Tree

• Searching in an AVL tree is performed exactly the same


way as it is performed in a binary search tree.
• Because of the height-balancing of the tree, the search
operation takes O(log n) time to complete.
• Since the operation does not modify the structure of the
tree.
Inserting a Node in an AVL Tree
• Algorithm:
Step 1. insert new node as BST
Step 2: if not AVL tree, do re-balancing to derive an AVL tree
• A new node is inserted as the leaf node, so it will always have
balance factor equal to zero.
•The nodes whose balance factors will change are those which
lie on the path between the root of the tree and the newly inserted
node.
Inserting a Node in an AVL Tree
• The possible changes which may take place in any node on the
path are as follows:
§ Initially the node was either left or right heavy and after
insertion has become balanced.
§ Initially the node was balanced and after insertion has
become either left or right heavy.
§ Initially the node was heavy (either left or right)
and the new node has been inserted in the heavy
sub-tree thereby creating an unbalanced sub-tree.
Such a node is said to be a critical node.
§ The critical node is the unbalanced node of lowest
level
Example of Critical node
• Example : Consider the AVL tree given below and
insert 9 into it.

45 45
Critical node
36 63
36 63

27
27 39 54 72

39 54 72
18

18

9
Example of AVL Tree insert
• Insert 50,25,10,5,7,3,30,20,8,15 into AVL tree.
AVL Tree Rotations
• In AVL tree, after performing operations like
insertion and deletion we need to check
the balance factor of every node in the tree.
• If every node satisfies the balance factor
condition then we conclude the operation
otherwise we must make it balanced.
• Whenever the tree becomes imbalanced due to
any operation we use rotation operations to
make the tree balanced.
AVL Tree Rotations
• Rotation operations are used to make the tree
balanced.
• Rotation is the process of moving nodes either to left
or to right to make the tree balanced.
• There are four rotations and they are classified into
two types.
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 at 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.
Deleting a Node from an AVL Tree

• Deleting algorithm
Step 1. delete node as BST
Step 2. if not height balanced, rebalance by rotation
• If the resulting BST of step 1 is not height-
balanced, find the critical node. There are four
possible cases similar to the inserting. Then do
corresponding rotation by the case
Deleting a Node from an AVL Tree
• Case 1. R-rotation
2
1
45
45
0
36 63 0
36 63 -1
1
1
27 -1 27 39
72
39 0
-1
0 0 0
18 40
18 40 0

-1
36
1

27 45 1
0
-1
18 0
39 63

0
40
Deleting a Node from an AVL Tree
§ Consider the AVL tree given below and delete 72 from it.
§ Case 2. L-R-rotation
2
1
45
-1 45

-1
36 63 0
36 63
0 0 0
0 0
27
39 72 27
39
0 0
0 0
37 41 1
39 37 41
1

36 45 1

1 0 0

27 37 41 63

0
AVL implementation node design

struct Node
{
int data;
int height; // or using balance factor
int data; // application data associated with data
struct Node *left, *right;
};
AVL right-rotate
node *right_rotate(node *y) {
node *x = y->left; y x
node *T2 = x->right; / \ Right Rotation / \
x T3 – – – – – – – > T1 y
// perform rotation /\ <------- /\
T1 T2 Left Rotation T2 T3
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
return x;
}
AVL left-rotate
node *left_rotate(node *x)
{ y x
node *y = x->right; / \ Right Rotation / \
x T3 – – – – – – – > T1 y
node *T2 = y->left; /\ <------- /\
// perform rotation T1 T2 Left Rotation T2 T3

y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
return y;
}
AVL insert
node* insert(node* root, int data) {
if (root == NULL) return(new_node(data));
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
else return root;

root->height = 1 + max(height(root->left), height(root->right));


int balance = balance_factor(root);

// do rotation by cases
AVL implementation insert
if (balance > 1 && balance_factor(root->left) >=0 )
return right_rotate(root);
if (balance < -1 && balance_factor(root->left) <=0 )
return left_rotate(root);
if (balance > 1 && balance_factor(root->left) < 0) {
root->left = left_rotate(root->left);
return right_rotate(root);
}
if (balance < -1 && balance_factor(root->left) > 0) {
root->right = right_rotate(root->right);
return left_rotate(root);
}
return root;
}

You might also like