0% found this document useful (0 votes)
2 views

AVL Tree

An AVL Tree is a height-balanced binary search tree where each node has a balance factor between -1 and 1, ensuring efficient operations such as search, insert, and delete with a time complexity of O(log n). It employs rotations to maintain balance during insertions and deletions, with four types of rotations: LL, RR, LR, and RL. Additionally, the document introduces B-trees, which are self-balancing trees that can store multiple keys per node, optimizing disk access times, and discusses graph data structures used in various applications.

Uploaded by

vikasdhiman85698
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AVL Tree

An AVL Tree is a height-balanced binary search tree where each node has a balance factor between -1 and 1, ensuring efficient operations such as search, insert, and delete with a time complexity of O(log n). It employs rotations to maintain balance during insertions and deletions, with four types of rotations: LL, RR, LR, and RL. Additionally, the document introduces B-trees, which are self-balancing trees that can store multiple keys per node, optimizing disk access times, and discusses graph data structures used in various applications.

Uploaded by

vikasdhiman85698
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

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.

Tree is said to be balanced if balance factor of each node is in between -1 to


1, otherwise, the tree will be unbalanced and need to be balanced.

Balance Factor (k) = height (left(k)) - height


(right(k))
If balance factor of any node is 1, it means that the left sub-tree is one level
higher than the right 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

Algorithm Average case 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)


Operations on AVL tree
Due to the fact that, AVL tree is also a binary search tree therefore, all the
operations are performed in the same way as they are performed in a binary
search tree. Searching and traversing do not lead to the violation in property
of AVL tree. However, insertion and deletion are the operations which can
violate this property and therefore, they need to be revisited.

SN Operation Description

1 Insertion Insertion in AVL tree is


performed in the same way as
is performed in a binary search
tree. However, it may lead to
violation in the AVL tree
property and therefore the tree
may need balancing. The tree
can be balanced by applying
rotations.

2 Deletion Deletion can also be performed


in the same way as it is
performed in a binary search
tree. Deletion may also disturb
the balance of the tree
therefore, various types of
rotations are used to rebalance
the tree.

Why AVL Tree?

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:

1. L L rotation: Inserted node is in the left subtree of left subtree of A


2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A

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

In above example, node A has balance factor -2 because a node C is inserted


in the right subtree of A right subtree. We perform the RR rotation on the
edge below A.

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.

Let us understand each and every step very clearly:

State Action

A node B has been inserted into the right subtree


of A the left subtree of C, because of which C has
become an unbalanced node having balance
factor 2. This case is L R rotation where: Inserted
node is in the right subtree of left subtree of C
As LR rotation = RR + LL rotation, hence RR
(anticlockwise) on subtree rooted at A is
performed first. By doing RR rotation, node A, ha
become the left subtree of B.

After performing RR rotation, node C is still


unbalanced, i.e., having balance factor 2, as
inserted node A is in the left of left of C

Now we perform LL clockwise rotation on full tree


i.e. on node C. node C has now become the right
subtree of node B, A is left subtree of B

Balance factor of each node is now either -1, 0, o


1, i.e. BST is balanced now.

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

As RL rotation = LL rotation + RR rotation, hence


LL (clockwise) on subtree rooted at C is
performed first. By doing RR rotation, node C has
become the right subtree of B.

After performing LL rotation, node A is still


unbalanced, i.e. having balance factor -2, which
because of the right-subtree of the right-subtree
node A.

Now we perform RR rotation (anticlockwise


rotation) on full tree, i.e. on node A. node C has
now become the right subtree of node B, and
node A has become the left subtree of B.
Balance factor of each node is now either -1, 0, o
1, i.e., BST is balanced now.

Q: Construct an AVL tree having the following elements

H, I, J, B, A, E, C, F, D, G, K, L

1. Insert H, I, J

On inserting the above elements, especially in the case of H, the BST


becomes unbalanced as the Balance Factor of H is -2. Since the BST is right-
skewed, we will perform RR Rotation on node H.

