You are on page 1of 63

MH1403 Algorithms and Computing

Lecture 7 AVL Tree

1
Outline

• Self-balancing binary search tree


• AVL tree
• Definition
• Insert
• Erase

2
Self-Balancing Binary Search Tree

• Binary search tree


• A convenient data structure for storing and searching data
• But the worst height of the binary search tree is O(n)
• Self-balancing binary search tree
• Automatically keeps its height small when performing
insertion and deletion operations
• There are a number of self-balancing binary search trees
• AVL tree
• Red-black tree
• Scapegoat tree
• Splay tree
• …….

3
AVL Tree

• AVL tree is the first invented self-balancing binary


search tree
• AVL tree is named after its two inventors Georgy Adelson-
Velsky and Evgenii Landis
• In an AVL tree, the difference between the heights of left
and right subtrees cannot be more than one for all nodes
• An empty tree has height –1
• A tree with a single node (only the root) has height 0
• The height of AVL tree is O(log n)
• The proof of the height of AVL tree is omitted here.
You may refer to the proof at:
https://people.csail.mit.edu/alinush/6.006-spring-2014/avl-height-proof.pdf

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

• Read-only operations (such as find/search and


traverse) on AVL tree are identical to those
operations on binary search tree
• We will learn the following two operations on AVL
tree
• Insertion
• Deletion

8
Insertion Operation on AVL Tree

9
Insertion Operation

• To insert a new node into an AVL tree:


• Step 1. Perform the binary search tree insertion operation
• Step 2. On the path from the new node to the root, find
the first node whose balance factor is changed to -2 or 2.
Denote this node as X.
• If node X does not exist, it means that the AVL tree after
inserting the new node is still an AVL tree. Insertion operation
is completed.
• Step 3. Rebalancing around the node X to change the
balance factors to -1, 0 or 1. Then insertion operation is
completed.
• We will learn the details of the rebalancing operations

10
Insertion Examples

• Example 1. After inserting a node into the AVL tree


directly, the AVL tree is still a valid AVL tree.

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

• Example 2. After inserting a node into the AVL tree


directly, the AVL tree becomes an invalid AVL tree.
Imbalance
BF = 2
2 3
20 Insert node 45 20
0 1 0 2
10 30 10 30
0 0 0 1
25 35 25 35
0
45
12
Rebalancing
• After inserting a new node into AVL tree, suppose that
on the path from the new node to the root, we find the
first node whose balance factor is changed to -2 or 2.
Denote this unbalanced node as X.
• There are four cases for rebalancing the tree:
• Case 1. Node was inserted into left subtree of left child of node X
• Case 2. Node was inserted into right subtree of right child of node X
• Case 3. Node was inserted into right subtree of left child of node X
• Case 4. Node was inserted into left subtree of right child of node X

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

• In general, the single right rotation is performed as follows:


(A, B, C denote three subtrees)
• X becomes the right child of X’ after rotation
• The right subtree of X’ becomes the left subtree of X after rotation

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

• In general, the single left rotation is performed as follows:


(A, B, C denote three subtrees)
• X becomes the left child of X’ after rotation
• The left subtree of X’ becomes the right subtree of X after rotation

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

• In general, the left-right double rotation is performed as


follows: (A, B, C, D denote four subtrees)
step 1 step 2
X X X’’

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

• In general, the right-left double rotation is performed as


follows: (A, B, C, D denote four subtrees)
step 1 step 2
X X X’’

X’ X’’ X X’
A A
X’’ X’
D B
A B C D

B C C D

35
Insertion Example

• Example: We will insert the following sequence of data into an


empty AVL tree:
3, 2, 1, 4, 5, 6, 7, 16, 15, 14
• Insert 3 (Fig. 1)
• Insert 2 (Fig. 2)

3 3

Fig 1 2

Fig 2

36
Insertion Example (cont.)

• Insert 1 (Fig. 3), unbalanced at node 3 (node X)


• Case 1, new node inserted into the left subtree of the left child of node 3
• Perform single right rotation around node 3, obtain Fig. 4.

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.)

• Insert 5 (Fig. 6), unbalanced at node 3 (node X)


• Case 2, new node inserted into the right subtree of right child of node 3
• Perform single left rotation around node 3, obtain Fig. 7

2 2

1 3 1 4

Fig 6 4 3 5
5
Fig 7

39
Insertion Example (cont.)

• Insert 6 (Fig. 8), unbalanced at node 2 (node X)


• Case 2, new node inserted into the right subtree of right child of node 2
• Perform single left rotation around node 2, obtain Fig. 9

2 4

1 4 2 5

3 5 1 3 6

Fig 8
6 Fig 9

40
Insertion Example (cont.)

• Insert 7 (Fig. 10), unbalanced at node 5 (node X)


• Case 2, new node inserted into the right subtree of right child of node 5
• Perform single left rotation around node 5, obtain Fig. 11

4
4

2 5 2 6

1 3 6
1 3 5 7

Fig 10 7 Fig 11

41
Insertion Example (cont.)

• Insert 16 (Fig. 12)

2 6

1 3 5 7

16
Fig 12

42
Insertion Example (cont.)

• Insert 15 (Fig 13), unbalanced at node 7 (node X)


• Case 4, new node inserted into the left subtree of right child of node 7
• Perform right-left double rotation around node 7, obtain Fig 14

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.)

• Insert 14 (Fig 15), unbalanced at node 6 (node X)


• Case 4, new node inserted into the left subtree of right child of node 6
• Perform right-left double rotation around node 6, obtain Fig 16
• Done
4 4

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

• The complexity of insertion operation of AVL tree is


