Transcript AVL Trees

AVL Trees
• AVL (Adel`son-Vel`skii and Landis) tree =
– A BST
– With the property: For every node, the heights
of the left and right subtrees differ at most by
one
– Each node contains a value (-1, 1, 0) indicating
which subtree is "heavier”
Balance Factor
– Each node is marked with a balance factor
indicating which subtree is "heavier”
– Balance Factor: height (right subtree) minus height (left subtree)
– A balanced tree 1
(-1, 1, 0)
-1
0
1
0
1
0
AVL Implementation issues:
– Insert and Delete are modified. They restructure
the tree to make it balanced (if necessary)
Fixing Imbalances
 An imbalance is detected when the height difference between two
subtrees of a node becomes greater than 1 or smaller than -1
 There are two cases of imbalances:
-2
1
-1
0
-2
2
or
2
-1
1
0
CASE 1
Imbalance caused by inserting node
in left subtree of left child or right
subtree of right child (i.e., insertion
occurs on the “outside”)
0
or
0
CASE 2
Imbalance caused by inserting node
in right subtree of left child or left
subtree of right child (i.e., insertion
occurs on the “inside”)
Fixing Imbalances (cont’d)
 Fixing an imbalance is done by rotating the tree
 There are two types of rotation:
 single rotation
 for CASE 1 imbalances
 double rotation
 for CASE 2 imbalances
 consists of two single rotations
 The rotations must always preserve the BST property
Fixing Imbalances: Case 1
node with imbalance
6
D
4
4
C
2
A
B
right rotate
node 4 about 6
2
A
6
B C
D
This is a single right rotation. A single left rotation is symmetric.
Fixing Imbalances: Case 1 (cont’d)
left rotate node Q about P
This is a single left rotation
Fixing Imbalances: Case 2
node with imbalance
node with imbalance
6
6
D
4
2
A
2
4
B
C
STEP 1: left rotate
node 4 about 2
4
D
A
B
C STEP2:
right rotate
A
node 4 about 6
2
6
B C
D
Fixing Imbalances: Case 2 (cont’d)
STEP 1: Right rotate R about Q
STEP 2: Left rotate R about P
Note that P can be part of a larger AVL tree; it
can be a child of some other node in the tree
AVL Trees: Insert
1. Insert the node as in a BST
2. Starting at the newly inserted node, travel up
the tree (towards the root), updating the
balances along the way
The first node for which the balance factor
becomes +/- 2 (if any) is the root P of a subtree
that needs to be rebalanced
EXAMPLE: Insert the items
4, 2, 1, 5, 6, 7, 8, 18, 17, 16, 13, 12
into an initially empty AVL tree
AVL Trees: Insert, rebalance locally
– If a node is entered into the larger AVL tree and
P becomes imbalanced, after restoring the
balance of P, does extra work need to be done
to the predecessor(s) of P?
• Fortunately not. The new height of P (after the
rotation) is exactly the same as the original height of
P prior to the insertion that caused P’s imbalance.
Thus no further updating of heights on the path to
the root is needed, and consequently no further
rotations are needed
• Does it mean that the tree can no grow in the height
at all?
AVL Trees: Delete
1. Delete the node as in a BST
2. Starting at the parent of the deleted node,
travel up the tree (towards the root),
updating the balances along the way
a. If the tree becomes unbalanced, decide which
rotation needs to be performed, rotate the tree
and update the balances
b. Keep traveling towards the root, checking the
balances. You may need to rotate again
AVL Trees: Efficiency
•
It can be shown that the worst case height of
an AVL tree is at most 44% larger than the
minimum possible for a BST (i.e.
approximately 1.44lgn)
Time Complexity of Basic AVL Tree
Operations
•
Insert
– Maximum possible number of rotations = 1
•
Delete
– Maximum possible number of rotations = lg(n)
•
Worst case times
– Search: O(lgn)
– Insert: O(lgn)
– Delete: O(lgn)
Other Methods
• AVL trees maintain balance of BSTs while they
are being created via insertions of data
• An alternative approach is to have trees that
readjust themselves when data is accessed,
making often accessed data items move to the
top of the tree (splay trees)