0% found this document useful (0 votes)
16 views18 pages

AVL Trees

AVL Trees are self-balancing binary search trees that maintain a height difference of no more than one between left and right subtrees, ensuring O(log n) time complexity for operations like search, insert, and delete. They are advantageous for their strict balancing, leading to faster searches compared to other trees like Red-Black Trees, although they can be more complex to implement. AVL Trees are commonly used in scenarios requiring frequent data lookups and consistent performance.

Uploaded by

saifullahmr46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

AVL Trees

AVL Trees are self-balancing binary search trees that maintain a height difference of no more than one between left and right subtrees, ensuring O(log n) time complexity for operations like search, insert, and delete. They are advantageous for their strict balancing, leading to faster searches compared to other trees like Red-Black Trees, although they can be more complex to implement. AVL Trees are commonly used in scenarios requiring frequent data lookups and consistent performance.

Uploaded by

saifullahmr46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

AVL Tree

Ali Mobin Memon


We know what
is tree / BST ?
Right ?
What is
Balancing BST ?
Self-Balancing Binary Search
Trees are height-balanced
binary search trees that
automatically keep the height
as small as possible when
insertion and deletion
operations are performed on
the tree.
Examples of Self Balancing
Tree
Examples: The most common examples of self-balancing binary search trees

are

• AVL Tree

• Red Black Tree and

• 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.

• The cost of these operations may become O(n) for a skewed


Binary tree. If we make sure that the height of the tree remains
O(log(n)) after every insertion and deletion, then we can
guarantee an upper bound of O(log(n)) for all these operations.

• The height of an AVL tree is always O(log(n)) where n is the


number of nodes in the tree.
AVL Tree Operations
• Insertion

• Deletion

• Searching ( same as BST )


Rotating the subtrees in an
AVL Tree:
• 4 ways to keep rotating AVL

• 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.

• It is a BST only (with balancing), so items can be traversed in sorted order.

• 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.

• AVL tree is relatively less complex to understand and implement


compared to Red Black Trees.
Advantages of AVL Tree
• AVL trees can self-balance themselves and therefore provides time
complexity as O(Log n) for search, insert and delete.

• It is a BST only (with balancing), so items can be traversed in sorted order.

• 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.

• AVL tree is relatively less complex to understand and implement


compared to Red Black Trees.
Disadvantages of AVL Tree
• It is difficult to implement compared to normal BST and easier
compared to Red Black

• 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

You might also like