You are on page 1of 41

LECTURE 5, 1

AVL TREES

LECTURER:
DIDAR YEDILKHAN, PHD IN COMPUTER SCIENCE
D.YEDILKHAN@ASTANAIT.EDU.KZ
LESSON AGENDA
1. AVL trees introduction: Motivation
2. AVL trees: Basics
3. AVL trees: Height
4. AVL trees: Rotation cases
5. AVL trees: Illustration and Applications
6. Implementation: How to create a node
7. Implementation: Height, Balance
8. Implementation: Rotations
9. Implementation: Insertion
10. Implementation: Removal
LESSON OUTCOMES
By the end of this lesson, you will be able to understand:
1. Basics of AVL trees including operations with AVL trees (rotation,
insertion, removal)
2. Running time (complexity) of AVL trees
3. Implementation of AVL trees
4. Real-world (practical) applications of AVL trees
INTRODUCTION TO AVL TREES
Motivation:

1) Linked lists: Quite easy to implement. Stores a lot of pointers -> O(N)
search operation time complexity
2) Binary search trees: O(N) can be reduced to O(logN) time complexity.
But is the tree is unbalanced: these operations will become slower and
slower.
3) Balanced binary trees: AVL trees or Red-black – they guarantee that
the tree will be balanced.

-> so, why we need balanced binary trees: O(logN) is guaranteed.


INTRODUCTION TO AVL TREES
Let’s consider the case when you have to construct a Binary Search
Tree from a sorted array: [1, 2, 3, 4]

Conclusion: if we construct a binary search tree from a sorted array, we


end up with a linked list: O(logN) is reduced to O(N)
INTRODUCTION TO AVL TREES
• The running time of BST operations depends on the height of the
binary tree: we should keep the tree balanced in order to get the best
performance
• In 1962 two Russian computer scientists invented (introduced) AVL
trees.
• In an AVL tree, the heights of two child subtrees of any node differ by
at most one
• Another solution is red-black trees (we will consider them in more
details later on this week)
• Operating systems relies heavily on these data structures.
INTRODUCTION TO AVL TREES
Definition: In computer science, an AVL tree (named after inventors
Georgy Adelson-Velsky and Evgenii Landis) is a self-balancing binary
search tree.
In an AVL tree, the heights of the two child subtrees of any node differ by
at most one; if at any time they differ by more than one, rebalancing is
done to restore this property. Lookup (search), insertion, and deletion all
take O(log n) time in both the average and worst cases, where n is the
number of nodes in the tree prior to the operation.
INTRODUCTION TO AVL TREES
• Most of the operations are the same as we have seen for binary
search trees
• Every node can have at most 2 children: the leftChild is smaller, and
the rightChild is greater than the parent node.
• The insertion operation is the same but on every insertion we have to
check whether the tree is balanced or not
• Deletion (removal) operations are the same
• Maximum / minimum finding operations are the same as well.
INTRODUCTION TO AVL TREES
• balancedTree.find (30)
• balancedTree.findMin()
• balancedTree.findMax()
RUNNING TIMES
Average case Worst case
Insert O(logN) O(logN)
Delete O(logN) O(logN)
Search O(logN) O(logN)

If the tree is balanced: all the operations running time is O(logN) in both
average case and worst case -> that is why it is important to use AVL
trees to make sure that your is tree is balanced.
AVL TREE: HEIGHT OF A TREE
Height of a tree – the length of the longest path from it to the leaf node.
We can use recursion to calculate the height of a tree:
node.h (height) = max (leftChild.height(), rightChild.height()) + 1

1. The leaf nodes have NULL children: we consider the height to be -1


for NULLS
2. AVL algorithm uses heights of the nodes: we want the heights as
small as possible -> if it gets high – we fix it (using rotations)
AVL TREE: HEIGHT OF A TREE
node.h (height) = max (leftChild.height(), rightChild.height()) + 1

Important: All subtrees height parameters should not differ for more than 1
AVL TREE: HEIGHT OF A TREE
node.h (height) = max (leftChild.height(), rightChild.height()) + 1

Important: All subtrees height parameters should not differ for more than 1
AVL TREE: HEIGHT OF A TREE
• AVL tree requires the height of the left and right child of every node to
differ at most +1 or -1
• | height(leftSubtree) – height(rightSubtree) | <= 1 (the absolute value
of the difference should be less or equal to 1)
• We can maintain this property in O(logN) time which is quite fast