The resultant balance tree is:


2. Insert B, A

On inserting the above elements, especially in case of A, the BST becomes


unbalanced as the Balance Factor of H and I is 2, we consider the first node
from the last inserted node i.e. H. Since the BST from H is left-skewed, we
will perform LL Rotation on node H.

The resultant balance tree is:


3. Insert E

On inserting E, BST becomes unbalanced as the Balance Factor of I is 2,


since if we travel from E to I we find that it is inserted in the left subtree of
right subtree of I, we will perform LR Rotation on node I. LR = RR + LL
rotation
3 a) We first perform RR rotation on node B

The resultant tree after RR rotation is:

3b) We first perform LL rotation on the node I

The resultant balanced tree after LL rotation is:


4. Insert C, F, D
On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B
and H is -2, since if we travel from D to B we find that it is inserted in the
right subtree of left subtree of B, we will perform RL Rotation on node I. RL =
LL + RR rotation.

4a) We first perform LL rotation on node E

The resultant tree after LL rotation is:

4b) We then perform RR rotation on node B

The resultant balanced tree after RR rotation is:

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.

5 a) We first perform RR rotation on node C

The resultant tree after RR rotation is:

5 b) We then perform LL rotation on node H


The resultant balanced tree after LL rotation is:

6. Insert K

On inserting K, BST becomes unbalanced as the Balance Factor of I is -2.


Since the BST is right-skewed from I to K, hence we will perform RR Rotation
on the node I.

The resultant balanced tree after RR rotation is:


7. Insert L

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

Why do you need a B-tree data structure?


The need for B-tree arose with the rise in the need for lesser time in accessing physical
storage media like a hard disk. The secondary storage devices are slower with a larger
capacity. There was a need for such types of data structures that minimize the disk
access.

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

6. The root has at least 2 children and contains a minimum of 1 key.

7. If n ≥ 1, then for any n-key B-tree of height h and minimum degree t ≥ 2, h ≥

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.

k is not found on the root node


3. Since k > 11, go to the right child of the root node.

Go to the right subtree


4. Compare k with 16. Since k > 16, compare k with the next key 18.

Compare with the keys from left to


right
5. Since k < 18, k lies between 16 and 18. Search in the right child of 16 or the left child of

18. k lies in between 16 and 18

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

Searching Complexity on B Tree


Worst case Time complexity: Θ(log n)

Average case Time complexity: Θ(log n)

Best case Time complexity: Θ(log n)

Average case Space complexity: Θ(n)


Worst case Space complexity: Θ(n)

B Tree Applications
 databases and file systems
 to store blocks of data (secondary storage media)

 multilevel indexing

Introduction to Graph Data Structure


Graph Data Structure is a non-linear data structure consisting of vertices and edges. It is useful in fields
such as social network analysis, recommendation systems, and computer networks. In the field of sports
data science, graph data structure can be used to analyze and understand the dynamics of team
performance and player interactions on the field.

What is Graph Data Structure?

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.

Components of Graph Data Structure

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.

Types Of Graphs in Data Structure and Algorithms

1. Null Graph

A graph is known as a null graph if there are no edges in the 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.

10. Cyclic Graph

A graph containing at least one cycle is known as a Cyclic graph.


11. Directed Acyclic Graph

A Directed Graph that does not contain any cycle.

12. Bipartite Graph

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.

Representation of Graph Data Structure:

There are multiple ways to store a graph: The following are the most common representations.

Adjacency Matrix

Adjacency List

Adjacency Matrix Representation of Graph Data Structure:

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;

mat[j][i] = 1; // Since the graph is undirected

