Chapter 7b - AVL - 2009 - 2009
Chapter 7b - AVL - 2009 - 2009
Chapter 7b - AVL - 2009 - 2009
DEFINITION:
AVL Tree
The name comes from the discoverers of this method, G.M.Adel'son-Vel'skii and E.M.Landis.
Balance factor
Balance factor:
left_higher: HL = HR + 1
equal_height: right_higher:
HL = HR HR = HL + 1
In C++:
enum Balance_factor {left_higher, equal_height, right_higher};
AVL trees
non-AVL trees
At the stopping case of recursive, the empty subtree becomes a tree with one node for new data, taller is set to TRUE.
b) If two subtree had the same height, its parent's balance factor must be changed, the height of parent tree increases by 1. taller remains TRUE.
c) If the subtree was the higher subtree: only in this case, the definition of AVL is violated at the parent node, rebalancing must be done. taller becomes FALSE
HL
HL
//
10
Case b
taller = TRUE
11
Case b
taller = TRUE
12
Case b
taller = TRUE
13
Case b
taller = TRUE
14
taller = TRUE
15
16
17
Case b
taller = TRUE18
19
20
21
22
Case b
taller = TRUE
23
24
taller = TRUE
25
taller = TRUE
26
Case c
taller = TRUE
Single Rotation
27
Case c
taller = FALSE
taller = TRUE
Single Rotation
28
29
Case b
taller = TRUE
30
31
Case b
taller = TRUE
32
33
taller = TRUE
34
taller = TRUE
Double Rotation
35
Case c
taller = TRUE
taller = FALSE
Double Rotation
36
Recursive Insert
<ErrorCode> recursive_Insert (ref subroot <pointer>, val DataIn <DataType>, ref taller <boolean>)
Inserts a new node into an AVL tree. Pre subroot points to the root of a tree/ subtree. DataIn contains data to be inserted into the subtree.
Post If the key of DataIn already belongs to the subtree, duplicate_error is returned. Otherwise, DataIn is inserted into the subtree in such a way that the properties of an AVL tree are preserved. If the subtree is increased in height, the parameter taller is set to TRUE; otherwise it is set to FALSE. Return duplicate_error or success. Uses recursive_Insert , left_balance, right_balance functions.
38
H=0 H=1
3.
4.
39
1.
if (balance of subroot = left_higher) 1. left_balance (subroot) 2. taller = FALSE // Rebalancing always shortens the tree. else if (balance of subroot = equal_height) 1. subroot->balance = left_higher
else if (balance of subroot = right_higher) 1. subroot->balance = equal_height 2. taller = FALSE
Case c
HL
//
Case b
2.
Case a
\
HL
3.
HL
4.
else // (DataIn > subroot ->data) Insert in the right subtree 1. result = recursive_Insert(subroot->right, DataIn, taller ) 2. if (taller = TRUE) Case a 1. if (balance of subroot = left_higher) HR 1. subroot->balance = equal_height / 2. taller = FALSE Case b
2. else if (balance of subroot = equal_height) 1. subroot->balance = right_higher
HR
1.
Case c else if (balance of subroot = right_higher) HR 1. right_balance (subroot) \ 2. taller = FALSE // Rebalancing always shortens the tree. return result
3.
\\
End recursive_Insert
41
right_tree
subroot
rotate left
Total height = h + 3
1. right_tree = subroot->right 2. subroot->right = right_tree->left
Total height = h + 2
3. right_tree->left = subroot 4. subroot = right_tree 42
End rotate_left
43
Double Rotate
subroot
sub_tree
right_tree
right_tree
sub_tree
subroot
44
Double Rotate
The new balance factors for subroot and right_tree depend on the previous balance factor for subtree
old sub_tree / \
new subroot /
new right_tree \ -
new sub_tree -
45
right_balance function
<void> right_balance (ref subroot <pointer>)
Pre subroot points to a subtree of an AVL tree, doubly unbalanced on the right.
46
47
48
End right_balance
old sub_tree
/ \
new subroot
/
new right_tree
\ -
new sub_tree
49
Removal of a node
Reduce the problem to the case when the node x to be removed has at most one child. We use a parameter shorter to show if the height of a subtree has been shortened. While shorter is TRUE, do the following steps for each node p on the path from the parent of x to the root of the tree.
When shorter becomes FALSE, the algorithm terminates.
50
Removal of a node
Case 1: Node p has balance factor equal. So only this balance factor must be changed. The height of p is unchanged. shorter becomes FALSE.
51
Removal of a node
Case 2: The balance factor of p is not equal, the taller subtree was shortened. So the balance factor must be changed. The height of p is decreased. shorter remains TRUE.
52
Removal of a node
Case 3: The balance factor of p is not equal, the shorter subtree was shortened. So AVL definition is violated at p. Rebalancing must be done. Let q be the root of the taller subtree of p. Case 3a: The balance factor of q is equal. So single rotation needs to do. shorter becomes FALSE.
53
Removal of a node
Case 3b: The balance factor of q is the same as that of p. So single rotation needs to do. Balance factors of p and q become equal. shorter remains TRUE.
54
Removal of a node
Case 3c: The balance factor of q and p are opposite. Double rotation must be done (first around q, then around p). The balance factor of the new root is equal. Other balance factors are set as appropriate. shorter remains TRUE.
55
Removal of a node
Delete p
56
Removal of a node
Delete p
57
Removal of a node
Delete p
Case 2
shorter = TRUE
58
Removal of a node
Delete p
shorter = TRUE
Case 2
59
Removal of a node
Delete p
60
Removal of a node
Delete p
61
Removal of a node
Delete p
62
Removal of a node
Delete p
Case 3c
shorter = TRUE shorter = TRUE
63
Removal of a node
Delete p
Case 3c
shorter = TRUE shorter = TRUE
64
Removal of a node
Delete p
Case 3c shorter = TRUE
65
Removal of a node
Delete p
Case 3c shorter = TRUE
66
67
68
70
Fibonacci trees
Trees, as sparse as possible for AVL tree, are call Fibonacci trees.
71
where
and
72
A random BST, on average, has height about 1.39 lg n. A degenerate BST has height as large as n.
73
74