Document

advertisement
AVL-Trees: Motivation
•
Recall our discussion on BSTs
–
The height of a BST depends on the order of
insertion
•
•
–
•
E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into an empty BST
Problem: Lack of “balance” – Tree becomes highly
asymmetric and degenerates to a linked-list!!
Since all operations take O(h) time, where log N <= h
<= N-1, worst case running time of BST operations
are O(N)
Question: Can we make sure that regardless of
the order of key insertion, the height of the
BST is log(n)? In other words, can we keep the
BST balanced?
1
Height-Balanced Trees
•
Many efficient algorithms exist for balancing
BSTs in order to achieve faster running times
for the BST operations
–
–
–
–
Adelson-Velskii and Landis (AVL) trees (1962)
Splay trees and other self-adjusting trees (1978)
B-trees and other multiway search trees (1972)
Red-Black trees (1972)
•
•
Also known as Symmetric Binary B-Trees
Will not be covered in this course
2
AVL Trees: Formal Definition
1. All empty trees are AVL-trees
2. If T is a non-empty binary search tree with
TL and TR as its left and right sub-trees, then
T is an AVL tree iff
1. TL and TR are AVL trees
2. |hL – hR| <= 1, where hL and hR are the heights of TL
and TR respectively
hL
TL
TR
hL+1 or hL-1
3
AVL Trees
•
•
•
•
AVL trees are height-balanced
binary search trees
An AVL Tree
Balance factor of a node =
height(left subtree) - height(right
subtree)
An AVL tree can only have balance
factors of –1, 0, or 1 at every node
6
1
0
1
4
0
7
-1
0
9
Red numbers
are Balance Factors
For every node, heights of left and
right subtree differ by no more
than 1
4
AVL Trees: Examples and Non-Examples
6 0
1
0
5
7 -1
4
9 0
0
0
An AVL Tree
1
0
1
4
0
9 0
4
0
5 0
9 1
4
1 0 5
6 0
6 -1
1
9 2
7 -1
8
Non-AVL Tree
0
0
4
2
10 2
1
8 0
1
0 7
8 0
An AVL Tree
An AVL Tree
6 -1
1
6 0
6 1
9 0
0
Non-AVL Tree
3
4
7 -2
8 -1
1
9 0
Non-AVL Tree
Red numbers are Balance Factors
5
AVL Trees: Implementation
•
To implement AVL-trees, associate a height
with each node “x”
C++ Declaration
x
left
key
height
•
•
right
struct AVLTreeNode{
int key;
int height;
AVLTreeNode left;
AVLTreeNode right;
};
Balance factor (bf) of x = height of left
subtree of x – height of right subtree of x
In an AVL-tree, “bf” can be one of {-1, 0, 1}
6
Good News about AVL Trees
•
•
Can prove: Height of an AVL
An AVL Tree
tree of N nodes is always
Height = O(logN)
0
O(log N)
6
How? Can show:
-1
1
–
–
Height h = 1.44 log(N)
Prove using recurrence relation
for minimum number of nodes
S(h) in an AVL tree of height h:
•
–
S(h) = S(h-1) + S(h-2) + 1
5
0
4
7
9
0
Red numbers
are Balance Factors
Use Fibonacci numbers to get
bound on S(h) bound on height h
7
•
Good and Bad News about AVL Trees
Good News:
–
•
Search takes O(h) = O(logN)
Bad News
•
Insert and Delete may cause the tree to be unbalanced!
6
1
0
5
Insert 3
0
7
4
An AVL Tree
6 1
-1
9
2
1
0
0
4
5
7
-1
9
0
3
No longer an AVL Tree
8
Restoring Balance in an AVL Tree
•
Problem: Insert may cause balance factor to become
2 or –2 for some node on the path from root to
insertion point
•
Idea: After Inserting the new node
1.
Back up towards root updating balance factors along the
access path
2. If Balance Factor of a node = 2 or –2, adjust the tree by
rotation around deepest such node.
6 1
2
1
0
4
5
7
-1
9
0
3
Non- AVL Tree
9
Restoring Balance: Example
6
1
0
5
4
Insert 3
0
7
-1
9
AVL
2
1
0
0
Rotate
6 1
5
7
-1
4
9
3
Not AVL
•
6 0
0
0
0
3
7
4
5 0
-1
9
AVL
After Inserting the new node
1. Back up towards root updating heights along the access path
2. If Balance Factor of a node = 2 or –2, adjust the tree by
rotation around deepest such node.
10
0
AVL Tree Insertion (1)
P
P is the Pivot: bf is 2 or -2
L
A
•
R
B
C
D
Let the node that needs rebalancing be P.
–
–
P is called the pivot node
P is the first node that has a bf of 2 or -2 as we
backup towards the root after an insertion
11
AVL Tree Insertion (2)
P
P is the Pivot: bf is 2 or -2
L
A
•
R
B
C
D
There are 4 cases:
–
Outside Cases (require single rotation) :
−
Inside Cases (require double rotation) :
1. Insertion into left subtree of left child of P (LL Imbalance).
2. Insertion into right subtree of right child of P (RR Imbalan.)
3. Insertion into left subtree of right child of P (RL Imbalance)
4. Insertion into right subtree of left child of P (LR Imbalance)
12
LL Imbalance & Correction
Rotate
2
L
P
0 or 1 L
P
R
A
R
A
B
C
Tree after insertion
D
B
C
D
Tree after LL correction
•
LL Imbalance: We have inserted into the left
subtree of the left child of P (into subtree A)
–
–
bf of P is 2
bf of L is 0 or 1
– Correction: Single rotation to the right around13 P
LL Imbalance Correction Example (1)
1
1
4
Insert(2)
10
1
1 4
20 0
0 3
Backup
10
20 0
0 3
Initial AVLTree
0 2
2
4
Rotate
10
0
20 0
1 3
0 2
1
10
20 0
0 3
0 2
Identified 4
as the pivot
Tree right after
insertion of 2
Now, backup the
tree updating
balance factors
1
4 0
AVL Tree after
LL correction
Classify the type
of imbalance
•
LL Imbalance:
–
–
bf of P(4) is 2
bf of L(3) is 0 or 1
14
LL Imbalance Correction Example (2)
1
10
0 4
0 3
Insert(2)
0 4
20 0
5 0
Initial
AVLTree
0 3
Backup
1 10
2 10
1 4
20 0
1 3
5 0
0 2
0 2
Tree right after
insertion of 2
Now, backup the
tree updating
balance factors
Rotate
20 0
5 0
0 4
1 3
5 0
0 2
Identified 10 as
the pivot
10 0
20 0
AVL Tree after
LL Correction
Classify the type
of imbalance
•
LL Imbalance:
–
–
bf of P(10) is 2
bf of L(4) is 0 or 1
15
RR Imbalance & Correction
Rotate
P
R
-2
P
L
R 0 or -1
D
L
A
B
C
Tree after insertion
C
D
A
B
Tree after RR correction
•
RR Imbalance: We have inserted into the right
subtree of the right child of P (into subtree D)
–
–
bf of P is -2
bf of R is 0 or -1
– Correction: Single rotation to the left around16P
RR Imbalance Correction Example (1)
10 -1
0 4
Insert(40)
20 -1
30 0
Initial
AVLTree
0 4
10 -1
20
Backup
0 4
-1
20
-2
40 0
40 0
Identified 20
as the pivot
Tree right after
insertion of 40
10 -1
30 0
0 4
30 -1
30 0
Now, backup the
tree updating
balance factors
Rotate
10 -1
0 20
40 0
AVL Tree after
RR Correction
Classify the type
of imbalance
•
RR Imbalance:
–
–
bf of P(20) is -2
bf of R(30) is 0 or -1
RR Imbalance Correction Example (2)
10 -1
0 4
0 15
10
Insert(40) &
Backup
20 0
0 4
30 0
Rotate
-2
20 -1
0 15
Initial AVLTree
0 10
30 -1
0 4
30 -1
15 0
40 0
40 0
Tree after
insertion of 40
As we backed up the
tree, we updated
balance factors and
identified 10 as the
pivot
20 0
AVL Tree after
RR Correction
Classify the type
of imbalance
•
RR Imbalance:
–
–
bf of P(10) is -2
bf of R(20) is 0 or -1
LR Imbalance & Correction
2
-1 L
1
Rotate(1)
P
R
2
LR
C
B1
D
B2
Tree after insertion
•
R
2
LR
P
L
L
LR
A
Rotate(2)
P
B2
A
C
R
D
A
B1 B2
B1
Tree after rotate(1)
C
Tree after LR correction
LR Imbalance: We have inserted into the right
subtree of the left child of P (into subtree LR)
–
–
bf of P is 2
bf of L is -1
D
– Correction: Double rotation around L & P
LR Imbalance Correction Example
1
0
0 3
Insert(5) &
Backup
10
4
2
20 0
7 0
Initial AVLTree
As we backed up the
tree, we updated
balance factors and
identified 10 as the
pivot
10
2
-1 4
0 3
Rotate
0
0 4
20 0
1
7 1
7
0 3
5 0
Tree after
insertion of 5
10 -1
5 0
20 0
AVL Tree after
LR Correction
Classify the type
of imbalance
•
LR Imbalance:
–
–
bf of P(10) is 2
bf of L(4) is -1
20
RL Imbalance & Correction
P
-2
2
L
R
RL
A
Rotate(1)
D
C2
A
B
RL
P
RL
C1
C1
C2
R
L
R
Tree after insertion
•
2
L
1
-2
1
B
C1
P
Rotate(2)
D
Tree after Rotate(1)
A
C2
D
B
Tree after RL correction
RL Imbalance: We have inserted into the left
subtree of the right child of P (into subtree RL)
–
–
bf of P is -2
bf of R is 1
– Correction: Double rotation around L & P
RL Imbalance Correction Example
Insert(17)
-1 10
0
4
20 0
0 15
0 4
2
-1 15
30 0
Initial AVLTree
As we backed up the
tree, we updated
balance factors and
identified 10 as the
pivot
Rotate
-2 10
20 1
1
30 0
15 0
1 10
0 4
17 0
Tree after
insertion of 17
20 1
0 17
30 0
AVL Tree after
RL Correction
Classify the type
of imbalance
•
RL Imbalance:
–
–
bf of P(10) is -2
bf of R(20) is 1
22
Deletion
•
Deletion is similar to insertion
•
First do regular BST deletion keeping track of
the nodes on the path to the deleted node
•
After the node is deleted, simply backup the
tree and update balance factors
−
−
If an imbalance is detected, do the appropriate
rotation to restore the AVL tree property
You may have to do more than one rotation as you
backup the tree
23
Deletion Example (1)
1
10
0 4
0 3
Delete(20)
20 0
5 0
Initial AVLTree
1
10
Backup
0 4
0 3
2 10
Rotate
0 4
5 0
Tree after
deletion of 20
Now, backup the
tree updating
balance factors
-1
10 1
0 3
5 0
0 3
4
Idenfitied 10 as
the pivot
0
5
AVL Tree after
LL Correction
Classify the type
of imbalance
•
LL Imbalance:
–
–
bf of P(10) is 2
bf of L(4) is 0 or 1
24
Deletion Example (2)
1
10
-1 4
Delete(20)
20 0
5 0
Initial AVLTree
1
10
Backup
-1 4
2 10
-1 4
1
5 0
Tree after
deletion of 20
Now, backup the
tree updating
balance factors
Rotate
2
5 0
0 4
5
0
10 0
AVL Tree after
LL Correction
Idenfitied 10 as
the pivot
Classify the type
of imbalance
•
LR Imbalance:
–
–
bf of P(10) is 2
bf of L(4) is -1
25
Deletion Example (3)
1
1 3
1 2
20
0 1
-1 7
0 4
Delete(10)
10
1 5
1
0 15
0 8
1 5
-1
1 3
30 -1
0 40
1 2
0 1
Initial AVLTree
15
20
-1 7
0 4
0 8
-1
30 -1
0 40
Tree after deletion of 10
We have copied the
successor of 10 to root
and deleted 15
Now, backup the
tree updating
balance factors
26
Deletion Example (3) - continued
Backup
1
1 5
1 3
1 2
0 1
20
-1 7
0 4
Rotate
15
-2
0 8
0 40
Identified 20 as the pivot
RR Imbalance:
–
–
bf of P(20) is -2
bf of R(30) is 0 or -1
15
1 5
1 3
30 -1
Classify the type
of imbalance
•
1
1 2
0 1
30 0
-1 7
0 4
20 0 40 0
0 8
Tree after RR imbalance
is corrected
Is this an AVL
tree?
Continue backing
up the tree
updating balance
factors
27
Deletion Example (3) - continued
Backup
1 5
1 3
1 2
0 1
Rotate
2 15
30 0
-1 7
0 4
0 5
20 0 40 0
0 8
Identified 15 as the pivot
1 3
1 2
0 1
15 0
0 4
7 -1
30 0
0 8
20 0 40 0
Final Tree
Classify the type
of imbalance
•
LL Imbalance:
–
–
bf of P(15) is 2
bf of L(5) is 0 or 1
28
Search (Find)
•
Since AVL Tree is a BST, search algorithm is
the same as BST search and runs in
guaranteed O(logn) time
29
Summary of AVL Trees
•
Arguments for using AVL trees:
1. Search/insertion/deletion is O(log N) since AVL
trees are always balanced.
2. The height balancing adds no more than a constant
factor to the speed of insertion/deletion.
•
Arguments against using AVL trees:
1. Requires extra space for balancing factor (height)
2. It may be OK to have a partially balanced tree that
would give performance similar to AVL trees
without requiring the balancing factor
• Splay trees
30
Download