void displayMatrix(int mat[V][V]) {

for (int i = 0; i < V; i++) {

for (int j = 0; j < V; j++)

printf("%d ", mat[i][j]);

printf("\n");

int main() {

// Create a graph with 4 vertices and no edges

// Note that all values are initialized as 0

int mat[V][V] = {0};

// Now add edges one by one

addEdge(mat, 0, 1);

addEdge(mat, 0, 2);

addEdge(mat, 1, 2);

addEdge(mat, 2, 3);

/* Alternatively, we can also create using the below


code if we know all edges in advance

int mat[V][V] = {

{0, 1, 0, 0},

{1, 0, 1, 0},

{0, 1, 0, 1},

{0, 0, 1, 0}

}; */

printf("Adjacency Matrix Representation\n");

displayMatrix(mat);

return 0;

Output

Adjacency Matrix Representation

0110

1010

1101

0010

Adjacency List Representation of Graph:

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>

// Structure for a node in the adjacency list

struct Node {

int data;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode =

(struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;
return newNode;

// Function to add an edge between two vertices

void addEdge(struct Node* adj[], int i, int j) {

struct Node* newNode = createNode(j);

newNode->next = adj[i];

adj[i] = newNode;

newNode = createNode(i); // For undirected graph

newNode->next = adj[j];

adj[j] = newNode;

// Function to display the adjacency list

void displayAdjList(struct Node* adj[], int V) {

for (int i = 0; i < V; i++) {

printf("%d: ", i); // Print the vertex

struct Node* temp = adj[i];

while (temp != NULL) {

printf("%d ", temp->data); // Print its adjacent

temp = temp->next;

printf("\n");

}
}

// Main function

int main() {

// Create a graph with 4 vertices and no edges

int V = 4;

struct Node* adj[V];

for (int i = 0; i < V; i++) {

adj[i] = NULL; // Initialize adjacency list

// Now add edges one by one

addEdge(adj, 0, 1);

addEdge(adj, 0, 2);

addEdge(adj, 1, 2);

addEdge(adj, 2, 3);

printf("Adjacency List Representation:\n");

displayAdjList(adj, V);

return 0;

Output

Adjacency List Representation:


0: 1 2

1: 0 2

2: 0 1 3

3: 2

Comparison between Adjacency Matrix and Adjacency List

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

Adding Edge O(1) O(1)

Removing an edge O(1) O(N)

Initializing O(N*N) O(N)

Basic Operations on Graph Data Structure:

Below are the basic operations on the graph:

Insertion or Deletion of Nodes in the graph

Add and Remove vertex in Adjacency List representation of Graph

Add and Remove vertex in Adjacency Matrix representation of Graph

Insertion or Deletion of Edges in the graph

Add and Remove Edge in Adjacency List representation of a Graph

Add and Remove Edge in Adjacency Matrix representation of a Graph

Searching in Graph Data Structure- Search an entity in the graph.

Traversal of Graph Data Structure- Traversing all the nodes in the graph.

Difference between Tree and Graph:


Tree is a restricted type of Graph Data Structure, just with some more rules. Every tree will always be a
graph but not all graphs will be trees. Linked List, Trees, and Heaps all are special cases of graphs.

Real-Life Applications of Graph Data Structure:

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.

Advantages of Graph Data Structure:

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.

Disadvantages of Graph Data Structure:


Graph Data Structure can be complex and difficult to understand, especially for people who are not
familiar with graph theory or related algorithms.

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

Frequently Asked Questions(FAQs) on Graph Data Structure:

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.

2. What are the different types of Graph Data Structure?

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

3. What are the applications of Graph Data Structure?

Graph Data Structure has numerous applications in various fields, including social networks,
transportation networks, computer networks, recommendation systems, biology, chemistry, and more.

4. What is the difference between a directed graph and an undirected graph?

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.

5. What is a weighted graph?

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.

6. What is the degree of a vertex in a graph?


The degree of a vertex in a graph is the number of edges incident to that vertex. In a directed graph, the
indegree of a vertex is the number of incoming edges, and the outdegree is the number of outgoing
edges.

7. What is a path in a graph?

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.

8. What is a cycle in a graph?

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.

9. What are spanning trees and minimum spanning trees?

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.

You might also like