2.5 AVL Trees
2.5 AVL Trees
UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE ENGINEERING
Bachelor of Engineering
Design and Analysis of
Algorithms(CSH-311/ITH-311)
Outcome:
• Student will understand
Tree and operations performed on tree
Different Tree Types
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.
• Why AVL Trees?
Most of the BST operations (e.g., search, max, min, insert,
delete.. etc) take O(h) time where h is the height of the
BST. The cost of these operations may become O(n) for a
skewed Binary tree. If we make sure that height of the
tree remains O(Logn) after every insertion and deletion,
then we can guarantee an upper bound of O(Logn) for all
these operations
Example
• Here we see that the first tree is balanced and the next two trees are not balanced
• In the second tree, the left subtree of C has height 2 and the right subtree has height 0,
so the difference is 2. In the third tree, the right subtree of A has height 2 and the left
is missing, so it is 0, and the difference is 2 again. AVL tree permits difference
(balance factor) to be only 1.
• If the difference in the height of left and right sub-trees is more than 1, the tree is
balanced using some rotation techniques.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
• AVL Rotations
Right rotation
Left-Right rotation
Right-Left 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 −
• 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.
• Right-Left Rotation
• The second type of double rotation is Right-Left Rotation. It is a combination of
right rotation followed by left rotation.
• Left-Right Rotation
B Tree
REFERENCES
Text books:
•Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd edition 2012. problem, Graph coloring.
•Horowitz, Sahni and Rajasekaran, “Fundamentals of ComputerAlgorithms”,
University Press (India), 2nd edition
Websites:
1.https://www.tutorialride.com/data-structures/trees-in-data-structure.htm
2.https://www.geeksforgeeks.org/avl-tree-set-1-insertion/
Summary
Introduction to Trees
• Basic terms
Types of Trees
• Binary tree
• B tree
• Balanced trees