Professional Documents
Culture Documents
Froilan De Guzman
Learning outcomes:
- To calculate the balance factor of each nodes on the
binary tree
- To discuss the different rotation types
- To perform self-balancing tree
2
What is AVL tree?
AVL tree is a self-balancing binary search tree
in which each node maintains extra information
called a balance factor whose value is either -1,
0 or +1. AVL tree got its name after its inventor
Georgy Adelson-Velsky and Landis.
Balance Factor - Balance factor of a node in an
AVL tree is the difference between the height of
the left subtree and that of the right subtree of
that node.
Balance Factor =
Height of Left Subtree - Height of Right
Subtree
The self balancing property of an AVL tree is
maintained by the balance factor. The value of
balance factor should always be -1, 0 or +1.
3
Types of Rotation: Left Rotation
• If a tree becomes unbalanced, when a node is inserted into the right subtree
of the right subtree, then we perform a single left rotation
• In our example, node A has become unbalanced as a node is inserted in the
right subtree of A's right subtree. We perform the left rotation by
making A the left-subtree of B.
4
Types of Rotation: Right Rotation
• AVL tree may become unbalanced, if a node is inserted in the left subtree of
the left subtree. The tree then needs a right rotation.
• As depicted, the unbalanced node becomes the right child of its left child by
performing a right rotation.
5
Types of Rotation: Left-Right Rotation
• Double rotations are slightly complex version of already explained versions of rotations.
To understand them better, we should take note of each action performed while rotation.
Let's first check how to perform Left-Right rotation. A left-right rotation is a
combination of left rotation followed by right rotation.
6
Types of Rotation: Right-Left Rotation
• The second type of double rotation is Right-Left Rotation. It is a combination of right
rotation followed by left rotation.
7
Example #1: Insert the following
data items on the AVL tree
• 15
• 7
• 28
• 5
• 3
• 10
• 33
• 6
• 21
• 20
8
Example #1: Insert the following
data items on the AVL tree
• 15 15 0
• 7
• 28
• 5
• 3
• 10
• 33
• 6
• 21
• 20
9
Example #1: Insert the following
data items on the AVL tree
• 15 15 1
• 7
• 28
7 0
• 5
• 3
• 10
• 33
• 6
• 21
• 20
10
Example #1: Insert the following
data items on the AVL tree
• 15 15 0
• 7
• 28
7 0 28 0
• 5
• 3
• 10
• 33
• 6
• 21
• 20
11
Example #1: Insert the following
data items on the AVL tree
• 15 15 1
• 7
• 28
7 1 28 0
• 5
• 3
• 10 5 0
• 33
• 6
• 21
• 20
12
Example #1: Insert the following
data items on the AVL tree
• 15 15 2
• 7
• 28
7 2 28 0
• 5
• 3
• 10 5 1
• 33
• 6
3 0
• 21
• 20
13
Example #1: Insert the following
data items on the AVL tree
• 15 15 2
• 7
• 28
7 2 28 0
• 5
• 3
• 10 5 1
• 33
• 6
3 0
• 21
•
Perform Right Rotation
20
14
Example #1: Insert the following
data items on the AVL tree
• 15 15 1
• 7
• 28
5 0 28 0
• 5
• 3
• 10 3 0 7 0
• 33
• 6
• 21
• 20
15
Example #1: Insert the following
data items on the AVL tree
• 15 15 2
• 7
• 28
5 -1 28 0
• 5
• 3
• 10 3 0 7 -1
• 33
• 6
10 0
• 21
• 20
16
Example #1: Insert the following
data items on the AVL tree
• 15 15 2
• 7
• 28
5 -1 28 0
• 5
• 3
• 10 3 0 7 -1
• 33
• 6
10 0
• 21
• 20
Perform Left-Right Rotation 17
Example #1: Insert the following
data items on the AVL tree
7 0
• 15
• 7
• 28
5 1 15 0
• 5
• 3
• 10 3 0 10 0 28 0
• 33
• 6
• 21
• 20
18
Example #1: Insert the following
data items on the AVL tree
7 -1
• 15
• 7
• 28
5 1 15 -1
• 5
• 3
• 10 3 0 10 0 28 -1
• 33
• 6
33 0
• 21
• 20
18
Example #1: Insert the following
data items on the AVL tree
7 -1
• 15
• 7
• 28
5 0 15 -1
• 5
• 3
• 10 3 0 6 0 10 0 28 -1
• 33
• 6
33 0
• 21
• 20
18
Example #1: Insert the following
data items on the AVL tree
7 -1
• 15
• 7
• 28
5 0 15 -1
• 5
• 3
• 10 3 0 6 0 10 0 28 0
• 33
• 6
21 0 33 0
• 21
• 20
18
Example #1: Insert the following
data items on the AVL tree
7 -2
• 15
• 7
• 28
5 0 15 -2
• 5
• 3
• 10 3 0 6 0 10 0 28 1
• 33
• 6
21 1 33 0
• 21
• 20
20 0
18
Example #1: Insert the following
data items on the AVL tree
7 -2
• 15
• 7
• 28
5 0 15 -2
• 5
• 3
• 10 3 0 6 0 10 0 28 1
• 33
• 6
21 1 33 0
• 21
• 20
20 0
Perform Right-Left Rotation 18
Example #1: Insert the following
data items on the AVL tree
7 -1
• 15
• 7
• 28 21 0
5 0
• 5
• 3
• 10 3 0 15 0 28 -1
6 0
• 33
• 6
• 21 10 0 20 0
33 0
• 20
18
Example #2: Insert the following
data items on the AVL tree
• 1
• 2
• 3
• 4
• 5
• 6
• 7
27
Example #2: Insert the following
data items on the AVL tree
• 1 1 0
• 2
• 3
• 4
• 5
• 6
• 7
28
Example #2: Insert the following
data items on the AVL tree
• 1 1 -1
• 2
• 3 2 0
• 4
• 5
• 6
• 7
29
Example #2: Insert the following
data items on the AVL tree
• 1 1 -2
• 2
• 3 2 -1
• 4
• 5
3 0
• 6
• 7
30
Example #2: Insert the following
data items on the AVL tree
• 1 1 -2
• 2
• 3 2 -1
• 4
• 5
3 0
• 6
• 7
32
Example #2: Insert the following
data items on the AVL tree
2 -1
• 1
• 2
• 3 1 0 3 -1
• 4
• 5
• 6 4 0
• 7
33
Example #2: Insert the following
data items on the AVL tree
2 -2
• 1
• 2
• 3 1 0 3 -2
• 4
• 5
• 6 4 -1
• 7
5 0
34
Example #2: Insert the following
data items on the AVL tree
2 -2
• 1
• 2
• 3 1 0 3 -2
• 4
• 5
• 6 4 -1
• 7
5 0
36
Example #2: Insert the following
data items on the AVL tree
2 -2
• 1
• 2
• 3 1 0 4 -1
• 4
• 5
• 6 3 0 5 -1
• 7
6 0
37
Example #2: Insert the following
data items on the AVL tree
2 -2
• 1
• 2
• 3 1 0 4 -1
• 4
• 5
• 6 3 0 5 -1
• 7
6 0
39
Example #2: Insert the following
data items on the AVL tree
4 -1
• 1
• 2
2 0 5 --2
• 3
• 4
• 5 1 0 3 0 6 -1
• 6
• 7
7 0
40
Example #2: Insert the following
data items on the AVL tree
4 -1
• 1
• 2
2 0 5 --2
• 3
• 4
• 5 1 0 3 0 6 -1
• 6
• 7
7 0
42
References:
- https://www.programiz.com/dsa/avl-tree
- https://www.tutorialspoint.com/data_structures_algorithms/avl_tree_algorithm.htm
43
CDS 1101 – Final Project
Any game application of Data structures and algorithm in Python