O(log n)
• Inserting a new node into the AVL tree directly requires O(log
n) operations
• Finding the unbalanced node X requires O(log n) operations
(For AVL implementation, we should store and maintain the
height of each node in the implementation)
• Rebalancing the subtree rooted at node X requires O(1)
operations
• Case 1 and Case 2 requires modifying the left (or right) child of three
nodes: node X, X’, the parent of X (before rebalancing)
• Case 3 and Case 4 requires modifying the left (or right) child of four
nodes: node X, X’, X’’, the parent of X (before rebalancing)
• In total, the complexity is O(log n) operations

45
Deletion Operation on AVL Tree

46
Deletion Operation

• To delete a node from an AVL tree,


• Step 1. Perform standard binary search tree deletion
• Step 2. Find the first action node in the AVL tree.
• Action node is the parent of the node being physically deleted
• Step 3. On the path from the action node to the root, find
the first unbalanced node. Denote this node as X.
• If the unbalanced node does not exist, deletion operation is
completed
• Step 4. Rebalancing the subtree rooted at node X
• Step 5. Set the action node as the root of the rebalanced
subtree
• Repeat Step 3, 4, 5 until the deletion operation is
completed

47
Find the First Action Node (1)

• There are three possible scenarios to delete a node


from a binary search tree:
• The node is a leaf node
• The node has exactly one child
• The node has two children

48
Find the First Action Node (2)

• The node to be deleted is a leaf node


• The action node is the parent of the deleted node
• Example: Delete node 5 from the following tree. The
action node is node 10
3 3
20 Action Node 20
1 2 0 2
10 30 10 30
0 0 1 0 1
5 25 35 25 35
0 0 0 0
31 47 31 47

49
Find the First Action Node (3)

• The node to be deleted has exactly one child


• The action node is the parent of the deleted node
• Example: Delete node 10 from the following tree. The
action node is node 20
3 Action Node 3
20 20
1 2 0 2
10 30 5 30
0 0 1 0 1
5 25 35 25 35
0 0 0 0
31 47 31 47
50
Find the First Action Node (4)
• The node to be deleted has two children
• The action node is the parent of the node being physically
deleted (the node being physically deleted is the smallest
node in the right-subtree of the node to be deleted)
• Example: Delete node 30 from the following tree. The
action node is node 35
Action Node
3 3
20 20
1 2 1 2
10 30 10 31
0 0 1 0 0 1
5 25 35 5 25 35
0 0 0
31 47 Physically 31 47
deleted
51
Rebalancing

• Suppose that on the path from the action node to the


root, we find the first node whose balance factor is
changed to -2 or 2. Denote this unbalanced node as X.
• There are four cases for rebalancing the subtree
rooted at node X:
• Case 1. BF of X is -2, BF of the left child of X is -1 or 0
• Case 2. BF of X is 2, BF of the right child of X is 1 or 0
• Case 3. BF of X is -2, BF of the left child of X is 1
• Case 4. BF of X is 2, BF of the right child of X is -1
• Rebalancing the above four cases are identical to the
rebalancing of the four cases of insertion, respectively.

52
Rebalancing

• For insertion operation, we only need to rebalance at


most one unbalanced node, then the whole tree is
balanced
• For insertion operation, after rebalancing the subtree rooted at
an unbalanced node X, the height of the new subtree does not
change (comparing to the subtree before insertion)
• While for deletion operation, we need to rebalance all
the unbalanced nodes on the path from the action node
to the root
• For deletion operation, after rebalancing the subtree rooted at
an unbalanced node X, the height of the new subtree may
decrease by 1 (comparing to the subtree before deletion) , so we
need to consider rebalancing at higher level

53
Deletion Example

• We will delete node 1 from the following AVL tree

54
Deletion Example (cont.)

• After removing node 1, the action node is node 2.


• On the path from node 2 to the root, the first unbalanced
node (node X) is node 3.
• BF of node 3 is 2, BF of the right child of node 3 is 1  Case 2

55
Deletion Example (cont.)

• Rebalancing case 2 requires single left rotation around node


X (node 3), then the subtree rooted at 5 is now balanced.
The tree after this rebalancing is shown below.
• Set the action node as node 5

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.)

• Rebalancing case 4 requires right-left double rotation around


node X (node 3), then the subtree rooted at 11 is now
balanced. The tree after this rebalancing is shown below.
• Set the action node as node 11 .

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.)

• Rebalancing case 2 requires single left rotation around node X (node


21), then the subtree rooted at 34 is now balanced, as shown below.
• Set the action node as node 34
• There is no more unbalanced node. Deletion operation is completed.

60
Complexity of Deletion Operation

• The complexity of deletion operation of AVL tree is


O(log n)
• Deleting a node from AVL tree (before rebalancing)
requires O(log n) operations
• Finding all the unbalanced nodes X requires O(log n)
operations
• Rebalancing the subtree rooted at node X requires O(1)
operations, and we need to rebalancing at most O(log n)
subtrees
• In total, the complexity is O(log n) operations

61
Pros and Cons of AVL Tree

• Pros: AVL tree is more balanced comparing with some other


self-balancing binary search trees
• Cons: AVL tree uses more rotations during insertion and
deletion
• In an application, if the insertions and deletions are less
frequent and search is a more frequent operation, then AVL
tree is preferred.
• If an application involves frequent insertions and deletions,
then some other self-balanced binary search tree (such as
Red-Black tree, not taught in this course) may be preferred

62
Summary

• The height of AVL tree is O(log n)


• The complexity of most of the operations (such as
insertion, deletion, search) on AVL tree is O(log n)
• The height of AVL tree may need to be adjusted
after insertion and deletion operations
• Four cases for rebalancing an unbalanced node after
insertion
• Rebalancing at most one node after insertion
• Similarly, four cases for rebalancing an unbalanced
node after deletion
• May need to rebalancing a number of nodes after deletion

63

You might also like