This document discusses AVL trees, which are self-balancing binary search trees. It begins by explaining that binary search trees have O(log N) best case time for operations but can degrade to O(N) in the worst case if the tree becomes unbalanced. The document then introduces AVL trees, which maintain balance by ensuring that the heights of any node's two child subtrees differ by at most one. This guarantees search, insertion, and deletion operations take O(log N) time. The document explains the node height calculation and covers insertion and deletion algorithms in AVL trees through examples, including single and double rotations to rebalance the tree.
Binary search tree.
Balancedand unbalanced BST.
Approaches to balancing trees.
Balancing binary search trees.
Perfect balance.
Avl trees 1962.
Avl good but both perfect balance.
Height of an AVL tree
Nood
Binary search tree.
Balancedand unbalanced BST.
Approaches to balancing trees.
Balancing binary search trees.
Perfect balance.
Avl trees 1962.
Avl good but both perfect balance.
Height of an AVL tree
Nood
AVL Tree in Data Structures- It is height balanced tree with balance factor 1, -1 or 0. The different type of rotations used in this tree are: RR, LL, RL, LR
AVL (Adelson-Velskii and Landis) tree. An AVL tree is identical to a BST except height of the left and right subtrees can differ by at most 1.height of an empty tree is defined to be (–1).
Fast relaxation methods for the matrix exponential David Gleich
The matrix exponential is a matrix computing primitive used in link prediction and community detection. We describe a fast method to compute it using relaxation on a large linear system of equations. This enables us to compute a column of the matrix exponential is sublinear time, or under a second on a standard desktop computer.
AVL Trees
Adelson-Velskii and Landis
Binary Search Tree - Best Time
All BST operations are O(d), where d is tree depth
minimum d is for a binary tree with N nodes
What is the best case tree?
What is the worst case tree?
So, best case running time of BST operations is O(log N)
Binary Search Tree - Worst Time
Worst case running time is O(N)
What happens when you Insert elements in ascending order?
Insert: 2, 4, 6, 8, 10, 12 into an empty BST
Problem: Lack of “balance”:
compare depths of left and right subtree
Unbalanced degenerate tree
Balanced and unbalanced BST
Approaches to balancing trees
Don't balance
May end up with some nodes very deep
Strict balance
The tree must always be balanced perfectly
Pretty good balance
Only allow a little out of balance
Adjust on access
Self-adjusting
Balancing Binary Search Trees
Many algorithms exist for keeping binary search trees balanced
Adelson-Velskii and Landis (AVL) trees (height-balanced trees)
Splay trees and other self-adjusting trees
B-trees and other multiway search trees
Perfect Balance
Want a complete tree after every operation
tree is full except possibly in the lower right
This is expensive
For example, insert 2 in the tree on the left and then rebuild as a complete tree
AVL - Good but not Perfect Balance
AVL trees are height-balanced binary search trees
Balance factor of a node
height(left subtree) - height(right subtree)
An AVL tree has balance factor calculated at every node
For every node, heights of left and right subtree can differ by no more than 1
Store current heights in each node
Height of an AVL Tree
N(h) = minimum number of nodes in an AVL tree of height h.
Basis
N(0) = 1, N(1) = 2
Induction
N(h) = N(h-1) + N(h-2) + 1
Solution (recall Fibonacci analysis)
N(h) > h ( 1.62)
Height of an AVL Tree
N(h) > h ( 1.62)
Suppose we have n nodes in an AVL tree of height h.
n > N(h) (because N(h) was the minimum)
n > h hence log n > h (relatively well balanced tree!!)
h < 1.44 log2n (i.e., Find takes O(logn))
Node Heights
Node Heights after Insert 7
Insert and Rotation in AVL Trees
Insert operation may cause balance factor to become 2 or –2 for some node
only nodes on the path from insertion point to root node have possibly changed in height
So after the Insert, go back up to the root node by node, updating heights
If a new balance factor is 2 or –2, adjust tree by rotation around the node
Single Rotation in an AVL Tree
Implementation
Single Rotation
Double Rotation
Implement Double Rotation in two lines.
Insertion in AVL Trees
Insert at the leaf (as for all BST)
only nodes on the path from insertion point to root node have possibly changed in height
So after the Insert, go back up to the root node by node, updating heights
If a new balance factor is 2 or –2, adjust tree by rotation around the node
Insert in BST
Insert in AVL trees
Example of Insertions in an A
AVL Tree in Data Structures- It is height balanced tree with balance factor 1, -1 or 0. The different type of rotations used in this tree are: RR, LL, RL, LR
AVL (Adelson-Velskii and Landis) tree. An AVL tree is identical to a BST except height of the left and right subtrees can differ by at most 1.height of an empty tree is defined to be (–1).
Fast relaxation methods for the matrix exponential David Gleich
The matrix exponential is a matrix computing primitive used in link prediction and community detection. We describe a fast method to compute it using relaxation on a large linear system of equations. This enables us to compute a column of the matrix exponential is sublinear time, or under a second on a standard desktop computer.
AVL Trees
Adelson-Velskii and Landis
Binary Search Tree - Best Time
All BST operations are O(d), where d is tree depth
minimum d is for a binary tree with N nodes
What is the best case tree?
What is the worst case tree?
So, best case running time of BST operations is O(log N)
Binary Search Tree - Worst Time
Worst case running time is O(N)
What happens when you Insert elements in ascending order?
Insert: 2, 4, 6, 8, 10, 12 into an empty BST
Problem: Lack of “balance”:
compare depths of left and right subtree
Unbalanced degenerate tree
Balanced and unbalanced BST
Approaches to balancing trees
Don't balance
May end up with some nodes very deep
Strict balance
The tree must always be balanced perfectly
Pretty good balance
Only allow a little out of balance
Adjust on access
Self-adjusting
Balancing Binary Search Trees
Many algorithms exist for keeping binary search trees balanced
Adelson-Velskii and Landis (AVL) trees (height-balanced trees)
Splay trees and other self-adjusting trees
B-trees and other multiway search trees
Perfect Balance
Want a complete tree after every operation
tree is full except possibly in the lower right
This is expensive
For example, insert 2 in the tree on the left and then rebuild as a complete tree
AVL - Good but not Perfect Balance
AVL trees are height-balanced binary search trees
Balance factor of a node
height(left subtree) - height(right subtree)
An AVL tree has balance factor calculated at every node
For every node, heights of left and right subtree can differ by no more than 1
Store current heights in each node
Height of an AVL Tree
N(h) = minimum number of nodes in an AVL tree of height h.
Basis
N(0) = 1, N(1) = 2
Induction
N(h) = N(h-1) + N(h-2) + 1
Solution (recall Fibonacci analysis)
N(h) > h ( 1.62)
Height of an AVL Tree
N(h) > h ( 1.62)
Suppose we have n nodes in an AVL tree of height h.
n > N(h) (because N(h) was the minimum)
n > h hence log n > h (relatively well balanced tree!!)
h < 1.44 log2n (i.e., Find takes O(logn))
Node Heights
Node Heights after Insert 7
Insert and Rotation in AVL Trees
Insert operation may cause balance factor to become 2 or –2 for some node
only nodes on the path from insertion point to root node have possibly changed in height
So after the Insert, go back up to the root node by node, updating heights
If a new balance factor is 2 or –2, adjust tree by rotation around the node
Single Rotation in an AVL Tree
Implementation
Single Rotation
Double Rotation
Implement Double Rotation in two lines.
Insertion in AVL Trees
Insert at the leaf (as for all BST)
only nodes on the path from insertion point to root node have possibly changed in height
So after the Insert, go back up to the root node by node, updating heights
If a new balance factor is 2 or –2, adjust tree by rotation around the node
Insert in BST
Insert in AVL trees
Example of Insertions in an A
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptxssuser7b7f4e
It's a type of deep learning algorithm commonly used for image recognition and classification tasks. It's inspired by the human visual system and consists of multiple layers of neurons that perform convolutions, pooling, and activation functions to extract features from input images. CNNs have revolutionized fields like computer vision, achieving state-of-the-art performance in tasks such as object detection, image segmentation, and facial recognition.
The world of search engine optimization (SEO) is buzzing with discussions after Google confirmed that around 2,500 leaked internal documents related to its Search feature are indeed authentic. The revelation has sparked significant concerns within the SEO community. The leaked documents were initially reported by SEO experts Rand Fishkin and Mike King, igniting widespread analysis and discourse. For More Info:- https://news.arihantwebtech.com/search-disrupted-googles-leaked-documents-rock-the-seo-world/
Falcon stands out as a top-tier P2P Invoice Discounting platform in India, bridging esteemed blue-chip companies and eager investors. Our goal is to transform the investment landscape in India by establishing a comprehensive destination for borrowers and investors with diverse profiles and needs, all while minimizing risk. What sets Falcon apart is the elimination of intermediaries such as commercial banks and depository institutions, allowing investors to enjoy higher yields.
[Note: This is a partial preview. To download this presentation, visit:
https://www.oeconsulting.com.sg/training-presentations]
Sustainability has become an increasingly critical topic as the world recognizes the need to protect our planet and its resources for future generations. Sustainability means meeting our current needs without compromising the ability of future generations to meet theirs. It involves long-term planning and consideration of the consequences of our actions. The goal is to create strategies that ensure the long-term viability of People, Planet, and Profit.
Leading companies such as Nike, Toyota, and Siemens are prioritizing sustainable innovation in their business models, setting an example for others to follow. In this Sustainability training presentation, you will learn key concepts, principles, and practices of sustainability applicable across industries. This training aims to create awareness and educate employees, senior executives, consultants, and other key stakeholders, including investors, policymakers, and supply chain partners, on the importance and implementation of sustainability.
LEARNING OBJECTIVES
1. Develop a comprehensive understanding of the fundamental principles and concepts that form the foundation of sustainability within corporate environments.
2. Explore the sustainability implementation model, focusing on effective measures and reporting strategies to track and communicate sustainability efforts.
3. Identify and define best practices and critical success factors essential for achieving sustainability goals within organizations.
CONTENTS
1. Introduction and Key Concepts of Sustainability
2. Principles and Practices of Sustainability
3. Measures and Reporting in Sustainability
4. Sustainability Implementation & Best Practices
To download the complete presentation, visit: https://www.oeconsulting.com.sg/training-presentations
Premium MEAN Stack Development Solutions for Modern BusinessesSynapseIndia
Stay ahead of the curve with our premium MEAN Stack Development Solutions. Our expert developers utilize MongoDB, Express.js, AngularJS, and Node.js to create modern and responsive web applications. Trust us for cutting-edge solutions that drive your business growth and success.
Know more: https://www.synapseindia.com/technology/mean-stack-development-company.html
Enterprise Excellence is Inclusive Excellence.pdfKaiNexus
Enterprise excellence and inclusive excellence are closely linked, and real-world challenges have shown that both are essential to the success of any organization. To achieve enterprise excellence, organizations must focus on improving their operations and processes while creating an inclusive environment that engages everyone. In this interactive session, the facilitator will highlight commonly established business practices and how they limit our ability to engage everyone every day. More importantly, though, participants will likely gain increased awareness of what we can do differently to maximize enterprise excellence through deliberate inclusion.
What is Enterprise Excellence?
Enterprise Excellence is a holistic approach that's aimed at achieving world-class performance across all aspects of the organization.
What might I learn?
A way to engage all in creating Inclusive Excellence. Lessons from the US military and their parallels to the story of Harry Potter. How belt systems and CI teams can destroy inclusive practices. How leadership language invites people to the party. There are three things leaders can do to engage everyone every day: maximizing psychological safety to create environments where folks learn, contribute, and challenge the status quo.
Who might benefit? Anyone and everyone leading folks from the shop floor to top floor.
Dr. William Harvey is a seasoned Operations Leader with extensive experience in chemical processing, manufacturing, and operations management. At Michelman, he currently oversees multiple sites, leading teams in strategic planning and coaching/practicing continuous improvement. William is set to start his eighth year of teaching at the University of Cincinnati where he teaches marketing, finance, and management. William holds various certifications in change management, quality, leadership, operational excellence, team building, and DiSC, among others.
VAT Registration Outlined In UAE: Benefits and Requirementsuae taxgpt
Vat Registration is a legal obligation for businesses meeting the threshold requirement, helping companies avoid fines and ramifications. Contact now!
https://viralsocialtrends.com/vat-registration-outlined-in-uae/
Memorandum Of Association Constitution of Company.pptseri bangash
www.seribangash.com
A Memorandum of Association (MOA) is a legal document that outlines the fundamental principles and objectives upon which a company operates. It serves as the company's charter or constitution and defines the scope of its activities. Here's a detailed note on the MOA:
Contents of Memorandum of Association:
Name Clause: This clause states the name of the company, which should end with words like "Limited" or "Ltd." for a public limited company and "Private Limited" or "Pvt. Ltd." for a private limited company.
https://seribangash.com/article-of-association-is-legal-doc-of-company/
Registered Office Clause: It specifies the location where the company's registered office is situated. This office is where all official communications and notices are sent.
Objective Clause: This clause delineates the main objectives for which the company is formed. It's important to define these objectives clearly, as the company cannot undertake activities beyond those mentioned in this clause.
www.seribangash.com
Liability Clause: It outlines the extent of liability of the company's members. In the case of companies limited by shares, the liability of members is limited to the amount unpaid on their shares. For companies limited by guarantee, members' liability is limited to the amount they undertake to contribute if the company is wound up.
https://seribangash.com/promotors-is-person-conceived-formation-company/
Capital Clause: This clause specifies the authorized capital of the company, i.e., the maximum amount of share capital the company is authorized to issue. It also mentions the division of this capital into shares and their respective nominal value.
Association Clause: It simply states that the subscribers wish to form a company and agree to become members of it, in accordance with the terms of the MOA.
Importance of Memorandum of Association:
Legal Requirement: The MOA is a legal requirement for the formation of a company. It must be filed with the Registrar of Companies during the incorporation process.
Constitutional Document: It serves as the company's constitutional document, defining its scope, powers, and limitations.
Protection of Members: It protects the interests of the company's members by clearly defining the objectives and limiting their liability.
External Communication: It provides clarity to external parties, such as investors, creditors, and regulatory authorities, regarding the company's objectives and powers.
https://seribangash.com/difference-public-and-private-company-law/
Binding Authority: The company and its members are bound by the provisions of the MOA. Any action taken beyond its scope may be considered ultra vires (beyond the powers) of the company and therefore void.
Amendment of MOA:
While the MOA lays down the company's fundamental principles, it is not entirely immutable. It can be amended, but only under specific circumstances and in compliance with legal procedures. Amendments typically require shareholder
What are the main advantages of using HR recruiter services.pdfHumanResourceDimensi1
HR recruiter services offer top talents to companies according to their specific needs. They handle all recruitment tasks from job posting to onboarding and help companies concentrate on their business growth. With their expertise and years of experience, they streamline the hiring process and save time and resources for the company.
RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...BBPMedia1
Grote partijen zijn al een tijdje onderweg met retail media. Ondertussen worden in dit domein ook de kansen zichtbaar voor andere spelers in de markt. Maar met die kansen ontstaan ook vragen: Zelf retail media worden of erop adverteren? In welke fase van de funnel past het en hoe integreer je het in een mediaplan? Wat is nu precies het verschil met marketplaces en Programmatic ads? In dit half uur beslechten we de dilemma's en krijg je antwoorden op wanneer het voor jou tijd is om de volgende stap te zetten.
Business Valuation Principles for EntrepreneursBen Wann
This insightful presentation is designed to equip entrepreneurs with the essential knowledge and tools needed to accurately value their businesses. Understanding business valuation is crucial for making informed decisions, whether you're seeking investment, planning to sell, or simply want to gauge your company's worth.
Tata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s DholeraAvirahi City Dholera
The Tata Group, a titan of Indian industry, is making waves with its advanced talks with Taiwanese chipmakers Powerchip Semiconductor Manufacturing Corporation (PSMC) and UMC Group. The goal? Establishing a cutting-edge semiconductor fabrication unit (fab) in Dholera, Gujarat. This isn’t just any project; it’s a potential game changer for India’s chipmaking aspirations and a boon for investors seeking promising residential projects in dholera sir.
Visit : https://www.avirahi.com/blog/tata-group-dials-taiwan-for-its-chipmaking-ambition-in-gujarats-dholera/
Buy Verified PayPal Account | Buy Google 5 Star Reviewsusawebmarket
Buy Verified PayPal Account
Looking to buy verified PayPal accounts? Discover 7 expert tips for safely purchasing a verified PayPal account in 2024. Ensure security and reliability for your transactions.
PayPal Services Features-
🟢 Email Access
🟢 Bank Added
🟢 Card Verified
🟢 Full SSN Provided
🟢 Phone Number Access
🟢 Driving License Copy
🟢 Fasted Delivery
Client Satisfaction is Our First priority. Our services is very appropriate to buy. We assume that the first-rate way to purchase our offerings is to order on the website. If you have any worry in our cooperation usually You can order us on Skype or Telegram.
24/7 Hours Reply/Please Contact
usawebmarketEmail: support@usawebmarket.com
Skype: usawebmarket
Telegram: @usawebmarket
WhatsApp: +1(218) 203-5951
USA WEB MARKET is the Best Verified PayPal, Payoneer, Cash App, Skrill, Neteller, Stripe Account and SEO, SMM Service provider.100%Satisfection granted.100% replacement Granted.
Implicitly or explicitly all competing businesses employ a strategy to select a mix
of marketing resources. Formulating such competitive strategies fundamentally
involves recognizing relationships between elements of the marketing mix (e.g.,
price and product quality), as well as assessing competitive and market conditions
(i.e., industry structure in the language of economics).
Cracking the Workplace Discipline Code Main.pptxWorkforce Group
Cultivating and maintaining discipline within teams is a critical differentiator for successful organisations.
Forward-thinking leaders and business managers understand the impact that discipline has on organisational success. A disciplined workforce operates with clarity, focus, and a shared understanding of expectations, ultimately driving better results, optimising productivity, and facilitating seamless collaboration.
Although discipline is not a one-size-fits-all approach, it can help create a work environment that encourages personal growth and accountability rather than solely relying on punitive measures.
In this deck, you will learn the significance of workplace discipline for organisational success. You’ll also learn
• Four (4) workplace discipline methods you should consider
• The best and most practical approach to implementing workplace discipline.
• Three (3) key tips to maintain a disciplined workplace.
2. Binary Search Tree - Best
Time
• All BST operations are O(h), where h is
tree height
• minimum h is h log 2N for a binary tree
with N nodes
› What is the best case tree?
› What is the worst case tree?
• So, best case running time of BST
operations is O(log N)
2
3. Binary Search Tree - Worst
Time
• Worst case running time is O(N)
› What happens when you Insert elements in
ascending order?
• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
› Problem: Lack of “balance”:
• compare height of left and right subtree
› Unbalanced degenerate tree
3
5. Approaches to balancing trees
• Don't balance
› May end up with some nodes very deep
• Strict balance
› The tree must always be balanced perfectly
• Pretty good balance
› Only allow a little out of balance
• Adjust on access
› Self-adjusting
5
6. Balancing Binary Search
Trees
• Many algorithms exist for keeping
binary search trees balanced
› Adelson-Velskii and Landis (AVL) trees
(height-balanced trees)
› Splay trees and other self-adjusting trees
› B-trees and other multiway search trees
6
7. Perfect Balance
• Want a complete tree after every operation
› tree is full except possibly in the lower right
• This is expensive
› For example, insert 2 in the tree on the left and
then rebuild as a complete tree
6
5
4
1
9
5
8
Insert 2 &
complete tree
1
2
8
4
6
9
7
8. AVL - Good but not Perfect
Balance
• AVL trees are height-balanced binary
search trees
• Balance factor of a node
› height(left subtree) - height(right subtree)
• An AVL tree has balance factor
calculated at every node
› For every node, heights of left and right
subtree can differ by no more than 1
› Store current heights in each node
8
9. Height of an AVL Tree
• N(h) = minimum number of nodes in an
AVL tree of height h.
• Basis
› N(0) = 1, N(1) = 2
• Induction
h
› N(h) = N(h-1) + N(h-2) + 1
• Solution (recall Fibonacci analysis)
› N(h) >
h
(
1.62)
h-1
h-2
9
10. Height of an AVL Tree
• N(h) > h (
1.62)
• Suppose we have n nodes in an AVL
tree of height h.
› n > N(h) (because N(h) was the minimum)
› n > h hence log n > h (relatively well
balanced tree!!)
› h < 1.44 log2n (i.e., Find takes O(logn))
10
11. Node Heights
Tree A (AVL)
height=2 BF=1-0=1
Tree B (AVL)
2
6
6
1
0
1
1
4
9
4
9
0
0
0
0
0
1
5
1
5
8
height of node = h
balance factor = hleft-hright
empty height = -1
11
12. Node Heights after Insert 7
Tree A (AVL)
2
Tree B (not AVL)
balance factor
1-(-1) = 2
3
6
6
1
1
1
2
4
9
4
9
0
0
0
0
0
1
1
5
7
1
5
-1
8
0
height of node = h
balance factor = hleft-hright
empty height = -1
7
12
13. Insert and Rotation in AVL
Trees
• Insert operation may cause balance factor
to become 2 or –2 for some node
› only nodes on the path from insertion point to
root node have possibly changed in height
› So after the Insert, go back up to the root
node by node, updating heights
› If a new balance factor (the difference hlefthright) is 2 or –2, adjust tree by rotation around
the node
13
14. Single Rotation in an AVL
Tree
2
2
6
6
1
2
1
1
4
9
4
8
0
0
1
0
0
0
0
1
5
8
1
5
7
9
0
7
14
15. Insertions in AVL Trees
Let the node that needs rebalancing be .
There are 4 cases:
Outside Cases (require single rotation) :
1. Insertion into left subtree of left child of .
2. Insertion into right subtree of right child of .
Inside Cases (require double rotation) :
3. Insertion into right subtree of left child of .
4. Insertion into left subtree of right child of .
The rebalancing is performed through four
separate rotation algorithms.
15
22. AVL Insertion: Inside Case
Inserting into Y
destroys the
AVL property
at node j
j
k
h
h
X
Does “right rotation”
restore balance?
h+1
Z
Y
22
23. AVL Insertion: Inside Case
k
j
h
X
“Right rotation”
does not restore
balance… now k is
out of balance
h
h+1
Z
Y
23
24. AVL Insertion: Inside Case
Consider the structure
of subtree Y…
j
k
h
h
X
h+1
Z
Y
24
25. AVL Insertion: Inside Case
j
Y = node i and
subtrees V and W
k
h
i
h
X
h+1
Z
h or h-1
V
W
25
26. AVL Insertion: Inside Case
j
We will do a left-right
“double rotation” . . .
k
Z
i
X
V
W
26
27. Double rotation : first rotation
j
left rotation complete
i
Z
k
W
X
V
27
28. Double rotation : second
rotation
j
Now do a right rotation
i
Z
k
W
X
V
28
29. Double rotation : second
rotation
right rotation complete
Balance has been
restored
i
j
k
h
h
h or h-1
X
V
W
Z
29
30. Implementation
balance (1,0,-1)
key
left
right
No need to keep the height; just the difference in height,
i.e. the balance factor; this has to be modified on the path of
insertion even if you don’t perform rotations
Once you have performed a rotation (single or double) you won’t
need to go back up the tree
30
31. Single Rotation
RotateFromRight(n : reference node pointer) {
p : node pointer;
p := n.right;
n
n.right := p.left;
p.left := n;
n := p
}
You also need to
modify the heights
or balance factors
of n and p
X
Insert
Y
Z
31
32. Double Rotation
• Implement Double Rotation in two lines.
DoubleRotateFromRight(n : reference node pointer) {
????
n
}
X
Z
V
W
32
33. Insertion in AVL Trees
• Insert at the leaf (as for all BST)
› only nodes on the path from insertion point to
root node have possibly changed in height
› So after the Insert, go back up to the root
node by node, updating heights
› If a new balance factor (the difference hlefthright) is 2 or –2, adjust tree by rotation around
the node
33
34. Insert in BST
Insert(T : reference tree pointer, x : element) : integer {
if T = null then
T := new tree; T.data := x; return 1;//the links to
//children are null
case
T.data = x : return 0; //Duplicate do nothing
T.data > x : return Insert(T.left, x);
T.data < x : return Insert(T.right, x);
endcase
}
34
35. Insert in AVL trees
Insert(T : reference tree pointer, x : element) : {
if T = null then
{T := new tree; T.data := x; height := 0; return;}
case
T.data = x : return ; //Duplicate do nothing
T.data > x : Insert(T.left, x);
if ((height(T.left)- height(T.right)) = 2){
if (T.left.data > x ) then //outside case
T = RotatefromLeft (T);
else
//inside case
T = DoubleRotatefromLeft (T);}
T.data < x : Insert(T.right, x);
code similar to the left case
Endcase
T.height := max(height(T.left),height(T.right)) +1;
return;
}
35
41. AVL Tree Deletion
• Similar but more complex than insertion
› Rotations and double rotations needed to
rebalance
› Imbalance may propagate upward so that
many rotations may be needed.
41
42. Deletion of a Node
• Deletion of a node x from an AVL tree
requires the same basic ideas, including
single and double rotations, that are
used for insertion.
• With each node of the AVL tree is
associated a balance factor that is left
high, equal or right high according,
respectively, as the left subtree has
height greater than, equal to, or less
than that of the right subtree.
42
43. Method
1. Reduce the problem to the case when the
node x to be deleted has at most one child.
›
›
If x has two children replace it with its immediate
predecessor y under inorder traversal (the
immediate successor would be just as good)
Delete y from its original position, by proceeding as
follows, using y in place of x in each of the following
steps.
43
44. Method (cont.)
2.
Delete the node x from the tree.
› We’ll trace the effects of this change on height through all the
nodes on the path from x back to the root.
› The action to be taken at each node depends on
•
•
balance factor of the node
sometimes the balance factor of a child of the node.
44
45. Rebalancing the tree
Let p be the node whose balance factor becomes 2 or -2.
› Rotation is needed.
› Let q be the root of the taller subtree of p. We
have three cases according to the balance factor
of q.
45
46. Case a
Case a: The balance factor of q is 0.
› Apply a single rotation
› Height of p is unchanged.
height unchanged
p
1
-1
0
h-1
deleted
q
q
p
-1
T1
h
h
T2
h
T3
h-1
T1
h
T3
T2
46
47. Case b
Case b: The balance factor of q is the same as that of the
previous balance factor of p.
› Apply a single rotation
› Set the balance factors of p and q to 0
height reduced
› Height of the subtree reduced .
p
0
-1
-1
h-1
q
T1
h-1
deleted
p
T2
h
T3
h-1
0
h
h-1
T1
q
T3
T2
47
48. Case c
Case c: The previous balance factors of p and balance
factor of q are opposite.
› Apply a double rotation
› set the balance factors of the new root to 0
› Height of subtree is reduced.
height reduced
-1
p
0
1
h-1
q
r
p
q
r
T1
h-1
T2
h-1
or
h-2
T4
h-1
T3
T1
T2
h-1
or
h-2
T4
T3 h-1
48
63. Pros and Cons of AVL Trees
Arguments for AVL trees:
1. Search is O(log N) since AVL trees are always balanced.
2. Insertion and deletions are also O(logn)
3. The height balancing adds no more than a constant factor to the
speed of insertion.
Arguments against using AVL trees:
1. Difficult to program & debug; more space for balance factor.
2. Asymptotically faster but rebalancing costs time.
3. Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
4. May be OK to have O(N) for a single operation if total run time for
many consecutive operations is fast (e.g. Splay trees).
63