SlideShare a Scribd company logo
1 of 32
Download to read offline
Chapter 4: Trees (AVL Trees)
Text: Read Weiss, §4.4
1
AVL Trees
• An AVL (Adelson-Velskii and Landis) tree is
a binary search tree with a balance
condition. It must be easy to maintain, and
it ensures that the depth of the tree is
O (log N).
• Idea 1: left and
right subtrees are
of the same height
(not shallow).
2
Balance Condition for AVL Trees
• Idea 2: Every node must have left and right
subtrees of the same height. The height of an
empty tree is -1 (Only perfectly balanced trees
with 2k-1 nodes would satisfy).
AVL Tree = BST with every node satisfying the
property that the heights of left and right
subtrees can differ
only by one.
The tree on the left is
an AVL tree.
The height info is kept
for each node.
3
The Height of an AVL Tree - I
• For an AVL Tree with N nodes, the height is at
most 1.44log(N+2)-0.328.
• In practice it is slightly more than log N.
• Example: An AVL tree of height 9 with fewest
nodes (143).
4
• The minimum number of nodes, S(h), in an AVL tree of
height h is given by S(0)=1, S(1)=2,
S(h) = S(h-1) + S(h-2) +1
• Recall Fibonacci Numbers? (F(0) = F(1) = 1, F(n)=F(n-1) + F(n-2))
• Claim: S(h) = F(h+2) -1 for all h ≥ 0.
• Proof: By induction. Base cases; S(0)=1=F(2)-1=2-1,
S(1)=2=F(3)-1=3-1. Assume by inductive hypothesis that
claim holds for all heights k ≤ h. Then,
• S(h+1) = S(h) + S(h-1) + 1
= F(h+2) -1 +F(h+1) -1 + 1 (by inductive hypothesis)
= F(h+3) – 1
• We also know that
5
The Height of an AVL Tree - II














 −
