AVL Trees
AVL Trees
AVL TREE
Introduction:
AVL tree is a “height balancing binary search tree and is also known as self-balancing tree”.
AVL tree is the first dynamically balanced trees to be proposed. In 1962, Adelson Velski &
Lendis, the two creators of AVL tree published the concept of AVL tree in the paper “An
algorithm for the organization of information”. Hence, it was given the name AVL. In this
paper, it only described the algorithm on rebalancing the tree after an insertion and updating
the height of the tree. An algorithm on rebalancing after deletion was lacking and it only
appeared later in 1965 when another author submitted a technical report on it. In AVL tree,
the height of the two child sub tree nodes can only differ by 1 and this is known as the
property of AVL tree. Every node maintains an extra information known as “Balance
Factor”.
Binary Search Tree (BST) is sometime skewed because it cannot control the order in which
data comes for insertion. Similarly, AVL tree also cannot control the order in which data
arrives for insertion, but it can re-arrange data in tree. All major operations on a Binary
Search Tree including insertion, deletion and searching depend linearly on its height. For
both average and worst cases, the time complexity for performing insert, search and delete
operations in AVL tree is O (log n) where n is the number of nodes in the tree. The insert and
delete operations is slower and tedious as rotations are required to re-balance the tree.
Therefore, AVL tree are not preferred for applications that involves frequent insert and delete
operations. In 1973, an experiment was conducted by Scoggs to investigate the performance
of AVL tree and to compare the actual costs and timings involved in performing the basic
operations. It was found out that for large trees, searching is most expensive follow by
deletion then by insertion.
AVL tree is often compared with red-black trees as the time complexity is O (log n) for both.
Red, Black (RB) trees are also balanced, but comparatively less balanced then AVL tree. For
this reason, AVL tree appeared to be faster than red-black tree in search operations.
Although, AVL tree appear to be more objective when compare with RB tree, but it causes
more rotations for insertion and deletion. Moreover, the RB tree does not need to be re-
balanced as frequently as AVL tree.
The first variation of AVL tree is RAVL tree (relaxed AVL tree) and is only re-
balanced after insertion, but not after deletion. Thus, deletion can create trees that
are not AVL tree. The time complexity in RAVL tree for performing insert, search
[SIET] Page 1
Trees AVL tree, Red-black tree, Splay tree, B-tree
The second variation of AVL tree is WAVL tree (weak AVL tree). This new kind
of balanced binary tree was proposed by Haupler, San & Tarjan in their paper
“Rank-Balanced Tree” in 2015. WAVL is designed to combine the properties of
AVL tree and Red Black (RB) tree. If no deletion occurs, WAVL tree is the same
as the RAVL tree. The time complexity in WAVL tree for performing insert and
deletion operations at most 2 operations take O (1). Most of the re-balancing in
WAVL take place at the bottom of the tree.
These three diagrams above illustrates balance and unbalanced trees. In figure 1,
the tree is equally balanced as the balance factor is 0. In Figure 2 & 3, the tree is
unbalanced because the balance factor is 2 as illustrated above. In AVL trees, if
the balance factor is greater than 1, the three need to be balance by using one of
the four rotation techniques
Rotations
[SIET] Page 2
Trees AVL tree, Red-black tree, Splay tree, B-tree
Left Rotation
When a node is inserted into the right subtree of the right subtree and the tree
become unbalanced and single left rotation is performed. A simple diagram will be
illustrated to explain this process.
Figure 4 represent the right unbalanced tree. The balance factor of this tree is 2,
thus it needs to be rotated. In Figure 5, left Rotation is performed by making 2 the
left subtree of 1. Figure 6 represents the balanced tree after left rotation.
Right Rotation
Single right rotation required when a node is inserted into the left subtree of the left
subtree and the tree become unbalanced. A simple diagram will be illustrated to
explain this process.
Figure 7 represent the left unbalanced tree. The balance factor of this tree is 2, thus
it needs to be rotated. In Figure 8, left Rotation is performed by making 1 the left
subtree of 2. Figure 9 represents the balanced tree after right rotation.
[SIET] Page 3
Trees AVL tree, Red-black tree, Splay tree, B-tree
Left-Right Rotation
In Figure-10, Z is the root node and node X is a left subtree of Z and node Y is a
right subtree of X. The tree is not balanced as the balance factor of the tree is 2. For
this scenario, a double rotation is required. Firstly, left rotation is performed by
making Y the left subtree of Z and making X the left subtree of Y as shown in
Figure-11. Then, right rotation is performed. Figure- 12 show the balanced tree
after performing left-right rotation.
In Figure-11, Z is the root node and node X is a right subtree of Z and node Y is a
left subtree of X. The tree is not balanced as the balance factor of the tree is 2. For
this scenario, a double rotation is required. Firstly, right rotation is performed by
[SIET] Page 4
Trees AVL tree, Red-black tree, Splay tree, B-tree
making Y the right subtree of Z and making X the right subtree of Y as shown in
Figure-13. Then, right rotation is performed. Figure-14 show the balanced tree
after performing left-right rotation.
Operations Searching
The time complexity for search operation in AVL tree is O(log n). The search
operation is AVL tree is similar as search in binary search tree. This means that all
descendants to the right of a node are greater than the node and all descendants to
the left of a node are less than the node. Steps to perform search operations in
AVL tree will be described below.
Firstly, the element to search provided by the user is read. Then, this element is
compare with the root node value in the tree. If it matches, this value will be return
to the user. If the values do not match, compare search element with the root node
value. If search element is greater than the node value, continue the search process
in right subtree and if not, continue from the left subtree. Repeat the process until
the search element is found or completed with a leaf node. (Sathya, 2017).
Insertion
The time complexity for insert operation in AVL tree is O (log n). While inserting a
new number, insert operation may cause a balance factor of a node to become 2 or -
2 and required rotations to re-adjust the nodes and re-balance the tree. In AVL
tree, new node is inserted as a leaf and the insert will be done recursively. The
procedure for insertion is described below.
The element is inserted into the tree according to the concept of Binary Search Tree
(BST). The difference with BST is that the balance factor of every node is check
after every insertion to ensure that the tree is balanced. The next element will only
be added if the balance factor is either -1, 0 or 1.
Generally, there are 4 cases for insertions (Poonia, 2014)). These are.
[SIET] Page 5
Trees AVL tree, Red-black tree, Splay tree, B-tree
Example
Deletion
The delete operation is more complex than the insert operation. The element is
deleted into the tree according to the concept of Binary Search Tree (BST). The
[SIET] Page 6
Trees AVL tree, Red-black tree, Splay tree, B-tree
difference with BST is that the balance factor of every node is check after every
delete operation to ensure that the tree is balanced.
There are 3 cases to consider before performing the delete operation. Let ‘Y” be
the element to delete.
Storing
[SIET] Page 7
Trees AVL tree, Red-black tree, Splay tree, B-tree
sorted manner. As an example, an AVL tree can be coded together with hash table
for storing and retrieving data. AVL tree can also be implemented to store data read
from an input file. Another important usage of AVL tree is in classification
function of data mining.
Advantages
The first advantage of AVL tree is that it provides faster searching
operations compare to other counterparts such as red-black tree. This
allow the user to complete their task much faster than other search
operations.
This is vital in coding to ensure that projects are completed according to
schedule. In Addition, it is essential when user needs to complete faster
searches and in a more reliable nature.
AVL tree has the capability of performing searching, insertion and
deletion in a more efficient manner when compare with binary search tree.
The second advantage of AVL tree is due to its self-balancing capabilities.
One of the concerns for professional is to ensure that that the trees are
balanced. This is because if the tree is not balanced, the time to perform
operations such as searching, inserting, and deleting will be longer. (Nyln,
2016).
Current Application
The following are the current applications of AVL tree today.
AVL tree is mostly used in databases for indexing large records to improve
searching. Another application of AVL tree is in memory management subsystem
of Linux kernel to search memory. The next application of AVL tree is in
dictionary search engines. Data structure that may be built once without further
reconstruction such as language dictionaries or program dictionaries uses AVL
tree. It can be seen that AVL tree is commonly used in searching as searching in
AVL tree takes O (log n) only.
Another important use of AVL tree is in data mining. Classification is one of the
main functions of data mining. It can be used to solve the issue arise with data
mining. The concern with data mining in large databases is scalability and
[SIET] Page 8
Trees AVL tree, Red-black tree, Splay tree, B-tree
Algorithm:
[SIET] Page 9
Trees AVL tree, Red-black tree, Splay tree, B-tree
Red–black tree
Introduction:
When it comes to searching and sorting data, one of the most fundamental data structures is
the binary search tree. However, the performance of a binary search tree is highly dependent
on its shape, and in the worst case, it can degenerate into a linear structure with a time
complexity of O(n). This is where Red Black Trees come in, they are a type of balanced
binary search tree that use a specific set of rules to ensure that the tree is always balanced.
This balance guarantees that the time complexity for operations such as insertion, deletion,
and searching is always O(log n), regardless of the initial shape of the tree.
Red Black Trees are self-balancing, meaning that the tree adjusts itself automatically after
each insertion or deletion operation. It uses a simple but powerful mechanism to maintain
balance, by coloring each node in the tree either red or black.
Definition:
Red–black tree
Type Tree
Invented 1972
A red–black tree is a type of self-balancing binary search tree, a data structure used in
computer science, typically to implement associative arrays. The original structure was
invented in 1972 by Rudolf Bayer[1] and named "symmetric binary B-tree," but acquired its
modern name in a paper in 1978 by Leonidas J. Guibas and Robert Sedgewick.[2]
Since it is a balanced tree, it guarantees insertion, search and delete to be O(log n) in
time, where n is the total number of elements in the tree. [3]
A red–black tree is a binary search tree that inserts and deletes in such a way that the tree
is always reasonably balanced.
Red-Black tree is a binary search tree in which every node is colored with either red or black.
It is a
[SIET] Page 10
Trees AVL tree, Red-black tree, Splay tree, B-tree
type of self balancing binary search tree. It has a good efficient worst case running time
complexity.
Properties
Operations
Read-only operations on a red–black tree require no modification from those used for
binary search trees, because every red–black tree is a special case of a simple binary
search tree. However, the immediate result of an insertion or removal may violate the
properties of a red–black tree. Restoring the red–black properties requires a small number
(O(log n) or amortized O(1)) of color changes (which are very quick in practice) and
no more than three tree rotations (two for insertion). Although insert and delete
operations are complicated, their times remain O(log n).
[SIET] Page 11
Trees AVL tree, Red-black tree, Splay tree, B-tree
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where
h is the height of the BST. The cost of these operations may become O(n) for a skewed
Binary tree. If we make sure that the height of the tree remains O(log n) after every insertion
and deletion, then we can guarantee an upper bound of O(log n) for all these operations. The
height of a Red-Black tree is always O(log n) where n is the number of nodes in the tree.
A simple example to understand balancing is, that a chain of 3 nodes is not possible in the
Red-Black tree. We can try any combination of colors and see if all of them violate the Red-
Black tree property.
[SIET] Page 12
Trees AVL tree, Red-black tree, Splay tree, B-tree
1. The black height of the red-black tree is the number of black nodes on a path from the
root node to a leaf node. Leaf nodes are also counted as black nodes. So, a red-black
tree of height h has black height >= h/2.
2. Height of a red-black tree with n nodes is h<= 2 log2(n + 1).
3. All leaves (NIL) are black.
4. The black depth of a node is defined as the number of black nodes from the root to
that node
i.e the number of black ancestors.
5. Every red-black tree is a special case of a binary tree.
Black height is the number of black nodes on a path from the root to a leaf. Leaf nodes are
also counted black nodes. From the above properties 3 and 4, we can derive, a Red-Black
Tree of height h has black-height >= h/2.
Every Red Black Tree with n nodes has height <= 2Log2(n+1)
This can be proved using the following facts:
1. For a general Binary Tree, let k be the minimum number of nodes on all root to
NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). This expression can
also be written as k <= Log2(n+1).
2. From property 4 of Red-Black trees and above claim, we can say in a Red-Black Tree
with n nodes, there is a root to leaf path with at-most Log2(n+1) black nodes.
3. From properties 3 and 5 of Red-Black trees, we can claim that the number of black
nodes in a Red-Black tree is at least ⌊ n/2 ⌋ where n is the total number of nodes.
From the above points, we can conclude the fact that Red Black Tree with n nodes has a
height <= 2Log2(n+1).
As every red-black tree is a special case of a binary tree so the searching algorithm of a red-
black tree is similar to that of a binary tree.
Algorithm:
[SIET] Page 13
Trees AVL tree, Red-black tree, Splay tree, B-tree
Step 2: END
Example: Searching 11 in the following red-black tree.
Solution:
[SIET] Page 14
Trees AVL tree, Red-black tree, Splay tree, B-tree
In this post, we introduced Red-Black trees and discussed how balance is ensured. The hard
part is to maintain balance when keys are added and removed. We have also seen how to
search an element from the red-black tree. We will soon be discussing insertion and deletion
operations in coming posts on the Red-Black tree.
Applications:
1. Most of the self-balancing BST library functions like map, multiset, and multimap in
C++ ( or java packages like java.util.TreeMap and java.util.TreeSet ) use Red-Black
Trees.
2. It is used to implement CPU Scheduling Linux. Completely Fair Scheduler uses it.
3. It is also used in the K-mean clustering algorithm in machine learning for reducing
time complexity.
4. Moreover, MySQL also uses the Red-Black tree for indexes on tables in order to
reduce the searching and insertion time.
5. Red Black Trees are used in the implementation of the virtual memory manager in
some operating systems, to keep track of memory pages and their usage.
6. Many programming languages such as Java, C++, and Python have implemented Red
Black Trees as a built-in data structure for efficient searching and sorting of data.
7. Red Black Trees are used in the implementation of graph algorithms such as
Dijkstra’s shortest path algorithm and Prim’s minimum spanning tree algorithm.
8. Red Black Trees are used in the implementation of game engines.
Advantages:
1. Red Black Trees have a guaranteed time complexity of O(log n) for basic operations
like insertion, deletion, and searching.
3. Red Black Trees can be used in a wide range of applications due to their efficient
performance and versatility.
4. The mechanism used to maintain balance in Red Black Trees is relatively simple and
easy to understand.
Disadvantages:
1. Red Black Trees require one extra bit of storage for each node to store the color of the
node (red or black).
2. Complexity of Implementation.
3. Although Red Black Trees provide efficient performance for basic operations, they
may not be the best choice for certain types of data or specific use cases.
[SIET] Page 15
Trees AVL tree, Red-black tree, Splay tree, B-tree
SPLAY TREES
Splay trees are self-adjusting binary trees
Each time a node is accessed, it is moved to root position via rotations
No guarantee of balance (or shallow height)
But good amortized performance
Theorem: Any set of m operations (add, remove, contains, get) on an n-node splay
tree take at most O(m log n) time.
[SIET] Page 16
Trees AVL tree, Red-black tree, Splay tree, B-tree