Você está na página 1de 55

AVL Trees

By
Jyostna Devi Bodapati
Binary Search Tree - Best
Time
• All BST operations are O(d), where d is
tree depth
• minimum d is for a binary tree
with N nodes
› What is the best case tree?
› What is the worst case tree?
• So, best case running time of BST
operations is O(log N)
Binary Search Tree - Worst
Time
• Worst case running time is O(N)
› What happens when you Insert elements in
ascending order?
• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
› Problem: Lack of “balance”:
• compare depths of left and right subtree
› Unbalanced degenerate tree
Balanced and unbalanced BST
1 4
2 2 5
3
1 3
4
4 Is this “balanced”?
5
2 6 6

1 3 5 7 7
Balanced Binary Trees
› AVL trees
› Red black trees
› Splay trees
› B-trees
› B+ trees
AVLTree
• AVL is invented by G.M. Adelson-Velsky and
E.M. Landis in 1962.

• In an AVL tree, the heights of the two sub-trees


of a node may differ by at most one. Due to this
property, the AVL tree is also known as a height-
balanced tree.
• An empty tree has height of –1
• A tree with a single node has height of 0
• The structure of an AVL tree is the same as that
of a binary search tree but with a little difference.
AVLTree
• AVL tree stores an additional variable called
the Balance Factor.
• Thus, every node has a balance factor
associated with it.
Balance factor of a node:
› height(left subtree) - height(right subtree)
› A binary search tree in which every node has a
balance factor of –1, 0, or 1 is said to be height
balanced.
› A node with any other balance factor is
considered to be unbalanced and requires
rebalancing of the tree.
AVL tree examples
Tree A (AVL) Tree B (AVL)
height=2 BF=1-0=1 2
6 6
1 0 1 1
4 9 4 9
0 0 0 0 0
1 5 1 5 8

height of node = h
balance factor = hleft-hright
empty height = -1
AVL tree examples
Tree A (AVL) Tree B (not AVL)
balance factor
2 3 1-(-1) = 2
6 6
1 1 1 2
4 9 4 9
0 0 0 0 0 1 -1
1 5 7 1 5 8
0
7
height of node = h
balance factor = hleft-hright
empty height = -1
Examples of AVLTree
Operations on AVLTree

› Search
› Insertion
› Deletion
› Traversal
Search operation on AVLTree
 Searching in an AVL tree is performed exactly the
same way as it is performed in a binary search tree.

 Due to the height-balancing of the tree, the search


operation takes O(log n) time to complete.
Insertion in AVL Trees
 Insertion in an AVL tree is same as insertion in
BST
 But the insertion is usually followed by an
additional step called rotation.
 Rotation is done to restore the balance of the
tree.
 After insertion, if the balance factor of every
node is still –1, 0, or 1, then rotations are not
required.
Maintaining Balance
 Consider this AVL tree
 Insert 15

 No change in the height


 So tree remains balanced
Maintaining Balance
 Consider this AVL tree
 Insert 42

 No change in the height


 So tree remains balanced
Maintaining Balance
 Consider this AVL tree
 Insert 23
Maintaining Balance
The heights of each of the sub-trees
from here to the root are increased by
one
Maintaining Balance
However, only two of the nodes are unbalanced:
17 and 36

Critical node: it is the closest ancestor to the newly inserted node


whose balance factor is not –1, 0, or 1)
Maintaining Balance
However, only two of the nodes are unbalanced: 17
and 36
We only have to fix the imbalance at the lowest node
Maintaining Balance
We can promote 23 to where 17 is, and
make 17 the left child of 23
Maintaining Balance
Thus, that node is no longer
unbalanced
Incidentally, neither is the root now balanced
again, too
Rotations

Rotation is done to restore the balance of the tree.


LL Rotation
 Imbalance is due to the insertion in the right subtree of the right
child of critical node
LL Rotation: General form
LL Rotation: Example
RR Rotation

 Imbalance occurs due to the insertion of a new node in the left


subtree of the left child of critical node
RR Rotation: General form
RR Rotation: Example

Insert 18 into the below tree


LR Rotation
Double left-right rotation: Combination of two rotations

perform left rotation

perform right rotation


LR Rotation

 Imbalance occurs due to the insertion of a new node in the right


subtree of the left child of critical node
LR Rotation: General form
LR Rotation: Example
RL Rotation

 Imbalance occurs due to the insertion of a new node in the left


subtree of the right child of critical node
RL Rotation: General form
If the balance factor of a node in an AVL tree is +2 or -2, the node is rotated to re-balance
the tree using one of the four cases shown in the following picture:

The 4 cases of AVL tree rebalancing using rotations. Picture


created by Marc Tanti, licensed for reuse under the GNU Free
Documentation License, Version 1.2

AVL Trees - Lecture Next: example -->


Initial AVL tree with balance factors: Balance ok
insert 1 -->
0
57

0 +1
26 72

-1
0 0 -1
25
38 63 94

0
0
3 0 0
37
47 78

AVL Trees - Lecture


Balance not ok
Insert 1 and recalculate balance factors
(Balance factor of -2
-1
is not allowed)
57

