Você está na página 1de 5

AVL Tree

AVL tree is a self-balancing Binary Search Tree (BST) where the


difference between heights of left and right subtrees cannot be more
than one for all nodes. The AVL tree is named after its
two Soviet inventors, G. M. Adelson-Velskii and E. M. Landis, who
published it in their 1962 paper "An algorithm for the organization of
information".

Definition
An empty binary tree, T is an AVL tree. If T is a non-empty binary tree with T L and TR as its left and right
subtrees, then T is an AVL tree iff
1. TL and TR are AVL tree and
2. | hR-hL | 1, where hL and hR are heights of TL and TR.
[hR-hL is also called the balance factor with possible values : -1, 0, 1]
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(Logn) after every insertion and deletion, then we can guarantee an
upper bound of O(Logn) for all these operations. The height of an AVL tree is always O(Logn) where n is
the number of nodes in the tree.
Insertion
To make sure that the given tree remains AVL after every insertion, we must augment the standard BST
insert operation to perform some re-balancing. Following are two basic operations that can be performed
to re-balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
1) Left Rotation
2) Right Rotation
T1, T2 and T3 are subtrees of the tree rooted with y (on left side) or x (on right side)
y
x
/\
Right Rotation(y)
/ \
x T3
T1 y
/\
/ \
T1 T2
Left Rotation (x)
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.
Steps to follow for insertion
Let the newly inserted node be w. Let z be the first unbalanced node, y be the child of z that comes on the
path from w to z and x be the grandchild of z that comes on the path from w to z.
1) Perform standard BST insert for w.
2) Starting from w, travel up and find the first unbalanced node.
3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z.
There can be 4 possible cases that need to be handled as x, y and z can be arranged in 4 ways:
a) y is left child of z and x is left child of y
(Left Left Case)
b) y is left child of z and x is right child of y
(Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)

d) y is right child of z and x is left child of y

(Right Left Case)

Following are the operations to be performed in above mentioned 4 cases. In all of the cases, we only
need to re-balance the subtree rooted with z and the complete tree becomes balanced as the height of
subtree (After appropriate rotations) rooted with z becomes same as it was before insertion.
Four possible cases
left-left/right-right solution is a Single rotation:
Suppose we have a tree that looks like:
c
/
b
Now insert the item a and get the resulting left-left binary tree:
c
/
b
/
a
this resulting tree violates the AVL criteria, the left subtree has a height of 2 but the right subtree has a
height 0. The difference in the two heights is 2 (which is greater than 1). So we have to perform a single
rotation (right-rotate w.r.t c) on the tree to get :
b
/ \
a c
Similarly, a right-right tree of the form c-b-a can be solve by a left-rotate w.r.t. c obtaining the above
result.
Right-left/left-right - solution is a double rotation:
Suppose we have a tree that looks like:
a
\
c
/
b
which is the result of inserting b to the tree : a-c. this resulting tree also violate the AVL criteria. So
we fix it by first rotating c to the right (so that we get a-b-c), and then rotating a down to the left so
that the tree is transformed into:
b
/
a

\
c

Q. What is a height-balanced tree? Define balance factor. Construct an AVL tree for the following keys
9, 14, 6, 23, 8, 11, 5, 24, 1, 77, 3, 10, 38, 40, 2.
Height-balanced tree: A tree where no leaf is much farther away from the root than any other leaf.
Different balancing schemes allow different definitions of much farther and different amounts of work
to keep them balanced.
There are different height-balancing schemes. AVL follows the following conditions to check to
determine if a binary tree is balanced.
An empty tree is height-balanced. A non-empty binary tree T is balanced if:

1) Left subtree of T is balanced


2) Right subtree of T is balanced
3) The difference between heights of left subtree and right subtree is not more than 1.

30

50
/ \
30 60
/ \ /
17 40 55

/
17

\
50
/ \
40 60
/
55
Not a height balanced tree

Not a height balanced tree

Define balance factor: The difference between heights of left subtree and right subtree
Insert the number to an empty AVL tree: 17 30 40 50 60 55
Insert 17
17
insert 30

17
\
30

insert 40

17
\
30
\
40

Insert (50)

30
/ \
17 40

LR(17)
-------->

30
/ \
17 40
\
50

Insert (60)

30

30

/ \
17 40
\
50
\
60
Insert (55)

/ \
17 50
/ \
40 60

LR(40)
-------->

30
/
17

Insert (57)

\
50
/ \
40 60
/
55

LR(30)
-------->

50
/
30

50
/ \
30 60
/ \ /
17 40 55

50
\
60

LR(55)

/
30

50
\
60

RR(60)

/
30

\
57

/ \
/
17 40 55

-------->

/ \ /
17 40 57
/
55

\
57

Insert : 9, 14, 6, 23, 8, 11, 5, 24, 1, 77, 3, 10, 38, 40, 2.


Result of insertion of 9, 14, 6, 23, 8, 11, 5, 24 is
_9_
/
\
6
14
/ \
/ \
5 8 11 23
/
\
1
24
Insert 77
_9_
/
\
6
14
/ \
/ \
5 8 11 23
/
\
1
24
\
77
Insert 3
_9_
/
\
6
14
/ \ / \
5 8 11 24
/
/ \
1
23 77
\
3
Insert 10, 38, 40
_9_
/
\
6
14
/ \ / \
3 8 11 24
/ \
/ / \
1 5 10 23 77
/
38
\
40
Insert (2)

_9_
/
\
6
14
LR(23)
/ \
/ \
5 8 11 24
/
/ \
1
23 77

_9_
/
6

\
14
/ \ / \
3 8 11 24
/ \
/ \
1 5
23 77

_9_
/
6

\
14
/ \ / \
3 8 11 24
/ \
/ / \
1 5 10 23 40
/ \
38 70

-------->

/ \ / \
17 40 55 60

_9_
/
6

\
14
/ \ / \
3 8 11 24
/ \
/ / \
1 5 10 23 40
\
/ \
2
38 70

_9_
/
3
/
1
\
2

\
14
\
/ \
6 11 24
/ \ / / \
5 8 10 23 40
/ \
38 70

Você também pode gostar