Escolar Documentos
Profissional Documentos
Cultura Documentos
Spring 2007
CSE, POSTECH
AVL Tree
Definition
Binary tree.
If T is a nonempty binary tree with TL and TR as
its left and right subtrees, then T is an AVL tree iff
1.
2.
4.
5.
Balance Factor
0 3
0
0 5
1 40
0 8
1 30
-1 20
0 35
0
25
45 -1
0
60
O(log n)
-1 10
1 7
0 3
0
0 5
1 40
0 8
0 9
1 30
-1 20
0 35
0
25
45 -1
0
60
Imbalance Types
1.
2.
3.
4.
Rotation
Definition
To switch children and parents among two or three
adjacent nodes to restore balance of a tree.
A rotation may change the depth of some nodes,
but does not change their relative ordering.
Left Rotation
Definition
In a binary search tree, pushing a node A down and to the
left to balance the tree.
A's right child replaces A, and the right child's left child
becomes A's right child.
9 A
4
15
Left Rotation
15
12
22
9
4
22
12
Right Rotation
Definition
In a binary search tree, pushing a node A down and to the
right to balance the tree.
A's left child replaces A, and the left child's right child
becomes A's left child.
A 15
9
4
22
12
Right Rotation
15
12
22
AVL Rotations
An LL Rotation
An LR Rotation
Insert(29)
1 7
0 3
0
0 5
1 40
0 8
1 30
-1 20
45 -1
0 35
0
25
29
0
60
1 40
0 8
1 30
0 5
0 35
1
20
-2
What are the new balance factors for 20,
25, 29?
25 -1
What type of imbalance do we have?
RR imbalance new node is in the right
29 0
subtree of right subtree of node 20 (node
with bf = -2) what rotation do we need?
What would the left subtree of 30 look like
after RR rotation?
45 -1
0
60
After RR Rotation
-1 10
1 7
0 3
0 1
1 40
0 8
1 30
0 5
0 25
0
20
45 -1
0 35
0
60
0
29
Do Exercise 15.1
- see the solution on the Web http://www.cise.ufl.edu/~sahni/dsaac/
An R0 Rotation
An R1 Rotation
An R-1 Rotation
Do Exercise 15.5
- see the solution on the Web
http://www.cise.ufl.edu/~sahni/dsaac/