-1 +1
26 72

-2
0 0 -1
25
38 63 94

-1
0
3 0 0
37
47 78

0
1

AVL TreesNext
- Lecture
step: Find rebalancing case -->
Find rebalancing case Balance not ok

-1
57

-1 +1
26 72

Left Left Case


-2
25 0 0 -1
38 63 94
Pivot

-1
3 0
37 0 0
47 78

0
1

AVLNext
Trees - Lecture
step: apply Left Left rebalancing -->
Rebalance and recalculate balance factors Balance ok

0
57

0 +1
26 72

0 0 0 -1
3 38 63 94

0 0 0
0 0
1 25 37
47 78

AVL Trees - Lecture Next step: insert 30 -->


Insert 30 and recalculate balance factors Balance ok

-1
57

+1 +1
26 72

0 -1 0 -1
3 38 63 94

0 0 -1
0 0
1 25 37
47 78

0
30
AVL Trees - Lecture Next step: Insert 32 -->
Insert 32 and recalculate balance factors Balance not ok

-2 57

+2 26 +1 72

0 3 -2 38 0 63 -1 94

0 1 0 25 -2 37 0 47 0 78

+1 30

0 32

AVL TreesNext
- Lecture
step: Find rebalancing case -->
Find rebalancing case Balance not ok

-2 57

+2 26 +1 72

0 3 -2 38 0 63 -1 94

0 1 0 25 -2 37 0 47 0 78

+1 30
Left Right Case
0 32

AVL Trees - Next


Lecture
step: Rebalance (Step 1) -->
Rebalance (Step 1) Balance not ok

-2 57

+2 26 +1 72

0 3 -2 38 0 63 -1 94

0 1 0 25 -2 37 0 78
0 47

-1 32

0 30

AVL Trees - Next


Lecture
step: Rebalance (Step 2) -->
Rebalance (Step 2) and recalculate balance factors Balance ok

-1 57

+1 26 +1 72

0 3 -1 38 0 63 -1 94

0 1 0 25 0 32 0 47 0 78

0 30 0 37

AVL Trees - Lecture Next step: Insert 35 -->


Insert 35 and recalculate balance factors Balance not ok

-2 57

+2 26 +1 72

0 3 -2 38 0 63 -1 94

0 1 0 25 +1 32 0 47 0 78

0 30 -1 37

0 35

AVL TreesNext
- Lecture
step: Find rebalancing case -->
Insert 35 Balance not ok

-2 57

+2 26 Start from first spot (from +1 72


bottom of tree) where balance
factor is incorrect.

0 3 -2 38 0 63 -1 94

0 1 0 25 +1 32 0 47 0 78

0 30 -1 37
Left Right Case

0 35

AVL Trees - Next


Lecture
step: Rebalance (Step 1) -->
Rebalance (Step 1) Balance not ok

-2
57

+1
+2 72
26

0 -2 0 -1 94
3 38 63

0 0
1 25 -2 0 0
37 47 78

0
32

0 0
30 35

AVL Trees - Next


Lecture
step: Rebalance (Step 2) -->
Rebalance (Step 2) Balance ok

-1
57

+1
26 +1
72

0
3 0 0 -1
37 63 94

0 1 0
25 0 +1 0
32 38 78

0 0 0
30 35 47

AVL Trees - Lecture Next step: Finished! -->


Finished! Balance ok

-1
57

+1
26 +1
72

-1 94
0
3 0
37
0
63
0 1
0
25 0
32
+1
38
0
78

0 0 0
30 35 47

Exercise: insert 36
AVL Trees - Lecture
Example of Insertions in an
AVL Tree
2
20 Insert 5, 40
0 1
10 30
0 0
25 35
Example of Insertions in an
AVL Tree
2
3
20 20
1 1 1 2
10 30 10 30
0 0 0 1
0 0
5 25 35 5 25 35
0
40
Now Insert 45
Single rotation (outside case)
3
3
20 20
1 2 1 2
10 30 10 30
0 0 2
0 0
5 25 35 5 25 40 1
0 0
35 45
Imbalance 1 40

0 45 Now Insert 34
Double rotation (inside case)
3
3
20 20
1 3 1 2
10 30 10 35
0 0 2
0 1
5 Imbalance 25 40 5 30 40 1
0
1 0 25 34 45
35 45 0
Insertion of 34 0
34
AVL Tree Deletion
• Similar but more complex than insertion
› Rotations and double rotations needed to
rebalance
› Imbalance may propagate upward so that
many rotations may be needed.
Pros and Cons of AVL Trees
Arguments for AVL trees:
1. Search is O(log N) since AVL trees are always balanced.
2. Insertion and deletions are also O(logn)
3. The height balancing adds no more than a constant factor to the
speed of insertion.

Arguments against using AVL trees:


1. Difficult to program & debug; more space for balance factor.
2. Asymptotically faster but rebalancing costs time.
3. Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
4. May be OK to have O(N) for a single operation if total run time for
many consecutive operations is fast (e.g. Splay trees).

Você também pode gostar