0% found this document useful (0 votes)
14 views17 pages

AVL Trees

AVL trees

Uploaded by

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

AVL Trees

AVL trees

Uploaded by

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

Trees AVL tree, Red-black tree, Splay tree, B-tree

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.

Variations of AVL tree


 Relaxed AVL tree (RAVL)
 Weak AVL tree (WAVL)

Relaxed AVL tree (RAVL):

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

and deletion operations is O(h+1), where h Is the height of the tree.

Weak AVL tree (WAVL):

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.

Balance Factor = heightOfLeftSubtree -


heightOfRightSubtree

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

There are four rotations


 Left Rotation
 Right Rotation
 Left-Right Rotation
 Right Left Rotation

[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

The first type of double rotation is a left-right rotation. “Left-Right rotation is a


combination of left rotation followed by right rotation”. A simple diagram will
be illustrated to explain this process.

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.

Right Left Rotation

The second type of double rotation is a right-left rotation. “Right-Left rotation is


a combination of right rotation followed by left rotation”. A simple diagram
will be illustrated to explain this process.

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.

 Outside Cases (only single rotation is required)


o Insertion into left-subtree of left child

[SIET] Page 5
Trees AVL tree, Red-black tree, Splay tree, B-tree

o Insertion into right-subtree of right child


 Inside Cases (double rotation is required

o Insertion into right-subtree of left child


o Insertion into left-subtree of right child

Example

A simple example will be illustrated below with a construction of an AVL tree


by inserting number 1,2,3,4,5,6 in order.

In A, the tree become unbalanced after inserting 3. As 3 is inserted into right-


subtree of the right child, a single left rotation is performed to re-balance the tree.
In C, thee tree become unbalanced after inserting 5. As 5 is also inserted into right-
subtree of the right child, a single left rotation is performed to re-balance the tree.
F represents the balanced tree after inserting number 1 to 6 in order.

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.

 Case 1  if “Y” is a leaf, delete Y. (Figure-1)


 Case 2  if Y has one child, use the child to replace the Y. (Figure-2)
 Case 3  if Y have two children, replace Y with its in-order predecessor
(right most child of left-subtree) and delete recursively. (Figure-3)

Why we use AVL tree ? Searching


AVL tree can be used for many purposes. The first and most common usage of
AVL tree is in search intensive applications as searching takes O (log n) in AVL
tree as the tree is balanced. AVL tree is used to create index of search keywords for
a bunch of documents (see current application of AVL tree). As an example, it is
used in designing databases where insertions and deletions are not frequent but
require frequent search operations for items present in there. AVL tree is also
implemented in creating search engines to provide faster search results. This is
because search engine requires to response to query as fast as possible. As an
example, search engine in fingerprint databases used AVL tree as the fingerprints
discovered in crime scenes need to be compared and searched faster in fingerprint
databases.

Storing

Another usage of AVL tree is for storing information in an efficient and

[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

efficiency. Decisions trees are widely used with classification method. As


classification model construction process is performed on huge data, the algorithms
may result in very bushy or meaningless results

Biometric authentication such as fingerprint can be used to verify a person and


in crime scene. Sets of fingerprints are searched in large databases and compared
in real time. To reduce consuming time, this require fast search engine in
searching or retrieving from large fingerprint databases. Experiments have been
conducted to find out the best searching methods among queue, SQL, graph
search, hash, binary and AVL tree as methods for searching and retrieving from
large databases According to results, it was found out the AVL tree algorithm is
the best method for searching (Elmadani, 2016).

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

Invented by Rudolf Bayer

Time complexity in big O notation

Average Worst case

Space O(n) O(n)

Search O(log n) O(log n)

Insert O(log n) O(log n)

Delete O(log n) O(log n)

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

A red–black tree is a binary search tree


where each node has a color attribute,
the value of which is either red or black.
In addition to the ordinary requirements
imposed on binary search trees,
the following requirements apply to
red–black trees:

1. A node is either red or black.


2. The root is black. (This rule is sometimes omitted from other definitions. Since the root can
always be changed from red to black, but not necessarily vice-versa, this rule has little effect
on analysis.)
3. All leaves are the same color as the root.
4. Both children of every red node are black.
5. Every simple path from a given node to any of its descendant leaves contains the same
number of black nodes. These constraints enforce a critical property of red–black trees: that
the path from the root to the furthest leaf is no more than twice as long as the path from the
root to the nearest leaf. The result is that the tree is roughly balanced. Since operations such as
inserting, deleting, and finding values require worst-case time proportional to the height of the
tree, this theoretical upper bound on the height allows red–black trees to be efficient in the
worst-case, unlike ordinary binary search trees.

6. Root property: The root is black.


7. External property: Every leaf (Leaf is a NULL child of a node) is black in Red-Black
tree.
8. Internal property: The children of a red node are black. Hence possible parent of red
node is a black node.
9. Depth property: All the leaves have the same black depth.
10. Path property: Every simple path from root to descendant leaf node contains same
number of black nodes.
11. The result of all these above-mentioned properties is that the Red-Black tree is
roughly balanced.

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

Rules That Every Red-Black Tree Follows:

1. Every node has a color either red or black.


2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a red parent or red
child).
4. Every path from a node (including root) to any of its descendants NULL nodes has
the same number of black nodes.
5. Every leaf (e.i. NULL node) must be colored BLACK.

Why Red-Black Trees?

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.

Sr. No. Algorithm Time Complexity


1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)

“n” is the total number of elements in the red-black tree.


Comparison with AVL Tree:
The AVL trees are more balanced compared to Red-Black Trees, but they may cause more
rotations during insertion and deletion. So if your application involves frequent insertions and
deletions, then Red-Black trees should be preferred. And if the insertions and deletions are
less frequent and search is a more frequent operation, then AVL tree should be preferred over
the Red-Black Tree.

How does a Red-Black Tree ensure balance?

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

Proper structure of three noded Red-black tree.


Interesting points about Red-Black 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 of a Red-Black 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).

Search Operation in Red-black Tree:

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:

searchElement (tree, val)


Step 1:
If tree -> data = val OR tree = NULL
Return tree
Else
If val < data
Return searchElement (tree -> left, val)
Else
Return searchElement (tree -> right, val)
[ End of if ]
[ End of if ]

[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:

1. Start from the root.


2. Compare the inserting element with root, if less than root, then recurse for left, else
recurse for right.
3. If the element to search is found anywhere, return true, else return false.

Just follow the blue bubble.

[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.

2. Red Black Trees are self-balancing.

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.

Splay Tree Rotations

Right Zig Rotation (left version too)

Right Zig-Zig Rotation (left version too)

Right Zig-Zag Rotation (left version too)

[SIET] Page 16
Trees AVL tree, Red-black tree, Splay tree, B-tree

Splay Tree Iterator

 Even contains method changes splay tree shape


 This breaks the standard in-order iterator!
 Because the stack is based on the shape of the tree
 Solution: Remove the stack from the iterator
 Observation: Given location of current node (node whose value is next to be returned), we can
compute it’s (in-order)successor in next()
 It’s either left-most leaf of right child of current, or
 It’s closest ”left-ancestor” of current
 Ancestor whose left child is also an ancestor of current
 Also, reset must “re-find” root
 Idea: Hold a single “reference” node, use it to find root

[Type text] Page 17

You might also like