Data Structure Solution
Data Structure Solution
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
➢ 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.
➢ 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.
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.
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.
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-02:
• Vertex-A has the least in-degree.
• So, remove vertex-A and its associated edges.
• Now, update the in-degree of other vertices.
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.
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
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.
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.
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
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.
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
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.
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.
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.
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
Now, the element to search is found. So algorithm will return the index of the element matched.
#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;
}
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); }
}
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
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
For each non-leaf node, 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.
A 5 11 5*2=10
B 1 100 1*3=3
C 6 0 6*1=6
D 3 101 3*3=9
Without encoding, the total size of the string was 120 bits. After encoding the size is
reduced to 32 + 15 + 28 = 75 .
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.
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.
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.
• 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.
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,
• 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.
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.
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.
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-
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
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-
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.
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-
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-
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-
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-
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
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.
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-
Case-02:
Case-03:
Case-04:
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.
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.
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.
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.
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.
This is the final balanced AVL tree after inserting all the given elements.
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.
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.
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.
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
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.
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 are the techniques used for resolving or handling the collision.