AVL Trees
AVL Trees
are
• AVL Tree
• Splay Tree
AVL tree
Defined as a self-
balancing Binary Search
Tree (BST) where the
difference between
heights of left and right
subtrees for any node
cannot be more than
one.
AVL Tree vs NON AVL Tree
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.
• Deletion
• Left Rotation
• Right Rotation
• Left-Right Rotation
• Right-Left Rotation
AVL Tree
Left Rotation
AVL Tree
Right Rotation
AVL Tree
Left-Right Rotation
AVL Tree
Right-Left Rotation
Advantages of AVL Tree
• AVL trees can self-balance themselves and therefore provides time
complexity as O(Log n) for search, insert and delete.
• Since the balancing rules are strict compared to Red Black Tree, AVL trees
in general have relatively less height and hence the search is faster.
• Since the balancing rules are strict compared to Red Black Tree, AVL trees
in general have relatively less height and hence the search is faster.
• Less used compared to Red-Black trees. Due to its rather strict balance,
AVL trees provide complicated insertion and removal operations as
more rotations are performed.
Applications of AVL Tree
• AVL Tree is used as a first example self balancing BST in teaching DSA as it is easier to
understand and implement compared to Red Black
• Applications, where insertions and deletions are less common but frequent data lookups
along with other operations of BST like sorted traversal, floor, ceil, min and max.
• Red Black tree is more commonly implemented in language libraries like map in C++,
set in C++, TreeMap in Java and TreeSet in Java.
• AVL Trees can be used in a real time environment where predictable and consistent
performance is required.
Thank you
Next week Test – 1 Retake