−
+
=
hh
hF
2
51
2
51
5
1
)(
• Thus, all the AVL tree operations can be performed
in O (log N) time. We will assume lazy deletions.
Except possibly insertion (update all the balancing
information and keep it balanced)
6
The Height of an AVL Tree - III
2
2
2
51
5
1
)(
11
2
2
51
5
1
)(
1)2()(
1
2
51
5
1
)(
2
51
2
51
5
1
)(
−
+
+
≥
−−
+
+
≥
−+=
−
+
≥
−
−
+
=






































































h
hS
h
hS
hFhS
h
hF
hh
hF
)(log
327.0)2log(44.1
2
51
log*2
5log
2
2
51
log
)2log(
2
51
log)2(5log
2
1
)2log(
2
2
51
5
1
2
2
2
2
51
5
1
)(
nOh
hN
h
N
hN
h
N
h
hSN
=
≥−+
≥
+
+−
+
+
+
++−≥+
+
+
≥+
−
+
+
≥≥








































AVL Tree Insertion
• Example: Let’s try to insert 6 into the AVL tree below. This
would destroy the AVL property of the tree. Then this
property has to be restored before the insertion step is
considered over.
• It turns out that this can always be done with a simple
modification to the tree known as rotation. After an insertion,
only the nodes that are on the path from the insertion point to
the root might have their balance altered. As we follow the
path up to the root and update the
balancing information there may exist
nodes whose new balance violates the
AVL condition. We will prove that our
rebalancing scheme performed once
at the deepest such node works.
7
6
• Let’s call the node to be balanced α. Since any node has at
most 2 children, and a height imbalance requires that α‘s 2
subtrees’ height differ by 2. There are four cases to be
considered for a violation:
1) An insertion into left subtree of the left child of α. (LL)
2) An insertion into right subtree of the left child of α. (LR)
3) An insertion into left subtree of the right child of α. (RL)
4) An insertion into right subtree of the right child of α. (RR)
• Cases 1 and 4 are mirror image symmetries with respect to α,
as are Cases 2 and 3. Consequently; there are 2 basic cases.
• Case I (LL, RR) (insertion occurs on the outside) is fixed by a
single rotation.
• Case II (RL, LR) (insertion occurs on the inside) is fixed by
double rotation.
8
AVL Tree Rotations
Single Rotation (LL)
• Let k2 be the first node on the path up violating AVL
balance property. Figure below is the only possible
scenario that allows k2 to satisfy the AVL property
before the insertion but violate it afterwards. Subtree
X has grown an extra level (2 levels deeper than Z
now). Y cannot be at the same level as X (k2 then out
of balance before insertion) and Y cannot be at the
same level as Z (then k1 would be the first to violate).
9
10
• Note that in single rotation inorder traversal orders
of the nodes are preserved.
• The new height of the subtree is exactly the same
as before. Thus no further updating of the nodes
on the path to the root is needed.
Single Rotation (RR)
• AVL property destroyed by insertion of
6, then fixed by a single rotation.
• BST node structure needs an additional
field for height.
11
Single Rotation-Example I
12
Single Rotation-Example II
• Start with an initially empty tree and insert items 1
through 7 sequentially. Dashed line joins the two
nodes that are the subject of the rotation.
13
Insert 6.
Balance
problem at
the root. So
a single
rotation is
performed.
Finally,
Insert 7
causing
another
rotation.
Single Rotation-Example III
14
Double Rotation (LR, RL) - I
• The algorithm that works for cases 1 and 4 (LL, RR)
does not work for cases 2 and 3 (LR, RL). The
problem is that subtree Y is too deep, and a single
rotation does not make it any less deep.
• The fact that subtree Y has had an item inserted into
it guarantees that it is nonempty. Assume it has a
root and two subtrees.
15
Below are 4 subtrees connected by 3 nodes. Note that
exactly one of tree B or C is 2 levels deeper than D (unless
all empty). To rebalance, k3 cannot be root and a rotation
between k1 and k3 was shown not to work. So the only
alternative is to place k2 as the new root. This forces k1 to
be k2’s left child and k3 to be its right child. It also completely
determines the locations of all 4 subtrees. AVL balance
property is now satisfied. Old height of the tree is restored;
so, all the balancing and and height updating is complete.
Double Rotation (LR) - II
16
Double Rotation (RL) - III
In both cases (LR and RL), the effect is the same as
rotating between α’s child and grandchild and then
between α and its new child. Every double rotation can be
modelled in terms of 2 single rotations. Inorder traversal
orders are always preserved between k1, k2, and k3.
Double RL = Single LL (α->right)+ Single RR (α)
Double LR = Single RR (α->left)+ Single LL (α )
17
• Continuing our example, suppose keys 8
through 15 are inserted in reverse order.
Inserting 15 is easy but inserting 14 causes a
height imbalance at node 7. The double
rotation is an RL type and involves 7, 15, and
14.
Double Rotation Example - I
18
• insert 13: double rotation is RL that will
involve 6, 14, and 7 and will restore the tree.
Double Rotation Example - II
19
Double Rotation Example - III
• If 12 is now inserted, there is an imbalance at
the root. Since 12 is not between 4 and 7, we
know that the single rotation RR will work.
20
Double Rotation Example - IV
• Insert 11: single rotation LL; insert 10: single
rotation LL; insert 9: single rotation LL; insert
8: without a rotation.
21
Double Rotation Example - V
• Insert 8½: double rotation LR. Nodes 8, 8½, 9
are involved.
Implementation Issues - I
• To insert a new node with key X into an AVL
tree T, we recursively insert X into the
appropriate subtree of T (let us call this TLR).
If the height of TLR does not change, then we
are done. Otherwise, if a height imbalance
appears in T, we do the appropriate single or
double rotation depending on X and the keys
in T and TLR, update the heights (making the
connection from the rest of the tree above),
and are done.
22
23
Implementation Issues - II
• Another efficiency issue concerns storage of
the height information. Since all that is really
required is the difference in height, which is
guaranteed to be small, we could get by with
two bits (to represent +1, 0, -1) if we really
try. Doing so will avoid repetitive calculation
of balance factors but results in some loss of
clarity. The resulting code is somewhat more
complicated than if the height were stored at
each node.
24
Implementation Issues - III
• First, the declarations. Also, a quick function
to return the height of a node dealing with the
annoying case of a NULL pointer.
typedef int ElementType;
struct AvlNode;
typedef struct AvlNode *Position;
typedef struct AvlNode *AvlTree;
struct AvlNode {
ElementType Element;
AvlTree Left;
AvlTree Right;
int Height;
};
static int Height( Position P ) {
if( P == NULL )
return -1;
else
return P->Height;
}
25
Implementation - LL
/* This function can be called only if K2 has */
/* a left child. Perform a rotate between */
/* a node (K2) and its left child. Update heights, */
/* then return new root */
static Position SingleRotateWithLeft( Position K2 ){
Position K1;
K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;
K2->Height=Max(Height(K2->Left), Height(K2->Right))+1;
K1->Height=Max(Height(K1->Left), K2->Height)+1;
return K1; /* New root */
}
26
Implementation - RR
/* This function can be called only if K1 has */
/* a right child. Perform a rotate between */
/* a node (K1) and its right child. Update heights, */
/* then return new root */
static Position SingleRotateWithRight( Position K1 ) {
Position K2;
K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;
K1->Height=Max(Height(K1->Left), Height(K1->Right))+1;
K2->Height=Max(Height(K2->Right), K1->Height)+1;
return K2; /* New root */
}
27
Implementation - LR
/* Double LR = Single RR (α->left)+ Single LL (α ) */
/* This function can be called only if K3 has a left */
/* child and K3's left child has a right child */
/* Do the left-right double rotation */
/* Update heights, then return new root */
static Position DoubleRotateWithLeft( Position K3 ){
/* Rotate between K1 and K2 */
K3->Left = SingleRotateWithRight( K3->Left );
/* Rotate between K3 and K2 */
return SingleRotateWithLeft( K3 );
}
28
Implementation - RL
/* Double RL = Single LL (α->right)+ Single RR (α) */
/* This function can be called only if K1 has a right */
/* child and K1's right child has a left child */
/* Do the right-left double rotation */
/* Update heights, then return new root */
static Position DoubleRotateWithRight( Position K1 ){
/* Rotate between K3 and K2 */
K1->Right = SingleRotateWithLeft( K1->Right );
/* Rotate between K1 and K2 */
return SingleRotateWithRight( K1 );
}
29
AvlTree Insert( ElementType X, AvlTree T ){
if( T == NULL ){
/* Create and return a one-node tree */
T = malloc( sizeof( struct AvlNode ) );
if( T == NULL )
FatalError( "Out of space!!!" );
else {
T->Element = X; T->Height = 0;
T->Left = T->Right = NULL;
}
}
else if( X < T->Element ){
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T ); /* LL */
else
T = DoubleRotateWithLeft( T ); /* LR */
} /* continued on the next slide */ /* ... */
ImplementationInsertion-I
30
Implementation – Insertion II
else if( X > T->Element ){
T->Right = Insert( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == 2 )
if( X > T->Right->Element )
T = SingleRotateWithRight( T ); /* RR */
else
T = DoubleRotateWithRight( T ); /* RL */
}
/* Else X is in the tree already; we'll do nothing */
T->Height=Max(Height(T->Left),Height(T->Right))+1;
return T;
}
ImplementationInsertion-II
AVL Tree Deletion* - I
31
AvlTree Delete( ElementType X, AvlTree T ){
if( T == NULL )
Error("Item not Found);
else if( X < T->Element ){
T->Left = Delete( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == -2 )
if( Height(T->Right->Right) > Height(T->Right->Left) )
T = SingleRotateWithRight( T ); /* RR */
else
T = DoubleRotateWithRight( T ); /* RL */
}
else if( X > T->Element ){
T->Right = Delete( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == -2 )
if( Height(T->Left->Left) > Height(T->Left->Right) )
T = SingleRotateWithLeft( T ); /* LL */
else
T = DoubleRotateWithLeft( T ); /* LR */
} /* Continued on the next slide */ /* ... */
32
AVL Tree Deletion* - II
else if( T->Left && T->Right ){ /* Found with two children */
/* Replace with smallest in right subtree */
TmpCell = FindMin( T->Right );
T->Element = TmpCell->Element;
T->Right = Delete( T->Element, T->Right );
if( Height( T->Right ) - Height( T->Left ) == -2 )
if( Height(T->Left->Left) > Height(T->Left->Right) )/*LL*/
T = SingleRotateWithLeft( T );
else
T = DoubleRotateWithLeft( T ); /*LR*/
}
else { /* Found with one or zero child */
TmpCell = T;
T = T->Left ? T->Left : T->Right; /* Also handles 0 child */
free( TmpCell );
}
if( T!= NULL )
T->Height=Max(Height(T->Left), Height(T->Right))+1;
return T;
}

More Related Content

Similar to 9 chapter4 trees_avl

Similar to 9 chapter4 trees_avl (20)

Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
Avl trees
Avl treesAvl trees
Avl trees
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
 
Lecture3
Lecture3Lecture3
Lecture3
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
L15-clean.pptx
L15-clean.pptxL15-clean.pptx
L15-clean.pptx
 
Avl tree
Avl treeAvl tree
Avl tree
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
Balanced
BalancedBalanced
Balanced
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 

More from SSE_AndyLi

Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsChapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsSSE_AndyLi
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsSSE_AndyLi
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDLSSE_AndyLi
 
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicChapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicSSE_AndyLi
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesSSE_AndyLi
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsChapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsSSE_AndyLi
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mstSSE_AndyLi
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpathSSE_AndyLi
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queuesSSE_AndyLi
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bstSSE_AndyLi
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3SSE_AndyLi
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2SSE_AndyLi
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1SSE_AndyLi
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2SSE_AndyLi
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1SSE_AndyLi
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introductionSSE_AndyLi
 

More from SSE_AndyLi (17)

Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsChapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic Functions
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDL
 
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicChapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational Logic
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and Gates
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsChapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital Systems
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 

9 chapter4 trees_avl

  • 1. Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1
  • 2. AVL Trees • An AVL (Adelson-Velskii and Landis) tree is a binary search tree with a balance condition. It must be easy to maintain, and it ensures that the depth of the tree is O (log N). • Idea 1: left and right subtrees are of the same height (not shallow). 2
  • 3. Balance Condition for AVL Trees • Idea 2: Every node must have left and right subtrees of the same height. The height of an empty tree is -1 (Only perfectly balanced trees with 2k-1 nodes would satisfy). AVL Tree = BST with every node satisfying the property that the heights of left and right subtrees can differ only by one. The tree on the left is an AVL tree. The height info is kept for each node. 3
  • 4. The Height of an AVL Tree - I • For an AVL Tree with N nodes, the height is at most 1.44log(N+2)-0.328. • In practice it is slightly more than log N. • Example: An AVL tree of height 9 with fewest nodes (143). 4
  • 5. • The minimum number of nodes, S(h), in an AVL tree of height h is given by S(0)=1, S(1)=2, S(h) = S(h-1) + S(h-2) +1 • Recall Fibonacci Numbers? (F(0) = F(1) = 1, F(n)=F(n-1) + F(n-2)) • Claim: S(h) = F(h+2) -1 for all h ≥ 0. • Proof: By induction. Base cases; S(0)=1=F(2)-1=2-1, S(1)=2=F(3)-1=3-1. Assume by inductive hypothesis that claim holds for all heights k ≤ h. Then, • S(h+1) = S(h) + S(h-1) + 1 = F(h+2) -1 +F(h+1) -1 + 1 (by inductive hypothesis) = F(h+3) – 1 • We also know that 5 The Height of an AVL Tree - II                − − + = hh hF 2 51 2 51 5 1 )(
  • 6. • Thus, all the AVL tree operations can be performed in O (log N) time. We will assume lazy deletions. Except possibly insertion (update all the balancing information and keep it balanced) 6 The Height of an AVL Tree - III 2 2 2 51 5 1 )( 11 2 2 51 5 1 )( 1)2()( 1 2 51 5 1 )( 2 51 2 51 5 1 )( − + + ≥ −− + + ≥ −+= − + ≥ − − + =                                                                       h hS h hS hFhS h hF hh hF )(log 327.0)2log(44.1 2 51 log*2 5log 2 2 51 log )2log( 2 51 log)2(5log 2 1 )2log( 2 2 51 5 1 2 2 2 2 51 5 1 )( nOh hN h N hN h N h hSN = ≥−+ ≥ + +− + + + ++−≥+ + + ≥+ − + + ≥≥                                        
  • 7. AVL Tree Insertion • Example: Let’s try to insert 6 into the AVL tree below. This would destroy the AVL property of the tree. Then this property has to be restored before the insertion step is considered over. • It turns out that this can always be done with a simple modification to the tree known as rotation. After an insertion, only the nodes that are on the path from the insertion point to the root might have their balance altered. As we follow the path up to the root and update the balancing information there may exist nodes whose new balance violates the AVL condition. We will prove that our rebalancing scheme performed once at the deepest such node works. 7 6
  • 8. • Let’s call the node to be balanced α. Since any node has at most 2 children, and a height imbalance requires that α‘s 2 subtrees’ height differ by 2. There are four cases to be considered for a violation: 1) An insertion into left subtree of the left child of α. (LL) 2) An insertion into right subtree of the left child of α. (LR) 3) An insertion into left subtree of the right child of α. (RL) 4) An insertion into right subtree of the right child of α. (RR) • Cases 1 and 4 are mirror image symmetries with respect to α, as are Cases 2 and 3. Consequently; there are 2 basic cases. • Case I (LL, RR) (insertion occurs on the outside) is fixed by a single rotation. • Case II (RL, LR) (insertion occurs on the inside) is fixed by double rotation. 8 AVL Tree Rotations
  • 9. Single Rotation (LL) • Let k2 be the first node on the path up violating AVL balance property. Figure below is the only possible scenario that allows k2 to satisfy the AVL property before the insertion but violate it afterwards. Subtree X has grown an extra level (2 levels deeper than Z now). Y cannot be at the same level as X (k2 then out of balance before insertion) and Y cannot be at the same level as Z (then k1 would be the first to violate). 9
  • 10. 10 • Note that in single rotation inorder traversal orders of the nodes are preserved. • The new height of the subtree is exactly the same as before. Thus no further updating of the nodes on the path to the root is needed. Single Rotation (RR)
  • 11. • AVL property destroyed by insertion of 6, then fixed by a single rotation. • BST node structure needs an additional field for height. 11 Single Rotation-Example I
  • 12. 12 Single Rotation-Example II • Start with an initially empty tree and insert items 1 through 7 sequentially. Dashed line joins the two nodes that are the subject of the rotation.
  • 13. 13 Insert 6. Balance problem at the root. So a single rotation is performed. Finally, Insert 7 causing another rotation. Single Rotation-Example III
  • 14. 14 Double Rotation (LR, RL) - I • The algorithm that works for cases 1 and 4 (LL, RR) does not work for cases 2 and 3 (LR, RL). The problem is that subtree Y is too deep, and a single rotation does not make it any less deep. • The fact that subtree Y has had an item inserted into it guarantees that it is nonempty. Assume it has a root and two subtrees.
  • 15. 15 Below are 4 subtrees connected by 3 nodes. Note that exactly one of tree B or C is 2 levels deeper than D (unless all empty). To rebalance, k3 cannot be root and a rotation between k1 and k3 was shown not to work. So the only alternative is to place k2 as the new root. This forces k1 to be k2’s left child and k3 to be its right child. It also completely determines the locations of all 4 subtrees. AVL balance property is now satisfied. Old height of the tree is restored; so, all the balancing and and height updating is complete. Double Rotation (LR) - II
  • 16. 16 Double Rotation (RL) - III In both cases (LR and RL), the effect is the same as rotating between α’s child and grandchild and then between α and its new child. Every double rotation can be modelled in terms of 2 single rotations. Inorder traversal orders are always preserved between k1, k2, and k3. Double RL = Single LL (α->right)+ Single RR (α) Double LR = Single RR (α->left)+ Single LL (α )
  • 17. 17 • Continuing our example, suppose keys 8 through 15 are inserted in reverse order. Inserting 15 is easy but inserting 14 causes a height imbalance at node 7. The double rotation is an RL type and involves 7, 15, and 14. Double Rotation Example - I
  • 18. 18 • insert 13: double rotation is RL that will involve 6, 14, and 7 and will restore the tree. Double Rotation Example - II
  • 19. 19 Double Rotation Example - III • If 12 is now inserted, there is an imbalance at the root. Since 12 is not between 4 and 7, we know that the single rotation RR will work.
  • 20. 20 Double Rotation Example - IV • Insert 11: single rotation LL; insert 10: single rotation LL; insert 9: single rotation LL; insert 8: without a rotation.
  • 21. 21 Double Rotation Example - V • Insert 8½: double rotation LR. Nodes 8, 8½, 9 are involved.
  • 22. Implementation Issues - I • To insert a new node with key X into an AVL tree T, we recursively insert X into the appropriate subtree of T (let us call this TLR). If the height of TLR does not change, then we are done. Otherwise, if a height imbalance appears in T, we do the appropriate single or double rotation depending on X and the keys in T and TLR, update the heights (making the connection from the rest of the tree above), and are done. 22
  • 23. 23 Implementation Issues - II • Another efficiency issue concerns storage of the height information. Since all that is really required is the difference in height, which is guaranteed to be small, we could get by with two bits (to represent +1, 0, -1) if we really try. Doing so will avoid repetitive calculation of balance factors but results in some loss of clarity. The resulting code is somewhat more complicated than if the height were stored at each node.
  • 24. 24 Implementation Issues - III • First, the declarations. Also, a quick function to return the height of a node dealing with the annoying case of a NULL pointer. typedef int ElementType; struct AvlNode; typedef struct AvlNode *Position; typedef struct AvlNode *AvlTree; struct AvlNode { ElementType Element; AvlTree Left; AvlTree Right; int Height; }; static int Height( Position P ) { if( P == NULL ) return -1; else return P->Height; }
  • 25. 25 Implementation - LL /* This function can be called only if K2 has */ /* a left child. Perform a rotate between */ /* a node (K2) and its left child. Update heights, */ /* then return new root */ static Position SingleRotateWithLeft( Position K2 ){ Position K1; K1 = K2->Left; K2->Left = K1->Right; K1->Right = K2; K2->Height=Max(Height(K2->Left), Height(K2->Right))+1; K1->Height=Max(Height(K1->Left), K2->Height)+1; return K1; /* New root */ }
  • 26. 26 Implementation - RR /* This function can be called only if K1 has */ /* a right child. Perform a rotate between */ /* a node (K1) and its right child. Update heights, */ /* then return new root */ static Position SingleRotateWithRight( Position K1 ) { Position K2; K2 = K1->Right; K1->Right = K2->Left; K2->Left = K1; K1->Height=Max(Height(K1->Left), Height(K1->Right))+1; K2->Height=Max(Height(K2->Right), K1->Height)+1; return K2; /* New root */ }
  • 27. 27 Implementation - LR /* Double LR = Single RR (α->left)+ Single LL (α ) */ /* This function can be called only if K3 has a left */ /* child and K3's left child has a right child */ /* Do the left-right double rotation */ /* Update heights, then return new root */ static Position DoubleRotateWithLeft( Position K3 ){ /* Rotate between K1 and K2 */ K3->Left = SingleRotateWithRight( K3->Left ); /* Rotate between K3 and K2 */ return SingleRotateWithLeft( K3 ); }
  • 28. 28 Implementation - RL /* Double RL = Single LL (α->right)+ Single RR (α) */ /* This function can be called only if K1 has a right */ /* child and K1's right child has a left child */ /* Do the right-left double rotation */ /* Update heights, then return new root */ static Position DoubleRotateWithRight( Position K1 ){ /* Rotate between K3 and K2 */ K1->Right = SingleRotateWithLeft( K1->Right ); /* Rotate between K1 and K2 */ return SingleRotateWithRight( K1 ); }
  • 29. 29 AvlTree Insert( ElementType X, AvlTree T ){ if( T == NULL ){ /* Create and return a one-node tree */ T = malloc( sizeof( struct AvlNode ) ); if( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } else if( X < T->Element ){ T->Left = Insert( X, T->Left ); if( Height( T->Left ) - Height( T->Right ) == 2 ) if( X < T->Left->Element ) T = SingleRotateWithLeft( T ); /* LL */ else T = DoubleRotateWithLeft( T ); /* LR */ } /* continued on the next slide */ /* ... */ ImplementationInsertion-I
  • 30. 30 Implementation – Insertion II else if( X > T->Element ){ T->Right = Insert( X, T->Right ); if( Height( T->Right ) - Height( T->Left ) == 2 ) if( X > T->Right->Element ) T = SingleRotateWithRight( T ); /* RR */ else T = DoubleRotateWithRight( T ); /* RL */ } /* Else X is in the tree already; we'll do nothing */ T->Height=Max(Height(T->Left),Height(T->Right))+1; return T; } ImplementationInsertion-II
  • 31. AVL Tree Deletion* - I 31 AvlTree Delete( ElementType X, AvlTree T ){ if( T == NULL ) Error("Item not Found); else if( X < T->Element ){ T->Left = Delete( X, T->Left ); if( Height( T->Left ) - Height( T->Right ) == -2 ) if( Height(T->Right->Right) > Height(T->Right->Left) ) T = SingleRotateWithRight( T ); /* RR */ else T = DoubleRotateWithRight( T ); /* RL */ } else if( X > T->Element ){ T->Right = Delete( X, T->Right ); if( Height( T->Right ) - Height( T->Left ) == -2 ) if( Height(T->Left->Left) > Height(T->Left->Right) ) T = SingleRotateWithLeft( T ); /* LL */ else T = DoubleRotateWithLeft( T ); /* LR */ } /* Continued on the next slide */ /* ... */
  • 32. 32 AVL Tree Deletion* - II else if( T->Left && T->Right ){ /* Found with two children */ /* Replace with smallest in right subtree */ TmpCell = FindMin( T->Right ); T->Element = TmpCell->Element; T->Right = Delete( T->Element, T->Right ); if( Height( T->Right ) - Height( T->Left ) == -2 ) if( Height(T->Left->Left) > Height(T->Left->Right) )/*LL*/ T = SingleRotateWithLeft( T ); else T = DoubleRotateWithLeft( T ); /*LR*/ } else { /* Found with one or zero child */ TmpCell = T; T = T->Left ? T->Left : T->Right; /* Also handles 0 child */ free( TmpCell ); } if( T!= NULL ) T->Height=Max(Height(T->Left), Height(T->Right))+1; return T; }