0% found this document useful (0 votes)
13 views13 pages

Avl Tree - Anusha J

Uploaded by

agalya17prabhu
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)
13 views13 pages

Avl Tree - Anusha J

Uploaded by

agalya17prabhu
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/ 13

AVL TREE

ANUSHA J
WHAT IS AN AVL TREE?
• An AVL Tree is a type of self-balancing
binary search tree (BST) where the
difference between the heights of the left
and right subtrees of every node is at most
1.
• This height difference is called the balance
factor. The purpose of the AVL tree is to
maintain an efficient balance between the
left and right subtrees, ensuring the tree
remains balanced as elements are inserted
or deleted.
• AVL Tree was invented by two Russian
scientists namely: G.M. Adelson-Velsky and
E.M. Landis and the first letters from each
word of their names are taken to name the
balanced tree and we call them AVL trees.
KEY CONCEPTS OF AVL TREE?
1. Balance Factor: The balance factor of a node in an AVL tree is
calculated as the height of its left subtree minus the height of its right
subtree.
• Balance Factor = Height(Left Subtree) - Height(Right Subtree) A node is
said to be Balanced if the balance factor is -1, 0, or +1.Unbalanced if the
balance factor is less than -1 or greater than +1.
2. Height of a Node: The height of a node is the number of edges on the
longest path from the node to a leaf node.
3. Rotations: When an AVL tree becomes unbalanced (i.e., a node has a
balance factor greater than 1 or less than -1), the tree is rebalanced using
rotations. There are four types of rotations:
• Right Rotation (Single Rotation): This is used when the left subtree is
too tall.
• Left Rotation (Single Rotation): This is used when the right subtree is
too tall.
• Left-Right Rotation (Double Rotation): This is used when the left
subtree of the left child is too tall.
• Right-Left Rotation (Double Rotation): This is used when the right
subtree of the right child is too tall.
OPERATIONS ON AVL TREES
• Insertion: Insertions in AVL trees follow the
standard binary search tree insertion rules. After
inserting a node, the tree is checked for balance
starting from the inserted node up to the root. If a
node is found to be unbalanced, a rotation is
performed to restore balance.
• Deletion: Deletion is similar to insertion in terms
of following the BST rules. After deletion, the tree
is checked for balance starting from the node's
parent up to the root, and rotations are performed
if necessary.
• Searching: Searching for a value in an AVL tree is

complexity of search operations is 𝑂(log⁡𝑛)O(logn),


identical to a regular binary search tree. The time

where 𝑛n is the number of nodes in the tree,


because the AVL tree maintains a balanced
AVL TREE CONSTRUCTION
1. Insert 30:
• The tree is initially empty, so we insert 30 as the root.
• Balance Factor of 30:
Height of left subtree−Height of right subtree=0−0=0
• This is balanced (since balance factor is between -1 and
1).
2. Insert 20:
• 20 is less than 30, so it becomes the left child of 30.
• Balance Factor of 30:
Height of left subtree−Height of right subtree=1−0=1
• The tree is still balanced (balance factor is between -1
and 1).
AVL TREE CONSTRUCTION
3. Insert 10:
• 10 is less than 30 and less than 20, so it becomes the
left child of 20.
• Balance Factor of 30:
Height of left subtree−Height of right subtree=2−0=2
• Balance Factor of 20:
Height of left subtree−Height of right subtree=1−0=1
• The tree is unbalanced at node 30 (balance factor is
Tree after Right Rotation:
2), so we perform a right rotation on node 30.
• Right Rotation on 30:
• 20 becomes the new root,30 becomes the right child
of 20.
AVL TREE CONSTRUCTION
4. Insert 25:
25 is less than 30 but greater than 20, so it becomes the right child of 20.
• Balance Factor of 20:
Height of left subtree−Height of right subtree=1−2=−1
• Balance Factor of 30:
Height of left subtree−Height of right subtree=1−0=1
• Balance Factor of 25:
Height of left subtree−Height of right subtree=0−0=0
• The tree is still balanced (balance factor is within the acceptable range).

5. Insert 5:
• 5 is less than 20 and less than 10, so it becomes the left child of 10.
• Balance Factor of 20:
Height of left subtree−Height of right subtree=2−2=0 .
• Balance Factor of 10:
Height of left subtree−Height of right subtree=1−0=1.
• Balance Factor of
30:Height of left subtree−Height of right subtree=1−0=1.
AVL TREE CONSTRUCTION
6. Insert 15:
15 is less than 20 but greater than 10, so it becomes the
right child of 10.
• Balance Factor of 20:
Height of left subtree−Height of right subtree=2−2=0.
• Balance Factor of 10:
Height of left subtree−Height of right subtree=1−1=0.
• Balance Factor of 30:
Height of left subtree−Height of right subtree=1−0=1.
• Balance Factor of 25:
Height of left subtree−Height of right subtree=0−0=0.
• The tree is still balanced.
AVL TREE
CONSTRUCTION
FINAL AVL TREE :
• After all insertions, the tree is
balanced, and no rotations were
necessary after step 3.
ADVANTAGES OF AVL TREES
• Self-Balancing: Automatically
maintains a balanced tree, ensuring that
the height difference between subtrees
of any node is minimal.
• Efficient Operations: Guarantees
logarithmic time complexity for
insertion, deletion, and searching.
• Useful in High-performance
Applications: Particularly when there is
a need for frequent updates (insertions
or deletions) alongside fast lookups.
DISADVANTAGES OF AVL TREES
• Complex Implementation: The logic
of maintaining balance (especially the
rotations) makes AVL trees more
complex to implement compared to
regular binary search trees.
• Overhead: The extra work required to
maintain the tree’s balance (via
rotations) can be a disadvantage when
compared to other balanced trees in
some cases.
APPLICATIONS OF AVL TREES
• Databases: AVL trees are used in
indexing and optimizing data retrieval in
databases.
• File Systems: AVL trees can be used to
maintain balanced structures for file
storage and quick access.
• Priority Queues: They can be used to
implement priority queues where
efficient insertion and deletion are
crucial.
THANK YOU

You might also like