Академический Документы
Профессиональный Документы
Культура Документы
AVL TREE
Overview
Binary trees
Binary tree
Tree with 02 children per node
Tree
Binary Tree
Trees
Terminology
Root no predecessor Leaf no successor Interior non-leaf Height distance from root to leaf
Root node Interior nodes Height
Leaf nodes
4
Balanced
Height = O( log(n) ) for n nodes Useful for searches
10 45 25 30
30
10
5
2 30 45
5
2 25
30
45
10
25
Requires
Ability to compare key values
10
Observations
O( log(n) ) operation for balanced tree Insertions may unbalance tree
11
Example Insertion
Insert ( 20 )
10 5 2 25 30 45 10 < 20, right 30 > 20, left
20
12
// must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree O( log(n) ) operation for balanced tree Deletions may unbalance tree
Observation
13
5
2 25
30
45
30
45
14
5
2 25
30
45 2
5
25
30
45 2
2
25
30
45
Deleting leaf
15
5
2 25
30
45 2
5
25
30
45 2
30
45
Deleting leaf
Resulting tree
16
17 AVL TREE
Node Heights
Tree A (AVL) height=2 BF=1-0=1 6 1 0 4 9 0 0 5 1
Tree B (AVL) 2 6 1 4
0 5 0 8
1 9
0 1
2 9
-1
Insert operation may cause balance factor to become 2 or 2 for some node
only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or 2, adjust tree by rotation around the node
20 AVL TREE
2 6 1 4 0 1 0 5 0 7 1 8 2 9 0 1 1 4 0 5
2 6 1 8 0 7 0 9
21 AVL TREE
j
h
k
h h
Y
23 AVL TREE
k
h+1 h
Y X
24 AVL TREE
Do a right rotation
k
h+1 h
Y X
25 AVL TREE
Do a right rotation
k
h+1 h
Y X
26 AVL TREE
k
h+1
j
h
h
Z
27 AVL TREE
j
h
k
h
Y
28 AVL TREE
j k
h+1
Y
29 AVL TREE
k
h
j
h+1
X Y
Z
30 AVL TREE
j
h h+1
k
h
Y
31 AVL TREE
j
h
k
h
X V
h+1
h or h-1
W
32 AVL TREE
j k X V
i
W
33 AVL TREE
i
k
Z W
V
34 AVL TREE
i
k
Z W
V
35 AVL TREE
i
k
h h or h-1
j
h
Z
36 AVL TREE
2 20 0 10 0 25 1 30 0 35
Insert 5, 40
37 AVL TREE
2 20 1 10 0 5 0 25 1 30 0 35 0 5 1 10
20
3 2 30 0 25 1 35
Now Insert 45
0 40
38 AVL TREE
3 20 1 10 0 5 0 25 Imbalan ce 2 30 2 35 1 40 0 5 1 10
20
3 2 30 0 25 40 1
0 35 Now Insert 34
0 45
0 45
39 AVL TREE
3 20 1 10 0 5 0 Imbalan 25 ce 1 35 Insertion of 34 0 3 30 2 40 0 5 1 10
20
3 2 35 1 30 40 1 34
45 0
0 25
0 45
34
40 AVL TREE
x
C
8
AVL Tree
3
8
4
3.5
Insert 3.5 4 5
3.5 8
After Rotation
41
An Extended Example
Single rotation
2
3
Fig 1 2 Fig 2 2 1 Fig 4
1 Fig 3 1
Single rotation
1 Fig 5 4
3
Fig 6
3
4
42
2 1 3 1
Single rotation 4
5 Fig 7 4 3 Fig 8 4
4
5
6 Single rotation
6 Fig 10 7
2 1
5
3
Fig 9 2 1 3 5 6 4 1
5
3
6
7 Fig 11
43
4 2 1 3 5
6
7 16
Fig 12
4
Double rotation
2 1 3
6
2
7
6
3 5 15 16
5
Fig 13 15
16
Fig 14
7
44
4 2 1 3
Double rotation 6
5 15 1 2 3
4
7 6 15
16
7 Fig 15 14 5 Fig 16 14
16
45
Rebalancing
47
Delete 55 (case 1)
60 20 10 40 65 70 85
15
30
50
55
80
90
48
Delete 55 (case 1)
60 20 10 40 65 70 85
15
30
50
55
80
90
49
Delete 50 (case 2)
60 20 10 40 65 70 85
15
30
50
55
80
90
50
Delete 50 (case 2)
60 20 10 40 65 70 85
15
30
50
80
90
55
51
Delete 60 (case 3)
60 20 10 40 65 70 85
15
30
50
prev 55
80
90
52
Delete 60 (case 3)
55 20 10 40 65 70 85
15
30
50
80
90
53
Delete 55 (case 3)
55 20 10 40 prev 65 70 85
15
30
50
80
90
54
Delete 55 (case 3)
50 20 10 40 65 70 85
15
30
80
90
55
Delete 50 (case 3)
50 20 10 40 prev 65 70 85
15
30
80
90
56
Delete 50 (case 3)
40 20 10 30 65 70 85
15
80
90
57
Delete 40 (case 3)
40 20 10 30 prev 65 70 85
15
80
90
58
Delete 40 : Rebalancing
30 20 10 Case ? 65 70 85
15
80
90
59
30 10 5 20 65 70 85
15
80
90
60