AVL Tree
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree
is named AVL in honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which each
node is associated with a balance factor which is calculated by subtracting
the height of its right sub-tree from that of its left sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right
sub-tree contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level
lower than the right sub-tree.
An AVL tree is given in the following figure. We can see that, balance factor
associated with each node is in between -1 and +1. therefore, it is an
example of AVL tree.
Complexity
SN Operation Description
AVL tree controls the height of the binary search tree by not letting it to be
skewed. The time taken for all operations in a binary search tree of height h
is O(h). However, it can be extended to O(n) if the BST becomes skewed
(i.e. worst case). By limiting this height to log n, AVL tree imposes an upper
bound on each operation to be O(log n) where n is the number of nodes.
AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -
1, 0, and 1. There are basically four types of rotations which are as follows:
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations. For a tree to be unbalanced,
minimum height must be at least 2, Let us understand each rotation
1. RR Rotation
When BST becomes unbalanced, due to a node is inserted into the right
subtree of the right subtree of A, then we perform RR rotation, RR rotation is
an anticlockwise rotation, which is applied on the edge below a node having
balance factor -2
2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left
subtree of the left subtree of C, then we perform LL rotation, LL rotation is
clockwise rotation, which is applied on the edge below a node having
balance factor 2.
In above example, node C has balance factor 2 because a node A is inserted
in the left subtree of C left subtree. We perform the LL rotation on the edge
below A.
3. LR Rotation
Double rotations are bit tougher than single rotation which has already
explained above. LR rotation = RR rotation + LL rotation, i.e., first RR
rotation is performed on subtree and then LL rotation is performed on full
tree, by full tree we mean the first node from the path of inserted node
whose balance factor is other than -1, 0, or 1.
State Action
4. RL Rotation
As already discussed, that double rotations are bit tougher than single
rotation which has already explained above. R L rotation = LL rotation + RR
rotation, i.e., first LL rotation is performed on subtree and then RR rotation is
performed on full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.
A node B has been inserted into the left subtree
of C the right subtree of A, because of which A
has become an unbalanced node having balance
factor - 2. This case is RL rotation where: Inserte
node is in the left subtree of right subtree of A
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J
5. Insert G
On inserting G, BST become unbalanced as the Balance Factor of H is 2,
since if we travel from G to H, we find that it is inserted in the left subtree of
right subtree of H, we will perform LR Rotation on node I. LR = RR + LL
rotation.
6. Insert K
On inserting the L tree is still balanced as the Balance Factor of each node is
now either, -1, 0, +1. Hence the tree is a Balanced AVL tree
B-tree
B-tree is a special type of self-balancing search tree in which each node can contain
more than one key and can have more than two children. It is a generalized form of
the binary search tree.
It is also known as a height-balanced m-way tree.
B-
tree
Other data structures such as a binary search tree, avl tree, red-black tree, etc can
store only one key in one node. If you have to store a large number of keys, then the
height of such trees becomes very large, and the access time increases.
However, B-tree can store many keys in a single node and can have multiple child
nodes. This decreases the height significantly allowing faster disk accesses.
B-tree Properties
1. For each node x, the keys are stored in increasing order.
2. In each node, there is a boolean value x.leaf which is true if x is a leaf.
3. If n is the order of the tree, each internal node can contain at most n - 1 keys along with
a pointer to each child.
4. Each node except root can have at most n children and at least n/2 children.
5. All leaves have the same depth (i.e. height-h of the tree).
logt (n+1)/2.
Operations on a B-tree
Searching an element in a B-tree
Searching for an element in a B-tree is the generalized form of searching an element in
a Binary Search Tree. The following steps are followed.
1. Starting from the root node, compare k with the first key of the node.
If k = the first key of the node, return the node and the index.
2. If k.leaf = true, return NULL (i.e. not found).
3. If k < the first key of the root node, search the left child of this key recursively.
4. If there is more than one key in the current node and k > the first key, compare k with
the next key in the node.
If k < next key, search the left child of this key (ie. k lies in between the first and the
second keys).
Else, search the right child of the key.
5. Repeat steps 1 to 4 until the leaf is reached.
Searching Example
1. Let us search key k = 17 in the tree below of degree 3.
B-tree
2. k is not found in the root so, compare it with the root key.
6. k is found. k is found
Algorithm for Searching an Element
BtreeSearch(x, k)
i = 1
while i ≤ n[x] and k ≥ keyi[x] // n[x] means number of keys in x node
do i = i + 1
if i n[x] and k = keyi[x]
then return (x, i)
if leaf [x]
then return NIL
else
return BtreeSearch(ci[x], k)
Insertion on B-tree
Deletion on B-tree
B Tree Applications
databases and file systems
to store blocks of data (secondary storage media)
multilevel indexing
Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More
formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted
by G(V, E).
Imagine a game of football as a web of connections, where players are the nodes and their interactions
on the field are the edges. This web of connections is exactly what a graph data structure represents,
and it’s the key to unlocking insights into team performance and player dynamics in sports.
Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertex
or nodes. Every node/vertex can be labeled or unlabelled.
Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of nodes in a
directed graph. Edges can connect any two nodes in any possible way. There are no rules. Sometimes,
edges are also known as arcs. Every edge can be labelled/unlabelled.
1. Null Graph
2. Trivial Graph
Graph having only a single vertex, it is also the smallest graph
possible.
3. Undirected Graph
A graph in which edges do not have any direction. That is the nodes are unordered pairs in the definition
of every edge.
4. Directed Graph
A graph in which edge has direction. That is the nodes are ordered pairs in the definition of every edge.
5. Connected Graph
The graph in which from one node we can visit any other node in the graph is known as a connected
graph.
6. Disconnected Graph
The graph in which at least one node is not reachable from a node is known as a disconnected graph.
7. Regular Graph
The graph in which the degree of every vertex is equal to K is called K regular graph.
8. Complete Graph
The graph in which from each node there is an edge to each other node.
9. Cycle Graph
The graph in which the graph is a cycle in itself, the minimum value of degree of each vertex is 2.
A graph in which vertex can be divided into two sets such that vertex in each set does not contain any
edge between them.
13. Weighted Graph
A graph in which the edges are already specified with suitable weight is known as a weighted graph.
Weighted graphs can be further classified as directed weighted graphs and undirected weighted
graphs.
There are multiple ways to store a graph: The following are the most common representations.
Adjacency Matrix
Adjacency List
In this method, the graph is stored in the form of the 2D matrix where rows and columns denote
vertices. Each entry in the matrix represents the weight of the edge between those vertices.
Below is the implementation of Graph Data Structure represented using Adjacency Matrix:
#include<stdio.h>
#define V 4
void addEdge(int mat[V][V], int i, int j) {
mat[i][j] = 1;
printf("\n");
int main() {
addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);
int mat[V][V] = {
{0, 1, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 1},
{0, 0, 1, 0}
}; */
displayMatrix(mat);
return 0;
Output
0110
1010
1101
0010
This graph is represented as a collection of linked lists. There is an array of pointer which points to the
edges connected to that vertex.
Below is the implementation of Graph Data Structure represented using Adjacency List:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
newNode->next = adj[i];
adj[i] = newNode;
newNode->next = adj[j];
adj[j] = newNode;
temp = temp->next;
printf("\n");
}
}
// Main function
int main() {
int V = 4;
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);
displayAdjList(adj, V);
return 0;
Output
1: 0 2
2: 0 1 3
3: 2
When the graph contains a large number of edges then it is good to store it as a matrix because only
some entries in the matrix will be empty. An algorithm such as Prim’s and Dijkstra adjacency matrix is
used to have less complexity.
Adjacency
Action Matrix Adjacency List
Traversal of Graph Data Structure- Traversing all the nodes in the graph.
Graph Data Structure has numerous real-life applications across various fields. Some of them are listed
below:
If we recall all the previous data structures that we have studied like array, linked list, tree, etc. All these
had some restrictions on structure (mostly linear and tree hierarchical which means no loops). Graph
allows random connections between nodes which is useful in many real world problems where do have
restrictions of previous data structures.
Used heavily in social networks. Everyone on the network is a vertex (or node) of the graph and if
connected, then there is an edge. Now imagine all the features that you see, mutual friends, people that
follow you, etc can seen as graph problems.
Used to represent the topology of computer networks, such as the connections between routers and
switches.
Used to represent the connections between different places in a transportation network, such as roads
and airports.
Neural Networks: Vertices represent neurons and edges represent the synapses between them. Neural
networks are used to understand how our brain works and how connections change when we learn. The
human brain has about 10^11 neurons and close to 10^15 synapses.
Compilers: Graph Data Structure is used extensively in compilers. They can be used for type inference,
for so-called data flow analysis, register allocation, and many other purposes. They are also used in
specialized compilers, such as query optimization in database languages.
Robot planning: Vertices represent states the robot can be in and the edges the possible transitions
between the states. Such graph plans are used, for example, in planning paths for autonomous vehicles.
Dependencies in a software project (or any other type of project) can be seen as graph and generating a
sequence to solve all tasks before dependents is a standard graph topological sorting algorithm.
For optimizing the cost of connecting all locations of a network. For example, minimizing wire length in a
wired network to make sure all devices are connected is a standard Graph problem called Minimum
Spanning Tree.
Graph Data Structure used to represent a wide range of relationships as we do not have any restrictions
like previous data structures (Tree cannot have loops and have to be hierarchical. Arrays, Linked List, etc
are linear)
They can be used to model and solve a wide range of problems, including pathfinding, data clustering,
network analysis, and machine learning.
Any real world problem where we certain set of items and relations between them can be easily
modeled as a graph and a lot of standard graph algorithms like BFS, DFS, Spanning Tree, Shortest Path,
Topological Sorting and Strongly Connected
Graph Data Structure can be used to represent complex data structures in a simple and intuitive way,
making them easier to understand and analyze.
Creating and manipulating graphs can be computationally expensive, especially for very large or
complex graphs.
Graph algorithms can be difficult to design and implement correctly, and can be prone to bugs and
errors.
Graph Data Structure can be difficult to visualize and analyze, especially for very large or complex
graphs, which can make it challenging to extract meaningful insights from the data.
Introduction to Graph
Visit Course
1. What is a graph?
A graph is a data structure consisting of a set of vertices (nodes) and a set of edges that connect pairs of
vertices.
Graph Data Structure can be classified into various types based on properties such as directionality of
edges (directed or undirected), presence of cycles (acyclic or cyclic), and whether multiple edges
between the same pair of vertices are allowed (simple or multigraph).
Graph Data Structure has numerous applications in various fields, including social networks,
transportation networks, computer networks, recommendation systems, biology, chemistry, and more.
In an undirected graph, edges have no direction, meaning they represent symmetric relationships
between vertices. In a directed graph (or digraph), edges have a direction, indicating a one-way
relationship between vertices.
A weighted graph is a graph in which each edge is assigned a numerical weight or cost. These weights
can represent distances, costs, or any other quantitative measure associated with the edges.
A path in a graph is a sequence of vertices connected by edges. The length of a path is the number of
edges it contains.
A cycle in a graph is a path that starts and ends at the same vertex, traversing a sequence of distinct
vertices and edges in between.
A spanning tree of a graph is a subgraph that is a tree and includes all the vertices of the original graph.
A minimum spanning tree (MST) is a spanning tree with the minimum possible sum of edge weights.
10. What algorithms are commonly used to traverse or search Graph Data Structure?
Common graph traversal algorithms include depth-first search (DFS) and breadth-first search (BFS).
These algorithms are used to explore or visit all vertices in a graph, typically starting from a specified
vertex. Other algorithms, such as Dijkstra’s algorithm and Bellman-Ford algorithm, are used for shortest
path finding.