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

Data Structure Solution

Sem 3 computer Engineering Mumbai University
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Data Structure Solution

Sem 3 computer Engineering Mumbai University
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Data Structure 3rd Sem

Graph
Introduction:
A 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).

Components of a Graph
• 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 Graph
➢ Null Graph
➢ Trivial Graph
➢ Undirected Graph
➢ Directed Graph
➢ Connected Graph
➢ Disconnected Graph
➢ Regular Graph
➢ Complete Graph
➢ Cycle Graph
➢ Cyclic Graph
➢ Directed Acyclic Graph
➢ Weighted Graph
➢ Bipartite Graph

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 1


Data Structure 3rd Sem

➢ Null Graph: A graph is known as a null graph if there are no edges in the graph.
➢ Trivial Graph: Graph having only a single vertex, it is also the smallest graph possible

➢ 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.
➢ Directed Graph: A graph in which edge has direction. That is the nodes are ordered pairs in the
definition of every edge.

➢ Connected Graph: The graph in which from one node we can visit any other node in the graph is
known as a connected graph.
➢ Disconnected Graph: The graph in which at least one node is not reachable from a node is known
as a disconnected graph

➢ Regular Graph: The graph in which the degree of every vertex is equal to K is called K regular
graph.
➢ Complete Graph: The graph in which from each node there is an edge to each other node.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 2


Data Structure 3rd Sem

➢ Cycle Graph: The graph in which the graph is a cycle in itself, the degree of each vertex is 2.
➢ Cyclic Graph: A graph containing at least one cycle is known as a Cyclic graph.

➢ Directed Acyclic Graph: A Directed Graph that does not contain any cycle.
➢ 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.

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

Representations of Graph
Here are the two most common ways to represent a graph :
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
An adjacency matrix is a way of representing a graph as a matrix of boolean (0’s and 1’s). Let’s
assume there are n vertices in the graph So, create a 2D matrix adjMat[n][n] having dimension n x n.
• If there is an edge from vertex i to j, mark adjMat[i][j] as 1.
• If there is no edge from vertex i to j, mark adjMat[i][j] as 0.

Representation of Undirected Graph to Adjacency Matrix:The below figure shows an undirected


graph. Initially, the entire Matrix is initialized to 0. If there is an edge from source to destination, we
insert 1 to both cases (adjMat[destination] and adjMat[destination]) because we can go either way.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 3


Data Structure 3rd Sem

Representation of Directed Graph to Adjacency Matrix:


The below figure shows a directed graph. Initially, the entire Matrix is initialized to 0. If there is an
edge from source to destination, we insert 1 for that particular adjMat[destination].

Adjacency List:
An array of Lists is used to store edges between two vertices. The size of array is equal to the
number of vertices (i.e, n). Each index in this array represents a specific vertex in the graph. The entry at
the index i of the array contains a linked list containing the vertices that are adjacent to vertex i.
Let’s assume there are n vertices in the graph So, create an array of list of size n as adjList[n].
• adjList[0] will have all the nodes which are connected (neighbour) to vertex 0.
• adjList[1] will have all the nodes which are connected (neighbour) to vertex 1 and so on.

Representation of Undirected Graph to Adjacency list:


The below undirected graph has 3 vertices. So, an array of list will be created of size 3, where each indices
represent the vertices. Now, vertex 0 has two neighbours (i.e, 1 and 2). So, insert vertex 1 and 2 at indices
0 of array. Similarly, For vertex 1, it has two neighbour (i.e, 2 and 1) So, insert vertices 2 and 1 at indices
1 of array. Similarly, for vertex 2, insert its neighbours in array of list.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 4


Data Structure 3rd Sem

Representation of Directed Graph to Adjacency list:


The below directed graph has 3 vertices. So, an array of list will be created of size 3, where each
indices represent the vertices. Now, vertex 0 has no neighbours. For vertex 1, it has two neighbour (i.e, 0
and 2) So, insert vertices 0 and 2 at indices 1 of array. Similarly, for vertex 2, insert its neighbours in array
of list.

Data Structures for Representing Graphs


• Two common data structures for representing graphs:
o Adjacency lists
o Adjacency matrix
Adjacency List Representation
An adjacency list is a way in which graphs can be represented in the computer’s memory. This
structure consists of a list of all nodes in G. Furthermore, every node is in turn linked to its own list that
contains the names of all other nodes that are adjacent to it. The key advantages of using an adjacency list
are:
• It is easy to follow and clearly shows the adjacent nodes of a particular node.
• It is often used for storing graphs that have a small-to-moderate number of edges. That is, an adjacency list
is preferred for representing sparse graphs in the computer’s memory; otherwise, an adjacency matrix is a
good choice.
• Adding new nodes in G is easy and straightforward when G is represented using an adjacency list. Adding
new nodes in an adjacency matrix is a difficult task, as the size of the matrix needs to be changed and
existing nodes may have to be reordered. Each node has a list of adjacent nodes
Example (undirected graph): (fig 1)

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 5


Data Structure 3rd Sem

o Example (directed graph):


o A: B, C, D
o B: D
o C: Nil
o D: C
Adjacency Matrix Representation
An adjacency matrix is used to represent which nodes are adjacent to one another. By definition, two
nodes are said to be adjacent if there is an edge connecting them. In a directed graph G, if node v is adjacent
to node u, then there is definitely an edge from u to v. That is, if v is adjacent to u, we can get from u to v by
traversing one edge. For any graph G having n nodes, the adjacency matrix will have the dimension of n * n.
In an adjacency matrix, the rows and columns are labelled by graph vertices. An entry aij in the adjacency
matrix will contain 1, if vertices vi and vj are adjacent to each other. However, if the nodes are not adjacent,
aij will be set to zero. It. Since an adjacency matrix contains only 0s and 1s, it is called a bit matrix or a
Boolean matrix. The entries in the matrix depend on the ordering of the nodes in G. Therefore, a change in
the order of nodes will result in a different adjacency matrix.
Aij = 1 if there is an edge from Vi to Vj
0 otherwise
Adjacency Matrix: 2D array containing weights on edges
o Row for each vertex o Column for each vertex
o Entries contain weight of edge from row vertex to column vertex o Entries contain ∞ if no edge from row
vertex to column vertex
o Entries contain 0 on diagonal (if self edges not allowed)
• Example undirected graph (assume self-edges not allowed):
ABCD
A0111
B10∞1
C1∞01
D1110
Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 6
Data Structure 3rd Sem

