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

AVL.ppt

This document covers AVL trees, a type of self-balancing binary search tree where the heights of the two sub-trees of a node differ by at most one. It explains the concept of balance factors, the insertion and deletion algorithms, and the necessary rotations to maintain balance after these operations. The document also includes examples and properties of AVL trees, emphasizing their efficiency in search, insertion, and deletion operations with a time complexity of O(log n).

Uploaded by

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

AVL.ppt

This document covers AVL trees, a type of self-balancing binary search tree where the heights of the two sub-trees of a node differ by at most one. It explains the concept of balance factors, the insertion and deletion algorithms, and the necessary rotations to maintain balance after these operations. The document also includes examples and properties of AVL trees, emphasizing their efficiency in search, insertion, and deletion operations with a time complexity of O(log n).

Uploaded by

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

LECTURE 23 AVL TREE

CHAPTER 10 OF TEXTBOOK
1. BINARY SEARCH TREE (BST)
2. THREADED TREE
3. AVL TREE
4. RED BLACK TREE
5. SPLAY TREE
Balanced BST
Balanced BST, i.e. height of left and right subtrees 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)

A BST is not balanced in general !


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
1
45
Balanced AVL tree
Left heavy AVL tree 1 0
45
0
36 63 0
0
36
63
0
1 27 0 0
39 54 72 0 0
27 0
39
54 72
0
0 18

Right heavy AVL tree


-1
45
0 -1

36 63
0 1
0 0
27 72
39 54

0
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

• Property: height of balanced tree of n nodes is O(log n).


Height Balanced Tree
Property: height of balanced tree of n nodes is O(log n).
Proof:
𝑁(ℎ) denote the minimum number of AVL tree of height h, then
𝑁(ℎ) <= n
𝑁(ℎ)=1+𝑁(ℎ−1)+𝑁(ℎ−2)
We may assume that 𝑁(ℎ−1)>𝑁(ℎ−2),
𝑁(ℎ)>1+𝑁(ℎ−2)+𝑁(ℎ−2)=1+2⋅𝑁(ℎ−2)>2⋅𝑁(ℎ−2)
𝑁(ℎ)>2⋅𝑁(ℎ−2)
𝑁(ℎ)>2⋅𝑁(ℎ−2)>2⋅2⋅𝑁(ℎ−4)>2⋅2⋅2⋅𝑁(ℎ−6)>⋯>2ℎ/2
log𝑁(ℎ) > log 2ℎ/2
ℎ < 2 log 𝑁(ℎ) <= 2 log n
Thus, these worst-case AVL trees have height ℎ = O(log𝑛).
Height Balanced Tree
1
45 YES
1 YES 0
45
0
36 63 0
0
36
63
0
1 27 0 0
39 54 72 0 0
27 0
39
54 72
0
0 18

2
-1 YES -1
45 NO
45
0 36 63 0
-1
0
36 63
0
27 -1
1
0 0 39
27 72
39 54
-1

42
0
70
3. 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 Adelson-Velskii and Landis
– provided the height balance property and insertion and
deletion operations that maintain the property in time
complexity of O(log n)
Features of AVL Trees
• The key advantage of using an AVL tree is that it takes O(log n)
time to perform search, insertion and deletion operations in
average case as well as worst case (because the height of the tree
is limited to O(log n).

• 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
• 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, no special provisions need to be taken.


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.
▪ 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
Example of critical node
Cases of critical nodes
• Height of sub-tree where a new node is inserted can
increase at most 1.
• The balance factor can crease by 1 or decrease by 1, so
the balance factor of critical node is either 2 or -2.
• There are four types of rebalancing rotations and
application of these rotations depends on the position of
the inserted node with reference to the critical node.
Cases of critical nodes
• 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.
Cases of critical nodes LL rotation
LL rotation

2 0
x y
h+2 h+1
right-rotation
1 0

y T3 T1 x
h+1 h-1 h h

T1 T2 T2 T3
h h-1 h-1 h-1
Cases of critical nodes LL rotation
Cases of critical nodes RR rotation
RR Rotation

-2
x 0
y
h+2
h+1
left-rotation
-1
0

T1 y x T3
h-1 h+1 h h

T2 T3
T1 T2
h-1 h
h-1 h-1
Cases of critical nodes RR rotation
L-R-Rotation

2
x
x
-1
left-rotation z T4 right-rotation z
y T4

y y x
T1 z T3

T1 T2 T3 T4
T2 T3 T1 T2
Cases of critical nodes LR Rotation
Cases of critical nodes LR Rotation
R-L-Rotation

-2

x right-rotation x
z
1 left-rotation
T1 y T1 z x y

z T4
y
T2 T1 T2 T3 T4

T2 T3
T3 T4
Cases of critical nodes LR Rotation
AVL Example
AVL Example
Example of AVL tree insert
Insert 50, 25, 10, 5, 7, 3, 30, 20, 8, 15 into AVL tree
Example of AVL tree insert
Insert 50, 25, 10, 5, 7, 3, 30, 20, 8, 15 into AVL tree
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
AVL Deletion
RULES FOR DELETION

• R0-> LL • THE NODE DELETED IS TO RIGHT OR


LEFT
• R1->LL • CHECK B.F (PREVIOUS) NODE’S
SIBLING.
• R-1->LR
DELETION 2 POSSIBILITIES
• L0->RR
• LEAF NODE
• L1->RL • DELETION OF ROOT WITH 1 OR 2
CHILDS. (IN-ORDER PREDECESSOR
• L-1->RR VALUE IS REPLACED)
Deleting a Node from an AVL Tree
Example: Consider the AVL tree given below and delete 72 from it.

Case 1. R-rotation

1 2 -1
45 45 36
0 0 1

-1 0 27 45 1
36 63 36 63
1 1 0
-1
27 -1 0
72 27 39 18
39 0 39 63
0 -1
0 0
0 0
18 40 18 40
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
-1 39
1
36 63 -1 0
36 63 1
36 45
0 0 0 0 0
27
27 1 0 0
39 72 39
0 0 27 37 41 63
0 0
37 41 0
37 41
Deletion in AVL trees
Deletion in AVL trees
• DELETE 4, 8,6,5,2,1,7 FROM FOLLOWING AVL TREE

8
3

2 4 7 10

1 6 9 11

You might also like