AVL trees

AVL trees
Deliverables
AVL Tree definition
Rotations in AVL
AVL Insertion, deletion and access
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
2
Adelson-Velskii and Landis
These are also called height
balanced trees. AVL tree is a
binary search tree such that
for every internal node v of
T, the heights of the children
of v can differ by at most 1.
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
3
AVL trees
The height of an AVL tree T storing n keys is O(logn)
Pf: Minimum number of nodes in an AVL tree of height h
for n(1) = 1 Number of nodes in a tree of height 1
for n(2) = 2 Number of nodes in a tree of height 2
For h≥3 an AVL tree of height h contains the root node, one AVL
sub tree of height h-1 and the other AVL sub tree of height h-2
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
4
AVL tree height: Pf Cont.
n(h)= 1+ n(h-1)+n(h-2) we know that n(h-1) ≥n(h-2)
n(h)>2n(h-2)
>4n(h-4)
>8n(h-6)
> 2in(h-2i)
suppose i= h  1
2
n(h)> 2 h2 1 n(2) = 2h/2 is the minimum number of elements
h< 2log n(h) so height is O(logn)
Complex proof shows that AVL tree of n nodes has height at most
log1.63n
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
5
AVL side leaning
13
18
8
20
11
16
19
17
12
15
14
6/7/2012 8:54 PM
5
10
6
9
Copyright @ gdeepak.Com®
3
7
4
2
1
6
AVL tree properties
If closest leaf is at level k, all nodes at levels 1 .. k-2 have 2
children. In an AVL tree of height h, the leaf closest to the
root is at level (h+1)/2.
On the first (h-1)/2 levels the AVL tree is a complete binary
tree. After (h-1)/2 levels the AVL tree may start thinning out.
Number of nodes in the AVL tree is at least 2(h-1)/2 and at
most 2h. Search is same as BST
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
7
Important property
Adding or removing a node only upsets the heights on a single
path to the root
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
8
Left and Right Rotation
K1
K2
K1
K2
A
B
6/7/2012 8:54 PM
C
A
Copyright @ gdeepak.Com®
B
C
9
Steps of rotation-1
K1
K2
K1
K2
A
B
6/7/2012 8:54 PM
C
A
Copyright @ gdeepak.Com®
B
C
10
Steps of rotation-2
K1
K2
K1
K2
A
B
6/7/2012 8:54 PM
C
A
Copyright @ gdeepak.Com®
B
C
11
Steps of rotation-3
K1
K2
K1
K2
A
B
6/7/2012 8:54 PM
C
A
Copyright @ gdeepak.Com®
B
C
12
Steps of rotation-4
K1
K2
K1
K2
A
B
6/7/2012 8:54 PM
C
A
Copyright @ gdeepak.Com®
B
C
13
Steps of rotation-5
Also if there was a parent node of K2 pointing to K2, now should point
to K1
K1
K2
K1
K2
A
B
6/7/2012 8:54 PM
C
A
Copyright @ gdeepak.Com®
B
C
14
Insertion example
After inserting 1
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
15
Insertion rules
To ensure balance condition for AVL-tree,
after insertion of a new node, we back up the
path from the inserted node to root and check
the balance condition for each node.
If after insertion, the balance condition does
not hold in a certain node, we do either single
rotation or double rotation
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
16
Insertion Causing Imbalance
• An insertion into the sub tree An insertion into the sub tree
P (outside) - case 1
Q (inside) - case 3
Q (inside) - case 2
R (outside) - case 4
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
17
Single Rotation (case 1)
• HA=HB+1
• HB=HC
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
18
Single Rotation (case 4)
•
•
HA=HB
HC=HB+1
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
19
Single rotation does not work for case 2 and 3 ; do
Double Rotation: Case 2
HA=HB=HC=HD
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
20
Double Rotation step 2
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
21
Double Rotation: Combined
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
22
Double Rotation-case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
23
Insert 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
24
Insert 5
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
25
Deletion
Removing a node from an AVL Tree is the same as removing
from a binary search tree. However, it may unbalance the tree.
Similar to insertion, starting from the removed node we check all
the nodes in the path up to the root for the first unbalance node.
Use the appropriate single or double rotation to balance the tree.
May need to continue searching for unbalanced nodes all the way
to the root.
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
26
AVL deletion
Deletion:
Case 1: if X is a leaf, delete X
Case 2: if X has 1 child, use it to replace X
Case 3: if X has 2 children, replace X with its inorder
predecessor (and recursively delete it)
Rebalancing
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
27
Delete 55 case 1
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
28
Delete 50 Case 2
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
29
Delete 60 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
30
Delete 60 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
31
Delete 55 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
32
Delete 55 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
33
Delete 50 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
34
Delete 50 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
35
Delete 40 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
36
Delete 40 case 3
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
37
Delete 40 after rebalancing
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
38
Complexity Analysis
Find element, insert element, and remove element
operations all have complexity O(log n) for worst case
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
39
Questions, Comments and Suggestions
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
40
Question 1
Inserting into an AVL tree with n nodes requires (log n)
rotations
A) True
B) False
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
41
Question 2
An AVL tree is balanced, therefore a median of all elements
in the tree is always at the root or one of its two children.
A) True
B) False
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
42
Question 3
What order should we insert the elements {1,2,… 7} into an
empty AVL tree so that we don’t have to perform any
rotations on it?
6/7/2012 8:54 PM
Copyright @ gdeepak.Com®
43