1 avl-trees: motivation recall our discussion on bsts –the height of a bst depends on the order of...
Post on 28-Dec-2015
215 Views
Preview:
TRANSCRIPT
1
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?
2
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
3
AVL Trees: Formal Definition1. All empty trees are AVL-trees2. 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 iff1. TL and TR are AVL trees2. |hL – hR| <= 1, where hL and hR are the heights
of TL and TR respectively
hLhL+1 or hL-1
TL TR
4
AVL Trees• AVL trees are height-balanced
binary search trees
• 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
• For every node, heights of left and right subtree differ by no more than 1
6
4
1
7
9 0
-1
0
0
1
Red numbersare Balance Factors
An AVL Tree
5
AVL Trees: Examples and Non-Examples
6
5
4
7
9 0
-1
0
0
1
Red numbers are Balance Factors
An AVL Tree
6
4
1
9 0
1
0
0
An AVL Tree
5
6
4
1
9 1
0
0
0
An AVL Tree
5 8 0
6
4
1
9 2
-1
0
1
Non-AVL Tree
7 -1
8 0
6
4
3
7 -2
0
1
2
Non-AVL Tree
8
9 010
-1
6
4
1
10 2
-1
0
1
Non-AVL Tree
8 0
9 070
0 0
6
AVL Trees: Implementation• To implement AVL-trees, associate a
height with each node “x”
left right
key
height
x
• 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}
C++ Declaration
struct AVLTreeNode{
int key;
int height;
AVLTreeNode left;
AVLTreeNode right;
};
7
Good News about AVL Trees• Can prove: Height of an AVL
tree of N nodes is always O(log N)
• How? Can show:– 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
– Use Fibonacci numbers to get bound on S(h) bound on height h
6
5
4
7
9 0
-1
0
0
1
Red numbersare Balance Factors
An AVL Tree
Height = O(logN)
8
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
5
4
7
9 0
-1
0
0
1
6
5
4
7
9 0
-1
1
1
2
3
Insert 3
0An AVL Tree
No longer an AVL Tree
9
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 node1. Back up towards root updating balance factors along the
access path2. If Balance Factor of a node = 2 or –2, adjust the tree by
rotation around deepest such node.6
5
4
7
9 0
-1
1
1
2
30
Non- AVL Tree
10
Restoring Balance: Example
6
5
4
7
9 0
-1
0
0
1
6
5
4
7
9 0
-1
1
1
2
3
Insert 3
0AVLNot AVL
Rotate6
4
3
7
9
-1
0
0
0
AVL
5 0 0
• After Inserting the new node1. Back up towards root updating heights along the access
path2. If Balance Factor of a node = 2 or –2, adjust the tree by
rotation around deepest such node.
11
AVL Tree Insertion (1)
P
L R
A 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
P is the Pivot: bf is 2 or -2
12
AVL Tree Insertion (2)
P
L R
A B C D
• There are 4 cases:– Outside Cases (require single 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.)
− Inside Cases (require double rotation) :3. Insertion into left subtree of right child of P (RL
Imbalance)4. Insertion into right subtree of left child of P (LR
Imbalance)
P is the Pivot: bf is 2 or -2
13
LL Imbalance & Correction
P
L R
A B C D
• 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 around P
2
0 or 1
Tree after insertion
L
R
C D
A
P
B
Tree after LL correction
Rotate
14
LL Imbalance Correction Example (1)
10
4 20
3
1
1
0
0
Initial AVLTree
Insert(2)
• LL Imbalance:– bf of P(4) is 2– bf of L(3) is 0 or
1
10
4 20
3
1
0
0
Tree right after insertion of 2
20
1
Now, backup the tree updating
balance factors
Backup
Classify the type of imbalance
Identified 4 as the pivot
10
4 20
3
1
2
1
0
20
0
Rotate10
4
20
2
1
0
0
03
0
AVL Tree afterLL correction
15
LL Imbalance Correction Example (2)
10
4 20
3
1
0
0
Initial AVLTree
Insert(2) Rotate
5
0
0
0
0
10
4 20
3
1
0
0
Tree right after insertion of 2
2
5 0
0
10
4
20
31
02 50 0
0
AVL Tree afterLL Correction
• LL Imbalance:– bf of P(10) is 2– bf of L(4) is 0 or 1
0
1
10
4 20
3
2
1
0
Identified 10 asthe pivot
2
5 0
Backup
Classify the type of imbalanceNow, backup the
tree updating balance factors
16
RR Imbalance & Correction
P
L R
A B C D
• 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 around P
-2
0 or -1
Tree after insertion
R
LC
D
A
P
B
Tree after RR correction
Rotate
RR Imbalance Correction Example (1)
10
4 20
30
-1
0
Initial AVLTree
0
-1
Insert(40) Rotate10
4 20
30
-1
0
-1
-2
40 0
Identified 20as the pivot
10
4
20 40
-1
0
0
30
0
0
AVL Tree afterRR Correction
• RR Imbalance:– bf of P(20) is -2– bf of R(30) is 0 or -
1
10
4 20
30
-1
0
0
-1
40 0
Tree right after insertion of 40
Backup
Classify the type of imbalance
Now, backup the tree updating
balance factors
RR Imbalance Correction Example (2)
10
4 20
30
-1
0
Initial AVLTree
0
0
Insert(40) &Backup
Rotate
• RR Imbalance:– bf of P(10) is -2– bf of R(20) is 0 or -
1
150
10
4 20
30
-10
-1
-2
40 0
Tree after insertion of 40
150
10
4 400 0
30
0
-1
AVL Tree afterRR Correction
20
15
0
0
As we backed up the tree, we
updated balance factors and
identified 10 as the pivot
Classify the type of imbalance
LR Imbalance & Correction
P
L R
B1
2
-1
Tree after insertion
1
2
B2
LR
Rotate(1)
A C D
Rotate(2)P
L
R
AB1
C D
2
B2
LR
Tree after rotate(1)
PL
R
B1 B2
LR
A
C D
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
– Correction: Double rotation around L & P
20
LR Imbalance Correction Example
10
4 20
7
1
0
0
0
Initial AVLTree
Insert(5) &Backup
Rotate
• LR Imbalance:– bf of P(10) is
2– bf of L(4) is -1
0 3
AVL Tree afterLR Correction
7
4
3 05
10
020
0
0
0
-1
Tree after insertion of 5
0
10
4 20-1
2
0
5
73 1
0
1
2
As we backed up the tree, we
updated balance factors and
identified 10 as the pivot
Classify the type of imbalance
RL Imbalance & Correction
P
L R
C1
-2
2
C2
RL
A DB
1
Tree after insertion
P
L
R
C1
-2
2
C2
RL
A
D
B
Tree after Rotate(1)
Rotate(1)
P
L
R
C1 C2
RL
A
D
B
Tree after RL correction
Rotate(2)
1
• 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
22
RL Imbalance Correction Example
10
4 20
15
-1
0 0
Initial AVLTree
Insert(17)
• RL Imbalance:– bf of P(10) is -2– bf of R(20) is 1
300 0
Rotate 15
20 1
30 017
10
4 0
1
AVL Tree afterRL Correction
0
010
4 20
15
-2
0 1
30-1 0
17 0
Tree after insertion of 17
2
1
As we backed up the tree, we
updated balance factors and
identified 10 as the pivot
Classify the type of imbalance
23
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
24
Deletion Example (1)
10
4 20
3
1
0
0
Initial AVLTree
Delete(20) Backup
5
0
0
• LL Imbalance:– bf of P(10) is 2– bf of L(4) is 0 or 1
Tree after deletion of 20
10
4
3
1
0 5
0
0
Idenfitied 10 as the pivot
10
4
3
2
0 5
0
0
Classify the typeof imbalance
Now, backup the tree updating
balance factors
Rotate4
10 1
5
3
0
0
-1
AVL Tree afterLL Correction
25
Deletion Example (2)
10
4 20
1
0
Initial AVLTree
Delete(20) Backup
5
-1
0
• LR Imbalance:– bf of P(10) is
2– bf of L(4) is -1
Tree after deletion of 20
10
4
1
5
-1
0
Idenfitied 10 as the pivot
10
4
2
5
-1
0
Classify the typeof imbalance
Now, backup the tree updating
balance factors
Rotate5
10 040
0
AVL Tree afterLL Correction
1
2
26
Deletion Example (3)
10
5 20
1
0
Initial AVLTree
Delete(10)
7
0
3 15 30
402 4
1
8
1
1
1 0 0
-1
-1
0 -1
15
5 20
1
0
Tree after deletion of 10
We have copied the successor of 10 to root
and deleted 15
7
0
3 30
402 4
1
8
1
1
1 0 0
-1
-1
-1
Now, backup the tree updating
balance factors
27
Deletion Example (3) - continued
Classify the type of imbalance
15
5 20
1
0
7
0
3 30
402 4
1
8
1
1
1 0 0
-1
-2
-1
Identified 20 as the pivot
• RR Imbalance:– bf of P(20) is -2– bf of R(30) is 0 or -
1
Rotate 15
5
20
1
7
0
3
30
40
2 4
1
8
1
1
1 0 0
-1
Tree after RR imbalanceis corrected
0 0
0
Is this an AVL tree?
Continue backing up the tree
updating balance factors
Backup
28
Deletion Example (3) - continued
Classify the type of imbalance
• LL Imbalance:– bf of P(15) is 2– bf of L(5) is 0 or 1
Rotate15
5
20
2
7
0
3
30
40
2 4
1
8
1
1
1 0 0
-1 0 0
0
Backup
Identified 15 as the pivot
5
7
0
3
2 4
1 8
0
1
1 0
0
15
20
30
400 0
0
0
-1
Final Tree
29
Search (Find)• Since AVL Tree is a BST, search algorithm
is the same as BST search and runs in guaranteed O(logn) time
30
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
top related