• Example directed graph (assume self-edges allowed):


ABCD
A∞111
B∞∞∞1
C∞∞∞∞
D∞∞1∞

Disadvantage : Adjacency matrix representation is easy to represent and feasible as long as the graph is
small and connected. For a large graph, whose matrix is sparse, adjacency matrix representation wastes a lot
of memory. Hence list representation is preferred over matrix representation.

TOPOLOGICAL SORT:- Topological Sort is a linear ordering of the vertices in such a way that if there is
an edge in the DAG going from vertex ‘u’ to vertex ‘v’,then ‘u’ comes before ‘v’ in the ordering.
It is important to note that-

• Topological Sorting is possible if and only if the graph is a Directed Acyclic Graph.
• There may exist multiple different topological orderings for a given directed acyclic graph.

Applications of Topological Sort-


Few important applications of topological sort are-
• Scheduling jobs from the given dependencies among jobs
• Instruction Scheduling
• Determining the order of compilation tasks to perform in make files
• Data Serialization

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 7


Data Structure 3rd Sem

Problem-01: Find the number of different topological orderings possible for the given graph-

Solution- The topological orderings of the above graph are found in the following steps-

Step-01: Write in-degree of each vertex-

Step-02:
• Vertex-A has the least in-degree.
• So, remove vertex-A and its associated edges.
• Now, update the in-degree of other vertices.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 8


Data Structure 3rd Sem

Step-03:
• Vertex-B has the least in-degree.
• So, remove vertex-B and its associated edges.
• Now, update the in-degree of other vertices.

Step-04:
There are two vertices with the least in-degree. So, following 2 cases are possible-
In case-01,
• Remove vertex-C and its associated edges.
• Then, update the in-degree of other vertices.

In case-02,
• Remove vertex-D and its associated edges.
• Then, update the in-degree of other vertices.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 9


Data Structure 3rd Sem

Step-05:
Now, the above two cases are continued separately in the similar manner.
In case-01,
• Remove vertex-D since it has the least in-degree.
• Then, remove the remaining vertex-E.

In case-02,
• Remove vertex-C since it has the least in-degree.
• Then, remove the remaining vertex-E.

Conclusion-
For the given graph, following 2 different topological orderings are possible-
• ABCDE
• ABDCE

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 10


Data Structure 3rd Sem

Problem-02: Find the number of different topological orderings possible for the given graph-

Solution- The topological orderings of the above graph are found in the following steps-
Step-01: Write in-degree of each vertex-

Step-02:
• Vertex-1 has the least in-degree.
• So, remove vertex-1 and its associated edges.
• Now, update the in-degree of other vertices.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 11


Data Structure 3rd Sem

Step-03:
There are two vertices with the least in-degree. So, following 2 cases are possible-
In case-01,
• Remove vertex-2 and its associated edges.
• Then, update the in-degree of other vertices.

In case-02,
• Remove vertex-3 and its associated edges.
• Then, update the in-degree of other vertices.

Step-04:
Now, the above two cases are continued separately in the similar manner.
In case-01,
• Remove vertex-3 since it has the least in-degree.
• Then, update the in-degree of other vertices.
In case-02,
• Remove vertex-2 since it has the least in-degree.
• Then, update the in-degree of other vertices.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 12


Data Structure 3rd Sem

Step-05:
In case-01,
• Remove vertex-4 since it has the least in-degree.
• Then, update the in-degree of other vertices.

In case-02,
• Remove vertex-4 since it has the least in-degree.
• Then, update the in-degree of other vertices.

Step-06:
In case-01,
• There are 2 vertices with the least in-degree.
• So, 2 cases are possible.
• Any of the two vertices may be taken first.
Same is with case-02.

Conclusion- For the given graph, following 4 different topological orderings are possible-
• 123456
• 123465
• 132456
• 132465

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 13


Data Structure 3rd Sem

Searching is a process of finding a particular element among several given elements.


• The search is successful if the required element is found.
• Otherwise, the search is unsuccessful.

Searching Algorithms-
Searching Algorithms are a family of algorithms used for the purpose of searching.
The searching of an element in the given array may be carried out in the following two ways-
1. Linear Search
2. Binary Search

Linear Search-
• Linear Search is the simplest searching algorithm.
• It traverses the array sequentially to locate the required element.
• It searches for an element by comparing it with each element of the array one by one.
• So, it is also called as Sequential Search.

Linear Search Algorithm is applied when-


• No information is given about the array.
• The given array is unsorted or the elements are unordered.
• The list of data items is smaller.
Linear Search Algorithm-
Consider-
• There is a linear array ‘a’ of size ‘n’.
• Linear search algorithm is being used to search an element ‘item’ in this linear array.
• If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.

Then, Linear Search Algorithm is as follows-

Begin
for i = 0 to (n - 1) by 1 do
if (a[i] = item) then
set loc = i
Exit
endif
endfor
set loc = -1
End

Linear Search Example- Consider-


• We are given the following linear array.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 14


Data Structure 3rd Sem

• Element 15 has to be searched in it using Linear Search Algorithm.

Now,
• Linear Search algorithm compares element 15 with all the elements of the array one by one.
• It continues searching until either the element 15 is found or all the elements are searched.

Linear Search Algorithm works in the following steps-

Step-01:
• It compares element 15 with the 1st element 92.
• Since 15 ≠ 92, so required element is not found.
• So, it moves to the next element.

