Lecture07 AVL Tree
Lecture07 AVL Tree
1
Outline
2
Self-Balancing Binary Search Tree
3
AVL Tree
4
Balance Factor of AVL Tree
• Each node of the ABL tree has a balance factor (BF)
• The balance factor of a node is computed as:
height(its right subtree) – height(its left subtree)
• In some textbooks, it is computed differently as:
height(its left subtree) – height(its right subtree)
• The balance factor of a node in an AVL tree is -1, 0 or 1
• If the balance factor of a node is smaller than -1 or
larger than 1, it is called an unbalanced node
6
Balance Factor of AVL Tree
• Example: The height and balance factors of the nodes are
shown below. (The height of a node is the height of the
subtree rooted at that node)
3,BF =1
20
1,BF=-1 2,BF=-1
10 30
0,BF = 0 1,BF=0 0,BF=0
5 25 35
0,BF = 0 0,BF = 0
21 27
7
Operations on AVL Tree
8
Insertion Operation on AVL Tree
9
Insertion Operation
10
Insertion Examples
2 2
20 Insert node 5 20
0 1 1 1
10 30 10 30
0 0 0 0
0
25 35 5 25 35
11
Insertion Examples
13
Rebalancing Case 1
• Case 1: Node was inserted into left subtree of left child of node X
• Example: Insert 23 into AVL tree. Imbalance
BF = -2
3 4
20 Insert node 23
20
1 2 1 3
10 30 10 30
0 1 0 0 2 0
5 25 35 5 25 35
0 0 1 0
21 27 21 27
0
23
14
Rebalancing Case 1
• Node 30 is the node X since node 30 is the first unbalanced
node from new node 23 to the root 20.
• Node 23 was inserted into the left subtree of left child (25) of
node 30 (node X) : case 1
4 Imbalance
20 BF = -2
1 3
10 30
0 2 0
5 25 35
1 0
21 27
0
23
15
Rebalancing Case 1
• To rebalance the tree in the previous slide, single right
rotation around the node X is needed
• Note that after the rotation, the right subtree of node 25 becomes
the left subtree of node 30
4 3
20 20
1 3 1 2
10 30 10 25
0 2 0 0 1 1
5 25 35 5 21 30
1 0
0 0 0
21 27
23 27 35
0
23
16
Rebalancing Case 1
X X’
X’ X
C
A
B B C
A
17
Rebalancing Case 1
• Note that single right rotation ensures that the resulting tree
is always a valid binary search tree (each node is larger than
its left tree, and smaller than its right subtree)
• Before rotation, we have the property A < X’ < B < X < C
• After rotation, we still have the property A < X’ < B < X < C
X X’
X’ X
C
A
B B C
A
18
Rebalancing Case 2
• Case 2: Node was inserted into right subtree of right child of node X
• Example: Imbalance
BF = -2
3 4
20 Insert node 42 20
1 2 1 3
10 30 10 30
0 0 1 0 0 2
5 25 35 5 25 35
0 0 0 1
31 47 31 47
0
42
19
Rebalancing Case 2
• Node 30 is now the node X since node 30 is the first
unbalanced node from new node 42 to the root 20.
• Node 42 was inserted into the right subtree of right child (35) of
node 30 (node X) : case 2 Imbalance
4 BF = 2
20
1 3
10 30
0 0 2
5 25 35
0 1
31 47
0
42
20
Rebalancing Case 2
• To rebalance the tree in the previous slide, single left
rotation around node X is needed
• Note that after the rotation, the left subtree of node 35 becomes
the right subtree of node 30
4 3
20 20
1 3 1 2
10 30 10 35
0 0 2 0 1 1
5 25 35 5 30 47
0 1 0 0 0
31 47 25 31 42
0
42
21
Rebalancing Case 2
X X’
X’ X
A
C
B A B
C
22
Rebalancing Case 2
• Note that single left rotation ensures that the resulting tree
is always a valid binary search tree (each node is larger than
its left tree, and smaller than its right subtree)
• Before rotation, we have the property A < X < B < X’ < C
• After rotation, we still have the property A < X < B < X’ < C
X X’
X’ X
A
C
B A B
C
23
Rebalancing Case 3
• Case 3: Node was inserted into right subtree of left child of node X
• Example: Imbalance
BF = -2
3 4
20 Insert node 26
20
1 2 1 3
10 30 10 30
0 1 0 0 2 0
5 25 35 5 25 35
0 0 0 1
21 27 21 27
0
26
24
Rebalancing Case 3
• Node 30 is now the node X since node 30 is the first
unbalanced node from new node 26 to the root 20.
• Node 26 was inserted into the right subtree of left child (25) of
node 30 (node X) : case 3 Imbalance
4 BF = -2
20
1 3
10 30
0 2 0
5 25 35
0 1
21 27
0
26
25
Rebalancing Case 3
• To rebalance the tree in the previous slide, left-right double
rotation is needed
• Step 1: Perform left rotation around the left child of node X
• Step 2: Perform right rotation around node X
26
Rebalancing Case 3
• Step 1: Left-rotation around the left child of node X
4 4
20 20
1 3 1 3
10 30 10 30
0 2 0 0 2 0
5 25 35 5 27 35
0 1 1
21 27 25
0 0 0
26 21 26
27
Rebalancing Case 3
• Step 2: Right-rotation around node X
4 3
20 20
1 3 1 2
10 30 10 27
0 2 0 0 1 1
5 27 35 5 25 30
1 0 0
0
25 26 35
21
0 0
21 26
28
Rebalancing Case 3
X’ X’’ X’ X
D D
X’’ X’
A
C A B C D
B C A B
29
Rebalancing Case 4
• Case 4: Node was inserted into left subtree of right child of node X
• Example: Imbalance
BF = 2
3 4
20 Insert node 34 20
1 2 1 3
10 30 10 30
0 0 1 0 0 2
5 25 35 5 25 35
0 0 1 0
31 47 31 47
0
34
30
Rebalancing Case 4
• Node 30 is now the node X since node 30 is the first
unbalanced node from new node 34 to the root 20.
• Node 34 was inserted into the left subtree of right child (35) of
node 30 (node X) : case 4
4 Imbalance
BF = 2
20
1 3
10 30
0 0 2
5 25 35
1 0
31 47
0
34
31
Rebalancing Case 4
• To rebalance the tree in the previous slide, right-left double
rotation is needed
• Step 1: Perform right rotation around the right child of node X
• Step 2: Perform left rotation around node X
32
Rebalancing Case 4
• Step 1: right-rotation around the right child of node X
4 4
20 20
1 3 1 3
10 30 10 30
0 0 2 0 0 2
5 25 35 5 25 31
1 0 1
31 47 35
0 0 0
34 34 47
33
Rebalancing Case 4
• Step 2: Left-rotation around node X
4 3
20 20
1 3 1 2
10 30 10 31
0 0 2 0 1 1
5 25 31 5 30 35
1 0 0 0
35 25 34 47
0 0
34 47
34
Rebalancing Case 4
X’ X’’ X X’
A A
X’’ X’
D B
A B C D
B C C D
35
Insertion Example
3 3
Fig 1 2
Fig 2
36
Insertion Example (cont.)
2 2
1 3
1 Fig 4
Fig 3
37
Insertion Example (cont.)
• Insert 4 (Fig. 5)
1 3
Fig 5
4
38
Insertion Example (cont.)
2 2
1 3 1 4
Fig 6 4 3 5
5
Fig 7
39
Insertion Example (cont.)
2 4
1 4 2 5
3 5 1 3 6
Fig 8
6 Fig 9
40
Insertion Example (cont.)
4
4
2 5 2 6
1 3 6
1 3 5 7
Fig 10 7 Fig 11
41
Insertion Example (cont.)
2 6
1 3 5 7
16
Fig 12
42
Insertion Example (cont.)
4 4
2 6 2 6
1 3 5 7 1 3 5 15
16 7 16
Fig 13 Fig 14
15
43
Insertion Example (cont.)
2 6 2 7
1 3 5 15 1 3 6 15
7 16 5 14 16
Fig 15 Fig 16
14
44
Complexity of Insertion Operation
45
Deletion Operation on AVL Tree
46
Deletion Operation
47
Find the First Action Node (1)
48
Find the First Action Node (2)
49
Find the First Action Node (3)
52
Rebalancing
53
Deletion Example
54
Deletion Example (cont.)
55
Deletion Example (cont.)
56
Deletion Example (cont.)
• On the path from the new action node (node 5) to the root, the first
unbalanced node is node 8 (node X)
• BF of node 8 is 2, BF of the right child of node 8 is -1 Case 4
57
Deletion Example (cont.)
58
Deletion Example (cont.)
• On the path from the new action node (node 11) to the root, the first
unbalanced node is node 21 (node X)
• BF of node 21 is 2, BF of the right child of node 21 is 1 Case 2
59
Deletion Example (cont.)
60
Complexity of Deletion Operation
61
Pros and Cons of AVL Tree
62
Summary
63