You are on page 1of 31

AVL Tree

Balanced binary tree


• The disadvantage of a binary search tree is that its height can be as large as N-1
• This means that the time needed to perform insertion and deletion and many
other operations can be O(N) in the worst case
• We want a tree with small height
• A binary tree with N node has height at least (log N)
• Thus, our goal is to keep the height of a binary search tree O(log N)
• Such trees are called balanced binary search trees. Examples are AVL tree, red-
black tree.
AVL Tree
• AVL tree is a self-balancing Binary Search Tree (BST)
where the difference between heights of left and right
subtrees cannot be more than one for all nodes.

AVL Not AVL


tree tree
Applications Of AVL Trees
• AVL trees are mostly used for in-memory sorts of sets and dictionaries.
• AVL trees are also used extensively in database applications in which insertions
and deletions are fewer but there are frequent lookups for data required.
• It is used in applications that require improved searching apart from the database
applications.
How does AVL Tree work? 10

• Consider the following keys inserted in the given order in the 20


binary search tree.
30
• The height of the tree grows linearly in size when we insert the
keys in increasing order of their value. Thus, the search operation,
at worst, takes O(n). 40

• It takes linear time to search for an element; hence there is no use


50
of using the Binary Search Tree structure. On the other hand, if the
height of the tree is balanced, we get better searching time.
60
10,20,30,40,50,60

HERE height of the tree=6


• Let us now look at the same keys but inserted in a different order.
Balance Factor in AVL Trees
• Balance factor (BF) is a fundamental attribute of every node in AVL
trees that helps to monitor the tree’s height.
• Properties of Balance Factor -1 <= HL–HR <= 1
balance factor
• The is known as the difference between the height of the left subtree and
the right subtree.
• Balance factor(node) = height(node->left) – height(node->right)
• Allowed values of BF are –1, 0, and +1.
• The value –1 indicates that the right sub-tree contains one extra, i.e., the
tree is right heavy.
• The value +1 indicates that the left sub-tree contains one extra, i.e., the
tree is left heavy.
• The value 0 shows that the tree includes equal nodes on each side, i.e.,
the tree is perfectly balanced.
There are 4 types of rotation in AVL tree:

• RR rotation: This rotation is performed when a new node is inserted at


the right child of the right subtree.
• LL rotation: This rotation is performed when a new node is inserted at
the left child of the left subtree.
• RL rotation: This rotation is performed when a new node is inserted at
the right child of the left subtree.
• LR rotation: This rotation is performed when a new node is inserted at
the left child of the right subtree.
Example
• Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25
15, 20, 24, 10, 13, 7, 30, 36, 25
20

15
15 24
20
10
24

13

20 20

13 24 15 24

10 15 13

10
15, 20, 24, 10, 13, 7, 30, 36, 25

20
13

13 24 10 20

10 15 7 15 24

7 30

13 36

10 20

7 15 30

24 36
15, 20, 24, 10, 13, 7, 30, 36, 25

13 13

10 20 10 20

7 15 30 7 15 24

24 36 30

25 13 25 36

10 24

7 20 30

15 25 36
Remove 24 and 20 from the AVL tree.

13 13

10 24 10 20

7 20 30 7 15 30

15 25 36 25 36

13 13

10 30 10 15

7 15 36 7 30

25 25 36
Example 2:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree

14

11 17

7 53

4
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree

14

7 17

4 11 53

13
AVL Tree Example:
• Now insert 12

14

7 17

4 11 53

13

12
AVL Tree Example:
• Now insert 12

14

7 17

4 11 53

12

13
AVL Tree Example:
• Now the AVL tree is balanced.

14

7 17

4 12 53

11 13
AVL Tree Example:
• Now insert 8

14

7 17

4 12 53

11 13

8
AVL Tree Example:
• Now insert 8

14

7 17

4 11 53

8 12

13
AVL Tree Example:
• Now the AVL tree is balanced.

14

11 17

7 12 53

4 8 13
AVL Tree Example:
• Now remove 53

14

11 17

7 12 53

4 8 13
AVL Tree Example:
• Now remove 53, unbalanced

14

11 17

7 12

4 8 13
AVL Tree Example:
• Balanced! Remove 11

11

7 14

4 8 12 17

13
AVL Tree Example:
• Remove 11, replace it with the largest in its left branch

7 14

4 12 17

13
AVL Tree Example:
• Remove 8, unbalanced

4 14

12 17

13
AVL Tree Example:
• Remove 8, unbalanced

4 12

14

13 17
AVL Tree Example:
• Balanced!!

12

7 14

4 13 17
• 12,10,3,17,22,34,9,2,5,1

You might also like