Step-02:
• It compares element 15 with the 2nd element 87.
• Since 15 ≠ 87, so required element is not found.
• So, it moves to the next element.

Step-03:
• It compares element 15 with the 3rd element 53.
• Since 15 ≠ 53, so required element is not found.
• So, it moves to the next element.

Step-04:
• It compares element 15 with the 4th element 10.
• Since 15 ≠ 10, so required element is not found.
• So, it moves to the next element.

Step-05:
• It compares element 15 with the 5th element 15.
• Since 15 = 15, so required element is found.
• Now, it stops the comparison and returns index 4 at which element 15 is present.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 15


Data Structure 3rd Sem

// C program to implement linear search


#include <stdio.h>
int linearSearch(int* arr, int size, int key) // linear search function that searches the key in arr
{ for (int i = 0; i < size; i++) // starting traversal
{ if (arr[i] == key) // checking condition
{ return i; }
}
return -1;
}
int main()
{ int arr[10] = { 3, 4, 1, 7, 5, 8, 11, 42, 3, 13 };
int size = sizeof(arr) / sizeof(arr[0]);
int key = 4; // calling linearSearch
int index = linearSearch(arr, size, key); // printing result based on value returned by
if (index == -1) // linearSearch()
{ printf("The element is not present in the arr.");
}
else { printf("The element is present at arr[%d].", index); }
return 0;
}

Binary Search:
Searching is the process of finding some particular element in the list. If the element is present in the
list, then the process is called successful, and the process returns the location of that element. Otherwise,
the search is called unsuccessful.

Binary search is the search technique that works efficiently on sorted lists. Hence, to search an
element into some list using the binary search technique, we must ensure that the list is sorted.

Binary search follows the divide and conquer approach in which the list is divided into two halves,
and the item is compared with the middle element of the list. If the match is found then, the
location of the middle element is returned. Otherwise, we search into either of the halves
depending upon the result produced through the match.

Binary search can be implemented on sorted array elements. If the list elements are not arranged in a
sorted manner, we have first to sort them.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 16


Data Structure 3rd Sem

Algorithm
Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the given array, 'lower_bound' is the in
dex of the first array element, 'upper_bound' is the index of the last array element, 'val' is the val
ue to search
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 17


Data Structure 3rd Sem

Now, the element to search is found. So algorithm will return the index of the element matched.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 18


Data Structure 3rd Sem

// C program for Binary search

#include <stdio.h>
int binarySearch(int a[], int beg, int end, int val)
{ int mid;
if(end >= beg)
{ mid = (beg + end)/2;
if(a[mid] == val) /* if the item to be searched is present at middle */
{ return mid+1; }
/* if the item to be searched is smaller than middle, then it can only be in left subarray */
else if(a[mid] < val)
{ return binarySearch(a, mid+1, end, val); }
/* if the item to be searched is greater than middle, then it can only be in right subarray */
else
{ return binarySearch(a, beg, mid-1, val); }
}
return -1;
}
int main() {
int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70}; // given array
int val = 40; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = binarySearch(a, 0, n-1, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0;
}

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 19


Data Structure 3rd Sem

Breadth-first search algorithm:


Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the
neighbouring nodes. Then for each of those nearest nodes, the algorithm explores their unexplored
neighbour nodes, and so on, until it finds the goal. That is, we start examining the node A and then all the
neighbours of A are examined. In the next step, we examine the neighbours of neighbours of A, so on and so
forth. This means that we need to track the neighbours of the node and guarantee that every node in the
graph is processed and no node is processed more than once. This is accomplished by using a queue that will
hold the nodes that are waiting for further processing.

Algorithm for BFS traversal

• Step 1: Define a Queue of size total number of vertices in the graph.


• Step 2: Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
• Step 3: Visit all the adjacent vertices of the verex which is at front of the Queue which is not visited and
insert them into the Queue.
• Step 4: When there is no new vertex to be visit from the vertex at front of the Queue then delete that
vertex from the Queue.
• Step 5: Repeat step 3 and 4 until queue becomes empty.
• Step 6: When queue becomes Empty, then the enqueue or dequeue order gives the BFS traversal order.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 20


Data Structure 3rd Sem

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 21


Data Structure 3rd Sem

Depth-first Search Algorithm


Depth-first search begins at a starting node A which becomes the current node. Then, it examines each node
N along a path P which begins at A. That is, we process a neighbour of A, then a neighbour of neighbour of
A, and so on. During the execution of the algorithm, if we reach a path that has a node N that has already
been processed, then we backtrack to the current node. Otherwise, the unvisited (unprocessed) node becomes
the current node. The algorithm proceeds like this until we reach a dead-end (end of path P). On reaching the
deadend, we backtrack to find another path P. The algorithm terminates when backtracking leads back to the
starting node A. In this algorithm, edges that lead to a new vertex are called discovery edges and edges that
lead to an already visited vertex are called back edges. Observe that this algorithm is similar to the in-order
traversal of a binary tree. Its implementation is similar to that of the breadth-first search algorithm but here
we use a stack instead of a queue. We use the following steps to implement DFS traversal...
• Step 1: Define a Stack of size total number of vertices in the graph.
• Step 2: Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
• Step 3: Visit any one of the adjacent vertex of the verex which is at top of the stack which is not visited
and push it on to the stack.
• Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top of the stack.
• Step 5: When there is no new vertex to be visit then use back tracking and pop one vertex from the stack.
• Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7: When stack becomes Empty, then produce final spanning tree by removing unused edges from the
graph.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 22


Data Structure 3rd Sem

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 23


Data Structure 3rd Sem

//BFS & DFS graph traversal method


