Title: AVL Trees
1AVL Trees
2Problems with Binary Search Trees
- Adding the sorted sequence A, B, C, ..., G to a
binary search trees leads gives a tree that is no
better than a linear list...
In fact it is less efficient than a linked list
because of the additional overhead associated
with each node.
A
3AVL trees Basic Concepts
- Named after its two inventors, who published it
in 1962. - G.M. Adelson-Velskii
- E.M. Landis
- Self-balancing BST
- Balance factor the height of its right subtree
minus the height of its left subtree - A node with balance factor equal to -1, 0 or 1
considered balanced - Whenever a tree is not balanced then rotate
around the root - Rotate the root into the lighter subtree
4AVL trees some properties
- The height of an AVL tree with n nodes is O(log
n) - The height balance property
- An AVL tree with n nodes can be searched in O(log
n) time - Any subtree of an AVL tree is an AVL tree
- Searching, insertion, and deletion can be done in
O(log n) time
5AVL trees examples
- Examples AVL trees
- Examples non-AVL trees
6Rotation
- 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. - Types
- Left rotation
- Right rotation
- Left right rotation
- Right left rotation
7Right Rotation
- Definition
- In a binary search tree, pushing a node A down
and to the right to balance the tree. - Three steps (1) get the left child of node A
node B (2) Set the right child of node B as the
left child of node A and (3) Set node A as the
right child of node B.
8Left Rotation
- Definition
- In a binary search tree, pushing a node A down
and to the left to balance the tree. - Three steps (1) get the right child of node A
node B (2) Set the left child of node B as the
right child of node A and (3) Set node A as the
left child of node B.
9Left Right Rotation
- Definition
- Sometimes a single left rotation is not
sufficient to balance an unbalanced tree. - Defined to be a right rotation at the right child
of the node followed by a left rotation at the
node
10Right Left Rotation
- Definition
- Similarly, sometimes a single right rotation is
not sufficient to balance an unbalanced tree. - Defined to be a left rotation at the left child
of the node followed by a right rotation at the
node
11Rotations when and How
- To decide when you need a tree rotation is
usually easy, but determining which type of
rotation requires a little thought. - General guidelines
-
- IF a tree is right heavy (balance factorgt1)
- IF trees right subtree is left heavy (balance
factor lt-1) - Perform Left Right rotation
- ELSE
- Perform single Left rotation
- IF a tree is left heavy (balance factorlt-1)
- IF trees left subtree is right heavy (balance
factor gt1) - Perform Right Left rotation
- ELSE
- Perform single Right rotation
12Building an AVL tree added A
A
13Building an AVL tree added B
A
B
14Building an AVL tree adding C
Right hand side subtree has height of 2
Tree A unbalanced (Balance factor 2)
15Building an AVL tree adding C
Left rotation
B
C
A
16Building an AVL tree added D
B
C
A
D
17Building an AVL tree adding E
B
C
A
D
E
Note Both B and C have a balance factor equal to
2. We start with the deepest node that is
unbalanced.
18Building an AVL tree added E
It is a balanced tree
B
D
A
E
C
19Building an AVL tree adding F
Oh, tree B is unbalanced!
B
D
A
E
C
F
20Building an AVL tree adding F
B
D
A
E
C
F
21Building an AVL tree added F
- Note
- D becomes Bs parent (and B becomes Ds left
child) - B becomes parent of Ds left child
- C becomes Bs right child
22Building an AVL tree adding G
Oh, E becomes unbalanced!
D
E
B
F
A
C
G
23Building an AVL tree added G
D
F
B
G
A
C
E
24Building an AVL tree added T
D
F
B
G
A
C
E
T
Checking balance factors for every node
everything is OK!
25Building an AVL tree adding K
D
F
B
G
A
C
E
T
Oh, G becomes unbalanced and its right subtree is
left heavy!!!
K
26Building an AVL tree adding K
D
F
B
G
A
C
E
K
Step 1. Rotate this subtree right to make it
heavier on the right.
T
27Building an AVL tree adding K
D
F
B
G
A
C
E
K
Step 2. Rotate this subtree left to make it to
balance it.
T
28Building an AVL tree added K
D
F
B
K
A
C
E
T
G