AVL Tree - Wikipedia PDF
AVL Tree - Wikipedia PDF
Invented 1962
Space
Search
[1] [1]
Insert
[1] [1]
Delete
[1] [1]
Animation showing the insertion of several elements into
an AVL tree. It includes left, right, left-right and right-left
rotations.
Definition
Balance factor …
[6]
[7]
A node with is
called "left-heavy", one with
is called "right-
heavy", and one with
is sometimes
simply called "balanced".
Remark
Properties …
Operations
Read-only operations of an AVL tree involve
carrying out the same actions as would be
carried out on an unbalanced binary search
tree, but modifications have to observe and
restore the height balance of the sub-trees.
Searching …
Traversal …
Insert …
This section has multiple issues. Please help
improve it or discuss these issues on the talk page.
1 for (X = parent(Z); X !=
null; X = parent(Z)) { //
Loop (possibly up to the
root)
2 // BalanceFactor(X)
has to be updated:
3 if (Z ==
right_child(X)) { // The
right subtree increases
4 if
(BalanceFactor(X) > 0) { //
X is right-heavy
5 // ===> the
temporary BalanceFactor(X)
== +2
6 // ===>
rebalancing is required.
7 G =
parent(X); // Save parent of
X around rotations
8 if
(BalanceFactor(Z) < 0)
// Right Left Case (see
figure 5)
9 N =
rotate_RightLeft(X, Z); //
Double rotation: Right(Z)
then Left(X)
10 else
// Right Right Case (see
figure 4)
11 N =
rotate_Left(X, Z); //
Single rotation Left(X)
12 // After
rotation adapt parent link
13 } else {
14 if
(BalanceFactor(X) < 0) {
15
BalanceFactor(X) = 0; // Z’s
height increase is absorbed
at X.
16 break; //
Leave the loop
17 }
18
BalanceFactor(X) = +1;
19 Z = X; //
Height(Z) increases by 1
20 continue;
21 }
22 } else { // Z ==
left_child(X): the left
subtree increases
23 if
(BalanceFactor(X) < 0) { //
X is left-heavy
24 // ===> the
temporary BalanceFactor(X)
== –2
25 // ===>
rebalancing is required.
26 G =
parent(X); // Save parent of
X around rotations
27 if
(BalanceFactor(Z) > 0)
// Left Right Case
28 N =
rotate_LeftRight(X, Z); //
Double rotation: Left(Z)
then Right(X)
29 else
// Left Left Case
30 N =
rotate_Right(X, Z); //
Single rotation Right(X)
31 // After
rotation adapt parent link
32 } else {
33 if
(BalanceFactor(X) > 0) {
34
BalanceFactor(X) = 0; // Z’s
height increase is absorbed
at X.
35 break; //
Leave the loop
36 }
37
BalanceFactor(X) = –1;
38 Z = X; //
Height(Z) increases by 1
39 continue;
40 }
41 }
42 // After a rotation
adapt parent link:
43 // N is the new root
of the rotated subtree
44 // Height does not
change: Height(N) == old
Height(X)
45 parent(N) = G;
46 if (G != null) {
47 if (X ==
left_child(G))
48 left_child(G)
= N;
49 else
50
right_child(G) = N;
51 } else
52 tree->root = N;
// N is the new root of the
total tree
53 break;
54 // There is no fall
thru, only break; or
continue;
55 }
56 // Unless loop is left
via break, the height of the
total tree increases by 1.
Delete …
1 for (X = parent(N); X !=
null; X = G) { // Loop
(possibly up to the root)
2 G = parent(X); //
Save parent of X around
rotations
3 // BalanceFactor(X)
has not yet been updated!
4 if (N ==
left_child(X)) { // the left
subtree decreases
5 if
(BalanceFactor(X) > 0) { //
X is right-heavy
6 // ===> the
temporary BalanceFactor(X)
== +2
7 // ===>
rebalancing is required.
8 Z =
right_child(X); // Sibling
of N (higher by 2)
9 b =
BalanceFactor(Z);
10 if (b < 0)
// Right Left Case (see
figure 5)
11 N =
rotate_RightLeft(X, Z); //
Double rotation: Right(Z)
then Left(X)
12 else
// Right Right Case (see
figure 4)
13 N =
rotate_Left(X, Z); //
Single rotation Left(X)
14 // After
rotation adapt parent link
15 } else {
16 if
(BalanceFactor(X) == 0) {
17
BalanceFactor(X) = +1; //
N’s height decrease is
absorbed at X.
18 break; //
Leave the loop
19 }
20 N = X;
21
BalanceFactor(N) = 0; //
Height(N) decreases by 1
22 continue;
23 }
24 } else { // (N ==
right_child(X)): The right
subtree decreases
25 if
(BalanceFactor(X) < 0) { //
X is left-heavy
26 // ===> the
temporary BalanceFactor(X)
== –2
27 // ===>
rebalancing is required.
28 Z =
left_child(X); // Sibling of
N (higher by 2)
29 b =
BalanceFactor(Z);
30 if (b > 0)
// Left Right Case
31 N =
rotate_LeftRight(X, Z); //
Double rotation: Left(Z)
then Right(X)
32 else
// Left Left Case
33 N =
rotate_Right(X, Z); //
Single rotation Right(X)
34 // After
rotation adapt parent link
35 } else {
36 if
(BalanceFactor(X) == 0) {
37
BalanceFactor(X) = –1; //
N’s height decrease is
absorbed at X.
38 break; //
Leave the loop
39 }
40 N = X;
41
BalanceFactor(N) = 0; //
Height(N) decreases by 1
42 continue;
43 }
44 }
45 // After a rotation
adapt parent link:
46 // N is the new root
of the rotated subtree
47 parent(N) = G;
48 if (G != null) {
49 if (X ==
left_child(G))
50 left_child(G)
= N;
51 else
52
right_child(G) = N;
53 if (b == 0)
54 break; //
Height does not change:
Leave the loop
55 } else {
56 tree->root = N;
// N is the new root of the
total tree
57 }
58 // Height(N)
decreases by 1 (== old
Height(X)-1)
59 }
60 // Unless loop is left
via break, the height of the
total tree decreases by 1.
function split(T,k)
if (T=nil) return
(nil,false,nil)
(L,(m,c),R)=expose(T)
if (k=m) return
(L,true,R)
if (k<m)
(L',b,R')=split(L,k)
return
(L',b,join(R',m,R))
if (k>m)
(L',b,R')=split(R,k)
return
(join(L,m,L'),b,R))
The union of two AVLs t1 and t2 representing
sets A and B, is an AVL t that represents
A ∪ B.
Pseudocode implementation for the union
algorithm
Rebalancing
If during a modifying operation (e.g. insert,
delete) a (temporary) height difference of
more than one arises between two child
subtrees, the parent subtree has to be
"rebalanced". The given repair tools are the so-
called tree rotations, because they move the
keys only "vertically", so that the ("horizontal")
in-order sequence of the keys is fully
preserved (which is essential for a binary-
search tree).[12][13]
i.e.
child of its
=> Z parent X (i.e. (see
Right
is a and Z is BalanceFactor(Z) figure
Right
right not left- ≥ 0) 4)
heavy
child of its
=> Z parent X (i.e.
Left
is a and Z is BalanceFactor(Z)
Left
left not right- ≤ 0)
heavy
child of its
=> Z (i.e. (see
Right parent X
is a BalanceFactor(Z) figure
Left and Z is
right = −1) 5)
left-heavy
Left => Z child of its (i.e.
Right is a parent X BalanceFactor(Z)
left and Z is = +1)
right-
heavy
Simple rotation …
Figure 4 shows a Right Right situation. In its
upper half, node X has two child trees with a
balance factor of +2. Moreover, the inner child
t23 of Z (i.e., left child when Z is right child
resp. right child when Z is left child) is not
higher than its sibling t4. This can happen by a
height increase of subtree t4 or by a height
decrease of subtree t1. In the latter case, also
the pale situation where t23 has the same
height as t4 may occur.
Double rotation …
1 node *rotate_RightLeft(node
*X, node *Z) {
2 // Z is by 2 higher
than its sibling
3 Y = left_child(Z); //
Inner child of Z
4 // Y is by 1 higher
than sibling
5 t3 = right_child(Y);
6 left_child(Z) = t3;
7 if (t3 != null)
8 parent(t3) = Z;
9 right_child(Y) = Z;
10 parent(Z) = Y;
11 t2 = left_child(Y);
12 right_child(X) = t2;
13 if (t2 != null)
14 parent(t2) = X;
15 left_child(Y) = X;
16 parent(X) = Y;
17 if (BalanceFactor(Y) >
0) { // t3 was higher
18 BalanceFactor(X) =
–1; // t1 now higher
19 BalanceFactor(Z) =
0;
20 } else
21 if
(BalanceFactor(Y) == 0) {
22
BalanceFactor(X) = 0;
23
BalanceFactor(Z) = 0;
24 } else {
25 // t2 was
higher
26
BalanceFactor(X) = 0;
27
BalanceFactor(Z) = +1; // t4
now higher
28 }
29 BalanceFactor(Y) = 0;
30 return Y; // return new
root of rotated subtree
31 }
ratio,
and
.
an RB tree's height is at most
.[19]
See also
Trees
Tree rotation
WAVL tree
Red–black tree
Splay tree
Scapegoat tree
B-tree
T-tree
List of data structures
References
1. Eric Alexander. "AVL Trees" .
2. Sedgewick, Robert (1983). "Balanced
Trees". Algorithms . Addison-Wesley.
p. 199 . ISBN 0-201-06672-6.
3. Adelson-Velsky, Georgy; Landis, Evgenii
(1962). "An algorithm for the organization
of information". Proceedings of the USSR
Academy of Sciences (in Russian). 146:
263–266. English translation by Myron J.
Ricci in Soviet Mathematics - Doklady,
3:1259–1263, 1962.
4. Pfaff, Ben (June 2004). "Performance
Analysis of BSTs in System Software"
(PDF). Stanford University.
5. AVL trees are not weight-balanced?
(meaning: AVL trees are not μ-balanced?)
Thereby: A Binary Tree is called -
balanced, with , if for every
node , the inequality
Further reading
Donald Knuth. The Art of Computer
Programming, Volume 3: Sorting and
Searching, Third Edition. Addison-Wesley,
1997. ISBN 0-201-89685-0. Pages 458–475
of section 6.2.3: Balanced Trees.
External links