0% found this document useful (0 votes)
22 views

Week 13 - AVL Trees

Uploaded by

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

Week 13 - AVL Trees

Uploaded by

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

DSA CS2002

AVL Trees
AVL Trees
ref sec 15.1 Data Structures, Algorithms and
applications in C++

Trees with worst-case height of O(logn) are called balanced trees.

AVL trees are balanced trees which were introduced in 1962 by two
Russian scientists Adelson-Velski and Landis.

Definition: An empty binary tree is an AVL tree. If T is a non-empty


binary tree with TL and TR as its left and right subtrees, then T is
an AVL tree iff (1) TL and TR are AVL trees and (2) |hL-hR |<=1
where hL and hR are heights of TL and TR respectively.

AVL search tree is a binary search tree that is also an AVL tree.
Are the following Binary Trees AVL trees ?
Searching an AVL tree
• Search is exactly similar to how it is
conducted in a binary search tree (the code
may be used as it is)
• Since height of AVL tree with n elements is
O(logn) therefore search time is also
O(logn).
Balance factor of nodes in AVL tree

AVL trees can contain nodes with balance factors of ONLY


-1, 0 or 1
Insertion in an AVL tree
• Insertion in an AVL tree proceeds in the same manner as
in a Binary Search Tree
• If it results in nodes with balance factor other than -1, 0 or
1 then the tree is unbalanced and there is a need to
rebalance it.
• E.g. when 32 is inserted in the following tree
Steps to rebalance an AVL after insertion if
needed
• Identify the nearest ancestor of the newly inserted node whose
balance factor has become -2 or 2 after insertion. Lets call this node
A
•Once A has been identified the imbalance can be classified as either
L (newly inserted node is in the left subtree of A ) or R type
imbalance.
•With this refinement of imbalance classification an imbalance at A
is either LL (new node is inserted in the left subtree of left subtree of
A), LR (new node is inserted in the right subtree of left subtree of A),
RR and RL.
Subtree rotation needed to rebalance an LL type
imbalance
Subtree rotation needed to rebalance an LR
type imbalance
Insert 10 in the following AVL tree

Insert 20 in the following AVL tree

Insert 46 in the following AVL tree


Insert 61 into the following AVL tree
Deletion from an AVL tree
• In case of AVL tree deletion proceeds in the same
manner as in a Binary Search tree (in which case only
nodes of degree 0 or 1 get physically deleted).
• If a delete causes violation of the AVL tree height
property this would have to occur on some node on the
path from the parent of the deleted node. Let q be this
node.
• Let A be the first node encountered on the path from q
to the root which is unbalanced (i.e. With balance
factor -2 or 2)
• This imbalance is classified as type L if deletion took
place from left subtree of A, and R if it took place
from the right subtree.
Deletion from AVL tree (R-type imbalance)

• An R type imbalance is further classified as R0, R-1 or R1


type imbalance depending on the balance factor of the root
of left subtree
• The rotations needed to rebalance the tree for R0 and R1
type imbalance is similar to LL type rotation
• The rotation needed to reblance R-1 type imbalance is
similar to LR type rotation
• Unlike insertion, one rotation may not suffice to restore
balance following a deletion. The number of rotations that
might be needed is O(logn).
Deletion Examples
Delete 15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy