AVL tree ( Balanced Binary Search Tree)-Data StructureYaksh Jethva
An AVL tree is another balanced binary search tree.
DEFINITION: An AVL Tree is a height-balanced binary search tree.
DEFINITION: The balance factor of a binary tree is the difference in heights of its two subtrees (hR - hL).
The balance factor (bf) of a height balanced binary tree may take on one of the values -1, 0, +1.
OK!!! we have seen that Binary Search Trees (BST) have a nice property the, insertion, deletions, minimum and maximum depends on the height of the tree. However, BST do not have the ability to keep a well balanced tree unless you are willing to randomize the initial input and have only insertions (Cormen’s Chapter 12.4). This is clearly not the case for the average application that uses Binary Trees (Look at “Linux Kernel Development”). Therefore, we require an auto-balancing data structure. This is accomplished by extending the BST into Red-Black Trees (RBT).
A red-black tree is a type of self balancing binary search tree, with one extra attribute for each node: the colour, which is either red or black.
A red–black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.
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 is the first dynamic tree in data structure which minimizes its height during insertion and deletion operations. This is because searching time is directly proportional to the height of binary search tree (BST). When insertion operation is performed it may result into increasing the height of the tree and when deletion is performed it may result into decreasing the height. To make the BST a height balance tree (AVL tree) creators of the AVL tree proposed various rotations. This paper shows BST and its operation and AVL.
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
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 ( Balanced Binary Search Tree)-Data StructureYaksh Jethva
An AVL tree is another balanced binary search tree.
DEFINITION: An AVL Tree is a height-balanced binary search tree.
DEFINITION: The balance factor of a binary tree is the difference in heights of its two subtrees (hR - hL).
The balance factor (bf) of a height balanced binary tree may take on one of the values -1, 0, +1.
OK!!! we have seen that Binary Search Trees (BST) have a nice property the, insertion, deletions, minimum and maximum depends on the height of the tree. However, BST do not have the ability to keep a well balanced tree unless you are willing to randomize the initial input and have only insertions (Cormen’s Chapter 12.4). This is clearly not the case for the average application that uses Binary Trees (Look at “Linux Kernel Development”). Therefore, we require an auto-balancing data structure. This is accomplished by extending the BST into Red-Black Trees (RBT).
A red-black tree is a type of self balancing binary search tree, with one extra attribute for each node: the colour, which is either red or black.
A red–black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.
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 is the first dynamic tree in data structure which minimizes its height during insertion and deletion operations. This is because searching time is directly proportional to the height of binary search tree (BST). When insertion operation is performed it may result into increasing the height of the tree and when deletion is performed it may result into decreasing the height. To make the BST a height balance tree (AVL tree) creators of the AVL tree proposed various rotations. This paper shows BST and its operation and AVL.
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
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
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
What is the purpose of the Sabbath Law in the Torah. It is interesting to compare how the context of the law shifts from Exodus to Deuteronomy. Who gets to rest, and why?
The Indian economy is classified into different sectors to simplify the analysis and understanding of economic activities. For Class 10, it's essential to grasp the sectors of the Indian economy, understand their characteristics, and recognize their importance. This guide will provide detailed notes on the Sectors of the Indian Economy Class 10, using specific long-tail keywords to enhance comprehension.
For more information, visit-www.vavaclasses.com
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
Synthetic fiber production is a fascinating and complex field that blends chemistry, engineering, and environmental science. By understanding these aspects, students can gain a comprehensive view of synthetic fiber production, its impact on society and the environment, and the potential for future innovations. Synthetic fibers play a crucial role in modern society, impacting various aspects of daily life, industry, and the environment. ynthetic fibers are integral to modern life, offering a range of benefits from cost-effectiveness and versatility to innovative applications and performance characteristics. While they pose environmental challenges, ongoing research and development aim to create more sustainable and eco-friendly alternatives. Understanding the importance of synthetic fibers helps in appreciating their role in the economy, industry, and daily life, while also emphasizing the need for sustainable practices and innovation.
This is a presentation by Dada Robert in a Your Skill Boost masterclass organised by the Excellence Foundation for South Sudan (EFSS) on Saturday, the 25th and Sunday, the 26th of May 2024.
He discussed the concept of quality improvement, emphasizing its applicability to various aspects of life, including personal, project, and program improvements. He defined quality as doing the right thing at the right time in the right way to achieve the best possible results and discussed the concept of the "gap" between what we know and what we do, and how this gap represents the areas we need to improve. He explained the scientific approach to quality improvement, which involves systematic performance analysis, testing and learning, and implementing change ideas. He also highlighted the importance of client focus and a team approach to quality improvement.
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
We all have good and bad thoughts from time to time and situation to situation. We are bombarded daily with spiraling thoughts(both negative and positive) creating all-consuming feel , making us difficult to manage with associated suffering. Good thoughts are like our Mob Signal (Positive thought) amidst noise(negative thought) in the atmosphere. Negative thoughts like noise outweigh positive thoughts. These thoughts often create unwanted confusion, trouble, stress and frustration in our mind as well as chaos in our physical world. Negative thoughts are also known as “distorted thinking”.
1. Data Structures & Algorithm
CS-102
Lecture 13,14
AVL TREES
Lecturer: Syeda Nazia Ashraf 1
2. Problem with BST
• The disadvantage of skewed BST is that the
worst case time complexity of a search is O(n)
3. Insertion in Binary Search Tree
BST for 3 4 5 7 9 14 15 16 17 18 20
14
15
4
9
7
18
3
5
16
20
17
Linked List!
3
• Data not Symmetric i.e Right Skewed
• Less nodes, but Height is more
• Go for Linear Search and its efficiency lost
• Time complexity for linear search = O(n),
which is less efficient and take more time
to search
4. Insertion in Binary Search Tree
BST for 14, 15, 4, 9,18, 3
14
15
4
9 18
3
4
Level 0, 20= 1 node
Level 1, 21= 2 nodes
Level 2, 22= 4 nodes
Height=3, i.e max 3 comparisons for searching
Search 9 ?
we move on one side
of the Tree according
to BST comparison
order
• For 10 levels, 29=512 nodes (at last level 9)
• Sum of all nodes=1023 nodes, i.e need max 10 comparisons (Advantage of BST)
• Time complexity for Binary Search= O(log2 n), which is more efficient than Linear Search because it takes
less time
• To make sure when value inserted in BST its height should be balanced, we use AVL TREE
5. Balanced BST
• We should keep the tree balanced.
• One idea would be to have the left and right
subtrees have the same height
5
7. Balanced BST
• We could insist that every node must have left and
right subtrees of same height.
• But this requires that the tree be a complete binary
tree
• To do this, there must have (2d+1 – 1) data items,
where d is the depth of the tree.
• This is too rigid a condition.
7
8. AVL
• There is a need to maintain the binary search tree to
be of balanced height, so that it is possible to
obtained for the search option a time complexity of
O(log n) in the worst case
• One of the most popular balanced tree was
introduced by Adelson-Velskii and Landis (AVL)
9. AVL Tree
• AVL (Adelson-Velskii and Landis) tree (are 2 Russian scientists,
proposed technique in their research paper published in 1962 to
balance binary tree and to save BST from degenerated form )
• 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).
9
10. AVL Tree
• An empty binary tree is an AVL Tree.
• A nonempty binary tree T is an AVL Tree iff given
TL and TR to be the left and right subtrees of T
and h(TL) and h(TR) to be the heights of subtrees
TL and TR respectively, TL and TR are AVL Trees
and | h(TL) - h(TR) | ≤ 1
11. Balanced Binary Tree
• The height of a binary tree is the maximum level of its
leaves (also called the depth).
• The balance of a node in a binary tree is defined as
the height of its left subtree minus height of its right
subtree.
• In balanced tree, each node has an indicated balance
of 1, 0, or –1.
11
12. Balance Factor
• h(TL) - h(TR) is called Balance Factor (BF)
• For AVL Tree, the balance factor of a node can
be either 0, 1, -1
15. Balanced Binary Tree
Insertions and effect on balance
-1
0
1
0
0
0
0
-1
0
1
0
0 0
0 0
0 0
U1 U2 U3 U4
U5 U6 U7 U8 U9 U10 U11 U12
B B
B B
B B
15
16. Balanced Binary Tree
• Tree becomes unbalanced only if the newly inserted
node
▪ is a left descendant (grand child) of a node that
previously had a balance of 1 (U1 to U8),
▪ or is a right descendant (grand child) of a node
that previously had a balance of –1 (U9 to U12)
16
17. Balanced Binary Tree
Insertions and effect on balance
-1
0
1
0
0
0
0
-1
0
1
0
0 0
0 0
0 0
U1 U2 U3 U4
U5 U6 U7 U8 U9 U10 U11 U12
B B
B B
B B
17
18. Inserting New Node in AVL Tree
1
0
A
B
T1
T3
T2
1
Use triangle notation for rest of the nodes
18
19. Inserting New Node in AVL Tree
2
1
A
B
T1
T3
T2
new
1
2
Violate AVL condition
19
20. Searching in AVL Tree
• Searching an AVL search tree for an element is
exactly similar to the method used in a binary
search tree.
22. Not an AVL tree
6
8
1
4
1
5
Height = 4
0
1
2
3
22
• After insertion of node 5, BF of 6 node disturb
• Before next insertion, we have to balance BST
• To balance BST, we use Rotations
• Rotations do shuffling of nodes and make BST balance and transform BFs to -1, 0 and 1
23. Rotations
•To perform the rotations it is necessary to identify a
specific node A whose balance factor (BF) is neither 0,
1, -1 and which is the nearest ancestor to the inserted
node on the path from the inserted node to the root.
24. Rotation Types
Note: The insertion occurs on the “outside” (i.e., left-left or right-right rotations)
Insertion occurs on the “inside” i.e., right-left or left-right rotations)
• LL rotation: Inserted node is in the left subtree of left
subtree of node A.
• RR rotation: Inserted node is in the right subtree of right
subtree of node A.
• LR rotation: Inserted node is in the right subtree of left
subtree of node A.
• RL rotation: Inserted node is in the left subtree of right
subtree of node A.
25. Trick to solve
Note: We also say, LL and RR rotations can fix through Single Rotation.
LR and RL Rotation can fix through Double Rotation
• For LL and RR rotations, identify A and B, then
make A as a child of B.
• FOR LR and RL rotations, identify A, B and C, then
make A and B child of C.
26. Example of Rotation
• Generate AVL Tree for values:
5, 7, 19, 12, 10, 15, 18, 20, 25, 23
• Solve Example on board….