Insertion operation:
1. A simple BST insertion according to the keys
2. Fix the AVL property on each insertion from insertion upward

• There may be several violations of AVL property from the inserted


node up to the root node and we have to check them all (one rotation
can result in unbalanced structure of parent nodes)
AVL TREE: ROTATIONS
When we perform rotations: We have to update the references which
can be done in O(1) time complexity. The in-order traversal remains the
same
AVL TREE: ROTATIONS
ANIMATION:
AVL TREE: ROTATIONS: CASE 1
Doubly-left heavy situation the difference of height parameters is more
than 1 (it is 2) – so we have to make a rotation to the right

The new root node is B, which was the left child of D before the rotation
AVL TREE: ROTATIONS: CASE 1
BEGIN rotateRight (Node node)

Node tempLeftNode = node.getLeftNode()


Node t = tempLeftNode.getRightNode()

tempLeftNode.setRightNode(node)
node.setLeftNode(t)

node.updateheight ()
tempLeftNode.updateHeight ()

END
AVL TREE: ROTATIONS: CASE 2
Doubly-right heavy situation the difference of height parameters is more
than 1 (it is 2) – so we have to make a rotation to the left

The new root node is D, which was the right child of B before the
rotation
AVL TREE: ROTATIONS: CASE 2
BEGIN rotateLeft (Node node)

Node tempRigthNode = node.getRigthNode()


Node t = tempRightNode.getLeftNode()

tempRightNode.setLeftNode(node)
node.setRightNode(t)

node.updateheight ()
tempRightNode.updateHeight ()

END
AVL TREE: ROTATIONS: CASE 3
Left-right situation when the root has a left child and the left child has a
right child -> the nodes may have left and right children -> we do not
modify the pointer
AVL TREE: ROTATIONS: CASE 4
Right-left situation when the root has a right child and the right child has
a left child -> the nodes may have left and right children -> we do not
modify the pointer
AVL TREE: ILLUSTRATION 1
balancedTree.insert(12):

1. Insert 12 as for a classic binary search tree


2. Make sure (check on every insertion) that the binary tree is
balanced
AVL TREE: ILLUSTRATION 1
Important: To be able to write an algorithm for calculating the height, we
consider null pointers (when a node have no left child for example) to be
of height -1
AVL TREE: ILLUSTRATION 1
After only one rotation: it is a valid balanced tree, the height of any left
and right subtree do not differ more than 1 -> so no further rotations are
needed
AVL TREE: ILLUSTRATION 2
Let’s create a binary tree using the sorted array
balancedTree.insert (10)
balancedTree.insert (20)
balancedTree.insert (30)
balancedTree.insert (40)
balancedTree.insert (50) Resulted tree
AVL TREE: ILLUSTRATION 2
Let’s create a binary tree using the sorted array
balancedTree.insert (60)

The question for students: How we have to inset 60 in the resulted tree?
AVL TREE: ILLUSTRATION 2
Let’s create a binary tree using the sorted array
balancedTree.insert (60)

The question for students: How we have to inset 60 in the resulted tree?
AVL TREE: APPLICATIONS
1) AVL sort: This data structure can be used in order to sort items. We
just have to N items and them perform in-order traversal.
Complexity: O(N*LogN) - (to sort N items) + O(N) (traverse items)

2) Databases when delete or insert items sometimes use AVL tree which
helps them to make a lot of look-ups.
AVL TREE: IMPLEMENTATION
Node class
AVL TREE: IMPLEMENTATION
AVL tree
AVL TREE: IMPLEMENTATION
Rotations:
AVL TREE: IMPLEMENTATION
Insert method:
AVL TREE: IMPLEMENTATION
Violation method:
AVL TREE: IMPLEMENTATION
Traverse method:
AVL TREE: IMPLEMENTATION
Test insertion:
AVL TREE: IMPLEMENTATION
Test insertion:
AVL TREE: IMPLEMENTATION
Test insertion:
AVL TREE: IMPLEMENTATION
Test insertion:
AVL TREE: IMPLEMENTATION
Test insertion (the case from the example – Illustration 2):
WEEK 5 TASK
Please implement the program where you to have to remove a node from the
given binary tree.

Hint:
1. You have to use the same removal structure as for a removal from a
binary search tree
2. When you remove nodes from the tree, your tree can get unbalanced
meaning you have check the balance and make required rotations.

You might also like