Professional Documents
Culture Documents
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.
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
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
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)
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)
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):
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.