Вы находитесь на странице: 1из 23

 An AVL tree is another balanced binary search

tree.
 Named after their inventors, Adelson - Velskii
and Landis.
 They were the first dynamically balanced trees
to be proposed.
 Like red-black trees, they are not perfectly
balanced, but pairs of sub-trees differ in height
by at most 1
An AVL tree is a binary search tree which has the
following properties :

1. The sub-trees of every node differ in height by at


most one.
2. Every sub-tree is an AVL tree.
 Maintaining an O(logn) search time.
 Addition and deletion operations also
take O(logn) time.
 Balance Factor(N) := Height(lift Subtree )- Height(right Subtree)

 Balance Factor(N) ∈ {–1,0,+1}

Fig. 1: AVL tree with balance factors


An AVL tree may perform the following four
kinds of rotations −
 Left rotation
 Right rotation
 Left-Right rotation
 Right-Left rotation
The first two rotations are single rotations and the
next two rotations are double rotations.
 If a tree becomes unbalanced, when a node is
inserted into the right subtree of the right
subtree, then we perform a single left
rotation −
 AVL tree may become unbalanced, if a node
is inserted in the left subtree of the left
subtree. The tree then needs a right rotation.
0
57

0 +1
26 72

-1
0 0 -1
25
38 63 94

0
0
3 0 0
37
47 78
Balance not ok
Find rebalancing case
-1 57

-1 26 +1 72

Left Left Case


- 25 0 38 0 63 -1 94
2

-1 3 0 37 0 47 0 78

0 1

Next step: apply Left Left rebalancing -->


Balance ok
Rebalance and recalculate balance factors
0 57

0 26 +1 72

0 3 0 38 0 63 -1 94

0 1 0 25 0 37 0 47 0 78

Next step: insert 30 -->


Balance ok
Insert 30 and recalculate balance factors

-1 57

+1 26 +1 72

0 3 -1 38 0 63 -1 94

0 1 0 25 -1 37 0 47 0 78

0 30

Next step: Insert 32 -->


Balance not ok
Insert 32 and recalculate balance factors
-2 57

+2 26 +1 72

0 3 -2 38 0 63 -1 94

0 1 0 25 -2 37 0 47 0 78

+ 30
1
0 32

Next step: Find rebalancing case -->


Balance not ok
Find rebalancing case
-2 57

+2 26 +1 72

0 3 -2 38 0 63 -1 94

0 1 0 25 -2 37 0 47 0 78

+ 30
Left Right Case 1
0 32

Next step: Rebalance (Step 1) -->


Balance not ok
Rebalance (Step 1)
-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

Next step: Rebalance (Step 2) -->


Balance ok
Rebalance (Step 2) and recalculate balance factors
-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

Next step: Insert 35 -->


Balance not ok
Insert 35
-2 57

+2 26 Start from first spot +1 72


(from bottom of tree)
where balance factor is
incorrect.

0 3 -2 38 0 63 -1 94

0 1 0 25 + 32 0 47 0 78
1

0 30 -1 37
Left Right Case

0 35

Next step: Rebalance (Step 1) -->


Balance not ok
Rebalance (Step 1)
-2 57

+2 26 +1 72

0 3 -2 38 0 63 -1 94

0 1 0 25 -2 37 0 47 0 78

0 32

0 30 0 35

Next step: Rebalance (Step 2) -->


Rebalance (Step 2) Balance ok

-1 57

+1 26 +1 72

0 3 0 37 0 63 -1 94

0 1 0 25 0 32 +1 38 0 78

0 30 0 35 0 47

Next step: Finished! -->


Balance ok
Finished!
-1 57

+1 26 +1 72

0 3 0 37 0 63 -1 94

0 1 0 25 0 32 +1 38 0 78

0 30 0 35 0 47

Exercise: insert 36

Вам также может понравиться