#include<stdio.h>
int top=-1,q[20],stack[20],front=-1,rear=-1,arr[20][20],visited[20]={0};
main()
{ int i,j,n,ch,s;
printf("Enter the Number of Vertices n:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ for(j=1;j<=n;j++)
{ printf("Enter 1 if %d has a node with %d else 0 \t",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("Enter Choice 1.BFS 2.DFS \n");
scanf("%d",&ch);
printf("Enter stating vertex n:");
scanf("%d",&s);
while(ch!=3)
{ switch(ch)
{ case 1:bfs(s,n); break;
case 2:dfs(s,n); break; }
scanf("%d",&ch);
for(i=0;i<=n;i++)
{ visited[i]=0; }
}
}
void add(int item)
{ if(rear==19)
printf("QUEUE FULL");
else
{ if(rear==-1)
{ q[++rear] = item;
front++; }
else
q[++rear]=item; }
}

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 24


Data Structure 3rd Sem

int delete()
{ int k;
if ((front>rear)||(front==-1))
return (0);
else
{ k=q[front++];
return(k); }
}
void push( int item )
{ if ( top == 19 )
printf( "Stack overflow " );
else
stack[ ++top ] = item;
}
int pop()
{ int k;
if ( top == -1 )
return ( 0 );
else
{ k = stack[ top-- ];
return ( k );
}
}
bfs(int s,int n)
{ int i,p;
add(s);
visited[s]=1;
p=delete();
if(p!=0) printf("%d",p);
while(p!=0)
{
for(i=1;i<=n;i++)
{ if((arr[p][i]!=0)&&(visited[i]==0))
{ add(i);
visited[i]=1;
}
}
Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 25
Data Structure 3rd Sem

p=delete();
if(p!=0) printf("%d",p);
}
for(i=1;i<=n;i++)
{ if(visited[i]==0) bfs(i,n); }
}

dfs(int s,int n)
{ int k,i;
push(s);
visited[s]=1;
k=pop();
if(k!=0) printf("%d",k);
while(k!=0)
{ for(i=1;i<=n;i++)
{ if((arr[k][i]!=0)&&(visited[i]==0))
{ push(i);
visited[i]=1;
}
k=pop();
if(k!=0) printf("%d",&k);
}
}
for(i=1;i<=n;i++)
{ if(visited[i]==0) dfs(i,n); }
}

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 26


Data Structure 3rd Sem

Huffman Coding
Huffman Coding is a technique of compressing data to reduce its size without losing
any of the details. It was first developed by David Huffman.
Huffman Coding is generally useful to compress the data in which there are frequently
occurring characters.
How Huffman Coding works?
Suppose the string below is to be sent over a network.

Initial string
Each character occupies 8 bits. There are a total of 15 characters in the above string.
Thus, a total of 8 * 15 = 120 bits are required to send this string.
Using the Huffman Coding technique, we can compress the string to a smaller size.
Huffman coding first creates a tree using the frequencies of the character and then
generates code for each character. Once the data is encoded, it has to be decoded.
Decoding is done using the same tree. Huffman Coding prevents any ambiguity in the
decoding process using the concept of prefix code ie. a code associated with a
character should not be present in the prefix of any other code. The tree created above
helps in maintaining the property. Huffman coding is done with the help of the following
steps.
1. Calculate the frequency of each character in the string

Frequency of string

2. Sort the characters in increasing order of the frequency. These are stored in a
priority queue Q

Characters sorted according to the frequency


3. Make each unique character as a leaf node.
Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 27
Data Structure 3rd Sem

4. Create an empty node z . Assign the minimum frequency to the left child of z and
assign the second minimum frequency to the right child of z .
Set the value of the z as the sum of the above two minimum frequencies

Getting the sum of the least numbers


5. Remove these two minimum frequencies from Q and add the sum into the list of
frequencies (* denote the internal nodes in the figure above).
6. Insert node z into the tree.
7. Repeat steps 3 to 5 for all the characters

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 28


Data Structure 3rd Sem

For each non-leaf node, assign 0 to the left edge and 1 to the right edge.

Assign 0 to the left edge and 1 to the right edge

For sending the above string over a network, we have to send the tree as well as the
above compressed-code. The total size is given by the table below.

Character Frequency Code Size

A 5 11 5*2=10

B 1 100 1*3=3

C 6 0 6*1=6

D 3 101 3*3=9

4*8=32 bits 15 28 bits

Without encoding, the total size of the string was 120 bits. After encoding the size is
reduced to 32 + 15 + 28 = 75 .

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 29


Data Structure 3rd Sem

Tree:

Tree is a non-linear data structure which organizes data in a hierarchical structure and this is a
recursive definition OR A tree is a connected graph without any circuits OR If in a graph, there is one and
only one path between every pair of vertices, then graph is called as a tree.
Properties- The important properties of tree data structure are-
• There is one and only one path between every pair of vertices in a tree.
• A tree with n vertices has exactly (n-1) edges.
• A graph is a tree if and only if it is minimally connected.
• Any connected graph with n vertices and (n-1) edges is a tree.
Tree Terminology- The important terms related to tree data structure are-

1. Root-
• The first node from where the tree originates is called as a root node.
• In any tree, there must be only one root node.
• We can never have multiple root nodes in a tree data structure.

2. Edge-
• The connecting link between any two nodes is called as an edge.
• In a tree with n number of nodes, there are exactly (n-1) number of edges.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 30


Data Structure 3rd Sem

3. Parent-
• The node which has a branch from it to any other node is called as a parent node.
• In other words, the node which has one or more children is called as a parent node.
• In a tree, a parent node can have any number of child nodes.

Here,
• Node A is the parent of nodes B and C
• Node B is the parent of nodes D, E and F
• Node C is the parent of nodes G and H
• Node E is the parent of nodes I and J
• Node G is the parent of node K
4. Child-
• The node which is a descendant of some node is called as a child node.
• All the nodes except root node are child nodes.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 31


Data Structure 3rd Sem

Here,
• Nodes B and C are the children of node A
• Nodes D, E and F are the children of node B
• Nodes G and H are the children of node C
• Nodes I and J are the children of node E
• Node K is the child of node G
5. Siblings-
• Nodes which belong to the same parent are called as siblings.
• In other words, nodes with the same parent are sibling nodes.

Here,
• Nodes B and C are siblings
• Nodes D, E and F are siblings
• Nodes G and H are siblings
• Nodes I and J are siblings
6. Degree-
• Degree of a node is the total number of children of that node.
• Degree of a tree is the highest degree of a node among all the nodes in the tree.

Here, Degree of node A = 2


• Degree of node B = 3
Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 32
Data Structure 3rd Sem

• Degree of node C = 2
• Degree of node D = 0
• Degree of node E = 2
• Degree of node F = 0
• Degree of node G = 1
• Degree of node H = 0
• Degree of node I = 0
• Degree of node J = 0
• Degree of node K = 0
7. Internal Node-
• The node which has at least one child is called as an internal node.
• Internal nodes are also called as non-terminal nodes.
• Every non-leaf node is an internal node.

8. Leaf Node-
• The node which does not have any child is called as a leaf node.
• Leaf nodes are also called as external nodes or terminal nodes.

9. Level-
• In a tree, each step from top to bottom is called as level of a tree.
• The level count starts with 0 and increments by 1 at each level or step.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 33


Data Structure 3rd Sem

10. Height-
• Total number of edges that lies on the longest path from any leaf node to a particular node is called
as height of that node.
• Height of a tree is the height of root node.
• Height of all leaf nodes = 0

Here, Height of node A = 3


• Height of node B = 2
• Height of node C = 2
• Height of node D = 0
• Height of node E = 1
• Height of node F = 0
• Height of node G = 1
• Height of node H = 0
• Height of node I = 0
• Height of node J = 0
• Height of node K = 0
11. Depth-
• Total number of edges from root node to a particular node is called as depth of that node.
• Depth of a tree is the total number of edges from root node to a leaf node in the longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used interchangeably.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 34


Data Structure 3rd Sem

Here,
• Depth of node A = 0
• Depth of node B = 1
• Depth of node C = 1
• Depth of node D = 2
• Depth of node E = 2
• Depth of node F = 2
• Depth of node G = 2
• Depth of node H = 2
• Depth of node I = 3
• Depth of node J = 3
• Depth of node K = 3
12. Subtree-
• In a tree, each child from a node forms a subtree recursively.
• Every child node forms a subtree on its parent node.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 35


Data Structure 3rd Sem

Binary Tree- Binary tree is a special tree data structure in which each node can have at most 2
children.Thus, in a binary tree,
Each node has either 0 child or 1 child or 2 children.

Types of Binary Trees- Binary trees can be of the following types-

1. Rooted Binary Tree


2. Full / Strictly Binary Tree
3. Complete / Perfect Binary Tree
4. Almost Complete Binary Tree
5. Skewed Binary Tree
1. Rooted Binary Tree- A rooted binary tree is a binary tree that satisfies the following 2 properties-
• It has a root node.
• Each node has at most 2 children.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 36


Data Structure 3rd Sem

2. Full / Strictly Binary Tree-


• A binary tree in which every node has either 0 or 2 children is called as a Full binary tree.
• Full binary tree is also called as Strictly binary tree.

Here,
• First binary tree is not a full binary tree.
• This is because node C has only 1 child.
3. Complete / Perfect Binary Tree- A complete binary tree is a binary tree that satisfies the following 2
properties-
• Every internal node has exactly 2 children.
• All the leaf nodes are at the same level.
Complete binary tree is also called as Perfect binary tree.

Here,
• First binary tree is not a complete binary tree.
• This is because all the leaf nodes are not at the same level.
4. Almost Complete Binary Tree- An almost complete binary tree is a binary tree that satisfies the
following 2 properties-
• All the levels are completely filled except possibly the last level.
• The last level must be strictly filled from left to right.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 37


Data Structure 3rd Sem

Here,
• First binary tree is not an almost complete binary tree.
• This is because the last level is not filled from left to right.
5. Skewed Binary Tree- A skewed binary tree is a binary tree that satisfies the following 2 properties-
• All the nodes except one node has one and only one child.
• The remaining node has no child OR A skewed binary tree is a binary tree of n nodes such that its
depth is (n-1).

Tree Traversal- Tree Traversal refers to the process of visiting each node in a tree data structure exactly
once.

Depth First Traversal- Following three traversal techniques fall under Depth First Traversal-
1.Preorder Traversal, 2. Inorder Traversal 3.Postorder Traversal
1. Preorder Traversal- Algorithm-
1. Visit the root
2. Traverse the left sub tree i.e. call Preorder (left sub tree)
3. Traverse the right sub tree i.e. call Preorder (right sub tree)
Root → Left → Right
Example- Consider the following example-

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 38


Data Structure 3rd Sem

Preorder Traversal Shortcut: Traverse the entire tree starting from the root node keeping yourself to the
left.

Applications-
• Preorder traversal is used to get prefix expression of an expression tree.
• Preorder traversal is used to create a copy of the tree.
2. Inorder Traversal-Algorithm-
1. Traverse the left sub tree i.e. call Inorder (left sub tree)
2. Visit the root
3. Traverse the right sub tree i.e. call Inorder (right sub tree)
Left → Root → Right
Example-

Inorder Traversal Shortcut: Keep a plane mirror horizontally at the bottom of the tree and take the
projection of all the nodes

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 39


Data Structure 3rd Sem

Application-
• Inorder traversal is used to get infix expression of an expression tree.
3. Postorder Traversal- Algorithm-
1. Traverse the left sub tree i.e. call Postorder (left sub tree)
2. Traverse the right sub tree i.e. call Postorder (right sub tree)
3. Visit the root
Left → Right → Root
Example-

Postorder Traversal Shortcut: Pluck all the leftmost leaf nodes one by one.

Applications-
• Postorder traversal is used to get postfix expression of an expression tree.
• Postorder traversal is used to delete the tree.
• This is because it deletes the children first and then it deletes the parent.
Breadth First Traversal-
• Breadth First Traversal of a tree prints all the nodes of a tree level by level.
• Breadth First Traversal is also called as Level Order Traversal.
Example-

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 40


Data Structure 3rd Sem

Binary Search Tree-


Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order.
In a binary search tree (BST), each node contains-
• Only smaller values in its left sub tree
• Only larger values in its right sub tree

Binary Search Tree Construction-


Let us understand the construction of a binary search tree using the following example
Example- Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
When elements are given in a sequence,
• Always consider the first element as the root node.
• Consider the given elements and insert them in the BST one by one.
The binary search tree will be constructed as explained below-
Insert 50-

Insert 70- As 70 > 50, so insert 70 to the right of 50.

Insert 60- As 60 > 50, so insert 60 to the right of 50.


• As 60 < 70, so insert 60 to the left of 70.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 41


Data Structure 3rd Sem

Insert 20- As 20 < 50, so insert 20 to the left of 50.

Insert 90- As 90 > 50, so insert 90 to the right of 50.


• As 90 > 70, so insert 90 to the right of 70.

Insert 10- As 10 < 50, so insert 10 to the left of 50.


• As 10 < 20, so insert 10 to the left of 20.

Insert 40- As 40 < 50, so insert 40 to the left of 50.


• As 40 > 20, so insert 40 to the right of 20.

Insert 100-
• As 100 > 50, so insert 100 to the right of 50.
• As 100 > 70, so insert 100 to the right of 70.
• As 100 > 90, so insert 100 to the right of 90.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 42


Data Structure 3rd Sem

Binary Search Tree Operations- Commonly performed operations on binary search tree are-
1. Search Operation
2. Insertion Operation
3. Deletion Operation
1. Search Operation- Search Operation is performed to search a particular element in the Binary Search
Tree.
Rules- For searching a given key in the BST,
• Compare the key with the value of root node.
• If the key is present at the root node, then return the root node.
• If the key is greater than the root node value, then recur for the root node’s right subtree.
• If the key is smaller than the root node value, then recur for the root node’s left subtree.
Example- Consider key = 45 has to be searched in the given BST-

• We start our search from the root node 25.


• As 45 > 25, so we search in 25’s right subtree.
• As 45 < 50, so we search in 50’s left subtree.
• As 45 > 35, so we search in 35’s right subtree.
• As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees.
• So, we conclude that 45 is not present in the above BST.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 43


Data Structure 3rd Sem

2. Insertion Operation-Insertion Operation is performed to insert an element in the Binary Search Tree
Rules- The insertion of a new key always takes place as the child of some leaf node.
For finding out the suitable leaf node,
• Search the key to be inserted from the root node till some leaf node is reached.
• Once a leaf node is reached, insert the key as child of that leaf node.
Example- Consider the following example where key = 40 is inserted in the given BST-

• We start searching for value 40 from the root node 100.


• As 40 < 100, so we search in 100’s left subtree.
• As 40 > 20, so we search in 20’s right subtree.
• As 40 > 30, so we add 40 to 30’s right subtree.
3. Deletion Operation-Deletion Operation is performed to delete a particular element from the Binary
Search Tree. when it comes to deleting a node from the binary search tree, following three cases are
possible-

Case-01: Deletion Of A Node Having No Child (Leaf Node)-

Just remove / disconnect the leaf node that is to deleted from the tree.
Example-Consider the following example where node with value = 20 is deleted from the BST-

Case-02: Deletion Of A Node Having Only One Child-


Just make the child of the deleting node, the child of its grandparent.
Example-Consider the following example where node with value = 30 is deleted from the BST-

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 44


Data Structure 3rd Sem

Case-02: Deletion Of A Node Having Two Children-


A node with two children may be deleted from the BST in the following two ways-
Method-01:
• Visit to the right subtree of the deleting node.
• Pluck the least value element called as inorder successor.
• Replace the deleting element with its inorder successor.
Example-Consider the following example where node with value = 15 is deleted from the BST-

Method-02:
• Visit to the left subtree of the deleting node.
• Pluck the greatest value element called as inorder predecessor.
• Replace the deleting element with its inorder predecessor.
Example-Consider the following example where node with value = 15 is deleted from the BST-

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 45


Data Structure 3rd Sem

AVL Tree-
• AVL trees are special kind of binary search trees.
• In AVL trees, height of left subtree and right subtree of every node differs by at most one.
• AVL trees are also called as self-balancing binary search trees.
Example- Following tree is an example of AVL tree

This tree is an AVL tree because-


• It is a binary search tree.
• The difference between height of left subtree and right subtree of every node is at most one.

Following tree is not an example of AVL Tree-

his tree is not an AVL tree because-


• The difference between height of left subtree and right subtree of root node = 4 – 2 = 2.
• This difference is greater than one.
Balance Factor- In AVL tree,
• Balance factor is defined for every node.
• Balance factor of a node = Height of its left subtree – Height of its right subtree
In AVL tree, Balance factor of every node is either 0 or 1 or -1.
AVL Tree Operations-
Like BST Operations, commonly performed operations on AVL tree are-
1. Search Operation
2. Insertion Operation
3. Deletion Operation

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 46


Data Structure 3rd Sem

After performing any operation on AVL tree, the balance factor of each node is checked.
There are following two cases possible-
Case-01:
• After the operation, the balance factor of each node is either 0 or 1 or -1.
• In this case, the AVL tree is considered to be balanced.
• The operation is concluded.
Case-02:
• After the operation, the balance factor of at least one node is not 0 or 1 or -1.
• In this case, the AVL tree is considered to be imbalanced.
• Rotations are then performed to balance the tree.

Rules To Remember-
Rule-01: After inserting an element in the existing AVL tree,
• Balance factor of only those nodes will be affected that lies on the path from the newly inserted node
to the root node.

Rule-02:To check whether the AVL tree is still balanced or not after the insertion,
• There is no need to check the balance factor of every node.
• Check the balance factor of only those nodes that lies on the path from the newly inserted node to the
root node.

Rule-03: After inserting an element in the AVL tree,


• If tree becomes imbalanced, then there exists one particular node in the tree by balancing which the
entire tree becomes balanced automatically.
• To re balance the tree, balance that particular node.

To find that particular node,


• Traverse the path from the newly inserted node to the root node.
• Check the balance factor of each node that is encountered while traversing the path.
• The first encountered imbalanced node will be the node that needs to be balanced.

To balance that node,


• Count three nodes in the direction of leaf node.
• Then, use the concept of AVL tree rotations to re balance the tree.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 47


Data Structure 3rd Sem

AVL Tree Rotations-(Rotation is the process of moving the nodes to make tree balanced.)
There are 4 kinds of rotations possible in AVL Trees-

1. Left Rotation (LL Rotation)


2. Right Rotation (RR Rotation)
3. Left-Right Rotation (LR Rotation)
4. Right-Left Rotation (RL Rotation)

Cases Of Imbalance And Their Balancing Using Rotation Operations-


Case-01:

Case-02:

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 48


Data Structure 3rd Sem

Case-03:

Case-04:

Problem- Construct AVL Tree for the following sequence of numbers-


50 , 20 , 60 , 10 , 8 , 15 , 32 , 46 , 11 , 48
Solution- Step-01: Insert 50

Step-02: Insert 20
• As 20 < 50, so insert 20 in 50’s left sub tree.

Step-03: Insert 60
• As 60 > 50, so insert 60 in 50’s right sub tree.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 49


Data Structure 3rd Sem

Step-04: Insert 10
• As 10 < 50, so insert 10 in 50’s left sub tree.
• As 10 < 20, so insert 10 in 20’s left sub tree.

Step-05: Insert 8
• As 8 < 50, so insert 8 in 50’s left sub tree.
• As 8 < 20, so insert 8 in 20’s left sub tree.
• As 8 < 10, so insert 8 in 10’s left sub tree.

To balance the tree,


• Find the first imbalanced node on the path from the newly inserted node (node 8) to the root
node.
• The first imbalanced node is node 20.
• Now, count three nodes from node 20 in the direction of leaf node.
• Then, use AVL tree rotation to balance the tree.
Following this, we have-

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 50


Data Structure 3rd Sem

Step-06: Insert 15
• As 15 < 50, so insert 15 in 50’s left sub tree.
• As 15 > 10, so insert 15 in 10’s right sub tree.
• As 15 < 20, so insert 15 in 20’s left sub tree.

To balance the tree,


•Find the first imbalanced node on the path from the newly inserted node (node 15) to the root
node.
• The first imbalanced node is node 50.
• Now, count three nodes from node 50 in the direction of leaf node.
• Then, use AVL tree rotation to balance the tree.
Following this, we have-

Step-07: Insert 32
• As 32 > 20, so insert 32 in 20’s right sub tree.
• As 32 < 50, so insert 32 in 50’s left sub tree.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 51


Data Structure 3rd Sem

Step-08: Insert 46
• As 46 > 20, so insert 46 in 20’s right sub tree.
• As 46 < 50, so insert 46 in 50’s left sub tree.
• As 46 > 32, so insert 46 in 32’s right sub tree.

Step-09: Insert 11
• As 11 < 20, so insert 11 in 20’s left sub tree.
• As 11 > 10, so insert 11 in 10’s right sub tree.
• As 11 < 15, so insert 11 in 15’s left sub tree.

Step-10: Insert 48
• As 48 > 20, so insert 48 in 20’s right sub tree.
• As 48 < 50, so insert 48 in 50’s left sub tree.
• As 48 > 32, so insert 48 in 32’s right sub tree.
• As 48 > 46, so insert 48 in 46’s right sub tree.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 52


Data Structure 3rd Sem

o balance the tree,


•Find the first imbalanced node on the path from the newly inserted node (node 48) to the root
node.
• The first imbalanced node is node 32.
• Now, count three nodes from node 32 in the direction of leaf node.
• Then, use AVL tree rotation to balance the tree.
Following this, we have-

This is the final balanced AVL tree after inserting all the given elements.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 53


Data Structure 3rd Sem

What is a B Tree?
B tree is a balancing tree that helps in maintaining and sorting data, and also grants searches, insertions,
deletions, and sequential access. It is also known as an m-way tree where m is used to determine the order of
the tree. Depending upon the value of m, a B-tree node can have more than one key and more than two
children based on the value of m.
Also, in the case of B-tree, it allows both key values and data pointers in internal and leaf nodes. This is
counted as one of the major disadvantages because the capability to embed the nodes at a particular level is
reduced thus extending the node levels in it, which is not good.

Application of Binary Trees:


• Search algorithms: Binary search algorithms use the structure of binary trees to efficiently search
for a specific element. The search can be performed in O(log n) time complexity, where n is the
number of nodes in the tree.
• Sorting algorithms: Binary trees can be used to implement efficient sorting algorithms, such as
binary search tree sort and heap sort.
• Database systems: Binary trees can be used to store data in a database system, with each node
representing a record. This allows for efficient search operations and enables the database system to
handle large amounts of data.
• File systems: Binary trees can be used to implement file systems, where each node represents a
directory or file. This allows for efficient navigation and searching of the file system.
• Compression algorithms: Binary trees can be used to implement Huffman coding, a compression
algorithm that assigns variable-length codes to characters based on their frequency of occurrence in the
input data.
• Decision trees: Binary trees can be used to implement decision trees, a type of machine learning
algorithm used for classification and regression analysis.
• Game AI: Binary trees can be used to implement game AI, where each node represents a possible
move in the game. The AI algorithm can search the tree to find the best possible move.
Real-time applications of Binary Trees:
• DOM in HTML.
• File explorer.
• Used as the basic data structure in Microsoft Excel and spreadsheets.
• Editor tool: Microsoft Excel and spreadsheets.
• Evaluate an expression
• Routing Algorithms

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 54


Data Structure 3rd Sem

Advantages of Binary Tree:


• Efficient searching: Binary trees are particularly efficient when searching for a specific element,
as each node has at most two child nodes, allowing for binary search algorithms to be used. This
means that search operations can be performed in O(log n) time complexity.
• Ordered traversal: The structure of binary trees enables them to be traversed in a specific order,
such as in-order, pre-order, and post-order. This allows for operations to be performed on the nodes in
a specific order, such as printing the nodes in sorted order.
• Memory efficient: Compared to other tree structures, binary trees are relatively memory-efficient
because they only require two child pointers per node. This means that they can be used to store large
amounts of data in memory while still maintaining efficient search operations.
• Fast insertion and deletion: Binary trees can be used to perform insertions and deletions in O(log
n) time complexity. This makes them a good choice for applications that require dynamic data
structures, such as database systems.
• Easy to implement: Binary trees are relatively easy to implement and understand, making them a
popular choice for a wide range of applications.
• Useful for sorting: Binary trees can be used to implement efficient sorting algorithms, such as
heap sort and binary search tree sort.

Disadvantages of Binary Tree:


• Limited structure: Binary trees are limited to two child nodes per node, which can limit their
usefulness in certain applications. For example, if a tree requires more than two child nodes per node, a
different tree structure may be more suitable.
• Unbalanced trees: Unbalanced binary trees, where one subtree is significantly larger than the
other, can lead to inefficient search operations. This can occur if the tree is not properly balanced or if
data is inserted in a non-random order.
• Space inefficiency: Binary trees can be space inefficient when compared to other data structures.
This is because each node requires two child pointers, which can be a significant amount of memory
overhead for large trees.
• Slow performance in worst-case scenarios: In the worst-case scenario, a binary tree can become
degenerate, meaning that each node has only one child. In this case, search operations can degrade to
O(n) time complexity, where n is the number of nodes in the tree.
• Complex balancing algorithms: To ensure that binary trees remain balanced, various balancing
algorithms can be used, such as AVL trees and red-black trees. These algorithms can be complex to
implement and require additional overhead, making them less suitable for some applications.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 55


Data Structure 3rd Sem

What is a B+ Tree?
B+ tree is nothing but an advancement of B tree that allows efficient and smooth insertion, deletion, and
sequential access. It is also known as an n-array tree which holds a large number of children per node.
B+ tree helps in reducing the drawback faced by B-tree by saving the data pointers at the leaf node level and
simply stocking the key values in the internal nodes.

S.No B tree B+ tree

1. B tree is a popular terminology that belongs to the B+ tree is nothing but an


computer science family. It is a balancing tree that helps advancement of B tree that allows
in maintaining and sorting data, and also grants efficient and smooth insertion,
searches, insertions, deletions, and sequential access. deletion, and sequential access.

2. In the case of B tree, the leaf nodes include data In the case of B+ tree, only the leaf
pointers. nodes include data pointers.

3. Here, the insertion may take longer. Here, the insertion is easier and faster
than the B tree.

4. In B tree, there is no duplicate of keys sustained in the In B+ tree, duplicates of keys are
tree. maintained.

5. The search process may take a longer time because all Here the search is faster than the B
the keys are not obtainable at the leaf. tree as the keys are present at leaf
nodes.

6. Deletion process is complicated. Deletion process is easier.

7. There are no redundant search keys available . The redundant search keys are
available.

8. In the B tree, all the leaf nodes are not saved as a On the B+ tree, all the leaf nodes are
structural linked list. saved as a structural linked list

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 56


Data Structure 3rd Sem

In data structures,
• There are several searching techniques like linear search, binary search, search trees etc.
• In these techniques, time taken to search any particular element depends on the total number of
elements.
Example-
• Linear Search takes O(n) time to perform the search in unsorted arrays consisting of n elements.
• Binary Search takes O(logn) time to perform the search in sorted arrays consisting of n
elements.
• It takes O(logn) time to perform the search in Binary Search Tree consisting of n elements.
Drawback- The main drawback of these techniques is-
• As the number of elements increases, time taken to perform the search also increases.
• This becomes problematic when total number of elements become too large.
Hashing in Data Structure- In data structures,
• Hashing is a well-known technique to search any particular element among several elements.
• It minimizes the number of comparisons while performing the search.

Advantage- Unlike other searching techniques,


• Hashing is extremely efficient.
• The time taken by it to perform the search does not depend upon the total number of elements.
• It completes the search with constant time complexity O(1).

Hashing Mechanism: In hashing,


• An array data structure called as Hash table is used to store the data items.
• Based on the hash key value, data items are inserted into the hash table.

Hash Key Value-


• Hash key value is a special value that serves as an index for a data item.
• It indicates where the data item should be be stored in the hash table.
• Hash key value is generated using a hash function.

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 57


Data Structure 3rd Sem

Hash Function-

Hash function is a function that maps any big number or string to a small integer value.

Hash function takes the data item as an input and returns a small integer value as an output.
• The small integer value is called as a hash value.
• Hash value of the data item is then used as an index for storing it into the hash table.
Types of Hash Functions- There are various types of hash functions available such as-
1. Mid Square Hash Function
2. Division Hash Function
3. Folding Hash Function etc
It depends on the user which hash function he wants to use.
Properties of Hash Function: The properties of a good hash function are-
• It is efficiently computable.
• It minimizes the number of collisions.
• It distributes the keys uniformly over the table.
Collision in Hashing- In hashing,
• Hash function is used to compute the hash value for a key.
• Hash value is then used as an index to store the key in the hash table.
• Hash function may return the same hash value for two or more keys.

When the hash value of a key maps to an already occupied bucket of the hash table,
it is called as a Collision.

Collision Resolution Techniques-

Collision Resolution Techniques are the techniques used for resolving or handling the collision.

Collision resolution techniques are classified as-

Vijay Kumar M P, Asst. Professor, Dept of AI & ML UCOE Page 58

You might also like