manan ds 6,7,10
manan ds 6,7,10
Experiment No: 6
AIM : Tree
Date: / / 2024
Theory:
A binary search tree is a binary tree in which each node possessed a key that satisfy the
following conditions
1. All key (if any) in the left sub tree of the root precedes the key in the root.
2. The key in the root precedes all key (if any) in the right sub tree.
3. The left and right sub tree sub trees of the root are again search trees.
Operations on tree
The most common operations performed on tree structure are that of traversal. This is a procedure
by which each node in the tree is processed exactly once in a systematic manner.
1. Pre-order Traversal
Page No
Data Structure (3130702) 230170132038
2. In-order Traversal
3. Post-order Traversal
Pre-order
In-order
Post-order
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node
{ int data;
struct Node* left;
struct Node* right;
};
Page No
Data Structure (3130702) 230170132038
struct Node* insertNode(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insertNode(root->left, data);
} else if (data > root->data) {
root->right = insertNode(root->right, data);
}
return root;
}
int main() {
struct Node* root = NULL;
int n, value;
return 0;
}
Output:
Page No
Data Structure (3130702) 230170132038
6.2 Implement recursive tree traversing methods in-order, preorder and post-order
traversal.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node
{ int data;
struct Node* left;
struct Node* right;
};
int main() {
struct Node* root = NULL;
int n, value;
Page No
Data Structure (3130702) 230170132038
postorderTraversal(root);
printf("\n");
return 0;
}
Output:
In-order Traversal: 20 30 40 50 70
Pre-order Traversal: 50 30 20 40 70
Post-order Traversal: 20 40 30 70 50
6.3 Identify widely used applications which use Tree data structure for implementation
of its important feature.
Observations:
Page No
Data Structure (3130702) 230170132038
Efficiency
Flexibility
Scalability
Hierarchical Nature
Application-specific Optimizations
Conclusion:
Tree data structures play a crucial role in the implementation of many widely used
applications due to their efficiency, flexibility, and hierarchical nature. From databases and file
systems to networking and artificial intelligence, trees provide a foundation for efficient data
organization, retrieval, and manipulation. The versatility of trees enables them to be used in a
variety of fields, from search engines that utilize tries for auto-completion to machine learning
algorithms that rely on decision trees for classification and decision-making tasks. The ability of
trees to scale and adapt to different data types makes them indispensable in modern computing.
Quiz:
Suggested Reference:
Page No
Data Structure (3130702) 230170132038
Experiment No: 7
AIM : Graph
Date:
Theory:
Graph:
A graph G can be defined as a non-empty set of vertices or nodes (V) and a set of edges (E) that
represents the relationship or connection between those nodes. The edges can be defined as a
mapping from E to pairs of elements of V. A graph can be represented as G = (V, E), where V
represents the set of nodes and E represents the set of edges. Each edge of the graph G can be
associated with a pair of nodes of the graph. If an edge X belongs to E and is associated with a pair
of nodes (u, v), where u and v belong to V, then we say that edge X connects node u and node v.
DFS is a graph traversal algorithm that is similar to the preorder traversal of a tree. The traversal
can start from any vertex vi of the graph. Initially, the vertex vi is visited, and then all the adjacent
vertices to vi are traversed recursively using DFS. As a graph can have cycles, we need to avoid
revisiting a node. To achieve this, when a vertex V is visited, it is marked as visited and should not
be selected for traversal again.
Page No
Data Structure (3130702) 230170132038
Breadth First Search (BFS) starts from a vertex v0 and marks it as visited. Then, all the
vertices adjacent to v0 are visited next.
Let the vertices adjacent to v0 be v1, v2, v3, and v4. These vertices are marked as visited.
All unvisited vertices adjacent to v1, v2, v3, and v4 are visited next.
The above process continues until all vertices are visited.
The algorithm for BFS maintains a list of vertices that have been visited but not explored
for adjacent vertices. This list is stored in a queue.
The queue initially contains the starting vertex.
In each iteration, a vertex is removed from the queue, and its adjacent vertices, which have
not been visited yet, are added to the queue.
The algorithm terminates when the queue becomes empty.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
int* visited;
};
struct Node* createNode(int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
Page No
Data Structure (3130702) 230170132038
int i;
for (i = 0; i < vertices; i++)
{ graph->adjLists[i] =
NULL; graph->visited[i] =
0;
}
return graph;
}
void addEdge(struct Graph* graph, int src, int dest)
{ struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
graph->visited[startVertex] = 1;
queue[++rear] = startVertex;
while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0)
{ queue[++rear] = adjVertex;
graph->visited[adjVertex] = 1;
}
temp = temp->next;
}
}
printf("\n");
Page No
Data Structure (3130702) 230170132038
}
// DFS algorithm
void DFS(struct Graph* graph, int vertex)
{ struct Node* adjList = graph->adjLists[vertex];
struct Node* temp = adjList;
graph->visited[vertex] = 1;
printf("%d ", vertex);
if (graph->visited[connectedVertex] == 0)
{ DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
addEdge(graph, 3, 5);
BFS(graph, 0);
DFS
resetVisited(graph);
Page No
Data Structure (3130702) 230170132038
return 0;
}
Output:
BFS Traversal: 0 1 2 3 4 5
DFS Traversal: 0 1 3 4 5 2
7.2 Identify widely used applications which use graphs data structure for implementation
of its important feature.
1) Social Networks
Feature: Graph algorithms are used for finding mutual friends, recommending new
connections, and identifying communities within the network.
Example: Facebook’s “People You May Know” feature is built on graph traversal
algorithms to recommend friends based on mutual connections.
2) Google Maps and GPS Navigation Systems
Feature: Graph algorithms like Dijkstra's and A* are used to find the shortest path
between locations.
Example: Google Maps uses graph data structures to calculate the quickest route,
taking into account factors like distance, traffic, and alternate routes.
3) Recommendation Systems
Feature: These systems use collaborative filtering and content-based filtering
techniques, where users and products are represented as nodes and interactions (e.g.,
purchases, ratings) as edges.
Example: Netflix recommends movies to users based on their viewing history and the
preferences of similar users, all of which is modeled using a graph.
4) Web Page Ranking (Google's PageRank)
Feature: Google’s PageRank algorithm is based on graph traversal to rank the
importance of web pages based on their inbound and outbound links.
Example: PageRank uses the graph structure of the web to determine which pages are
more relevant to a given search query.
5) Computer Networks (Routing Algorithms)
Feature: Routing algorithms like Open Shortest Path First (OSPF) and Border
Gateway Protocol (BGP) use graphs to find optimal paths for data transmission.
Example: Routers use graph algorithms to efficiently forward data packets across the
internet by finding the shortest or least congested path.
6) Artificial Intelligence (AI) and Machine Learning (ML)
Feature: Graph Neural Networks (GNN) and knowledge graphs help in tasks like
natural language processing, recommendation systems, and fraud detection.
Example: Knowledge graphs are used in semantic search engines like Google Search
to understand user queries in a more meaningful way by connecting related concepts.
7) Transport and Logistics
Feature: Graph algorithms are used for vehicle routing problems, minimizing costs
and delivery time.
Example: Amazon uses graph-based optimization to deliver packages efficiently by
Page No
Data Structure (3130702) 230170132038
reducing travel time and fuel consumption.
8) Compiler Design
Feature: Data-flow analysis, dead code elimination, and instruction scheduling are
graph-based features in modern compilers.
Example: Compilers build a control flow graph (CFG) to optimize code execution
paths and detect possible optimizations.
Observations:
Complex Relationships
Optimization
Scalability
Flexibility
AI Integration
Conclusion:
Graphs are a foundational data structure in computer science, with applications spanning
across various domains, from social networks to AI. They are invaluable in any scenario where
relationships between data points must be modeled, analyzed, and optimized.
Quiz:
Suggested Reference:
Page No
Data Structure (3130702) 230170132038
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)
Marks
Page No
Data Structure (3130702) 230170132038
Experiment No: 10
10.1 Write a program to create hash table and handle the collision using linear probing.
10.2 Write a program to demonstrate the file primitives such as fopen, fclose, fprintf.
10.3 Identify widely used applications which use Hashing technique for implementation of
its important feature.
Date:
Theory:
Hashing
Hashing is a method used to map a large number of data items to a smaller table by utilizing a
hashing function. This technique transforms a range of key values into a range of indexes of an
array.There are two different forms of hashing.
1. Open hashing or external hashing: Open or external hashing, allows records to be stored
in unlimited space (could be a hard disk). It places no limitation on the size of the tables.
2. Close hashing or internal hashing: Closed or internal hashing, uses a fixed space for
storage and thus limits the size of hash table.
Hashing Functions
Page No
Data Structure (3130702) 230170132038
Different hashing functions
1. Division-Method
2. Folding Method
3. Algebraic Coding
4. Multiplicative Hashing
5. Digit Analysis
6. Mid-square Methods
7. Length Dependent Method
File
In computing, a file is a group of records, where each record comprises one or more fields that have
the same sequence. Typically, each field has a predetermined length.
1. Sequential files
2. Direct files
3. Index files
4. Indexed Sequential files
5. Relative files
1. Creation
2. Insertion
3. Deletion
4. Updation
5. Reading
6. Searching
Page No
Data Structure (3130702) 230170132038
10.1 Write a program to create hash table and handle the collision using linear probing.
Program:
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
struct HashTableEntry {
int key;
int value;
};
table[index].key = key;
table[index].value = value;
}
Page No
Data Structure (3130702) 230170132038
int originalIndex = index;
int main() {
struct HashTableEntry hashTable[TABLE_SIZE];
initializeTable(hashTable);
switch (choice)
{ case 1:
printf("Enter key: ");
scanf("%d", &key);
printf("Enter value: ");
scanf("%d", &value);
insert(hashTable, key, value);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
Page No
Data Structure (3130702) 230170132038
value = search(hashTable, key);
if (value != -1) {
printf("Key found with value: %d\n", value);
} else {
printf("Key not found!\n");
}
break;
case 3:
displayTable(hashTable);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
1. Insert
2. Search
3. Display
4. Exit
Enter your choice: 1
Enter key: 12
Enter value: 100
1. Insert
2. Search
3. Display
4. Exit
Enter your choice: 1
Enter key: 22
Enter value: 200
1. Insert
2. Search
3. Display
4. Exit
Enter your choice: 3
Page No
Data Structure (3130702) 230170132038
Index Key Value
0 -- --
1 -- --
2 -- --
3 12 100
4 22 200
5 -- --
6 -- --
7 -- --
8 -- --
9 -- --
1. Insert
2. Search
3. Display
4. Exit
Enter your choice: 2
Enter key to search: 12
Key found with value: 100
10.2 Write a program to demonstrate the file primitives such as fopen, fclose, fprintf.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *filePtr;
if (filePtr == NULL) {
printf("Error! Could not open file.\n");
exit(1); // Exit if fopen() failed
}
fclose(filePtr);
Page No
Data Structure (3130702) 230170132038
printf("Data successfully written to 'example.txt'.\n");
return 0;
}
Output:
10.3 Identify widely used applications which use Hashing technique for implementation of
its important feature.
1) Databases
Application: Hashing is used in database indexing to quickly retrieve data without
scanning entire tables.
Feature: Hash tables are used to index rows for fast lookups, especially in large
datasets.
2) Cryptography
Application: Hash functions are a cornerstone of cryptography, where they are used
to ensure data integrity and generate digital signatures.
Feature: Cryptographic hash functions (e.g., SHA-256, MD5) create fixed-length
outputs that are virtually unique, even for slightly different inputs.
3) Password Storage
Application: Hashing is widely used to securely store user passwords in databases.
Feature: Passwords are hashed before storage so that even if a database is
compromised, attackers cannot easily recover the original passwords.
4) Caching Systems
Application: Hashing is used in caching systems like Redis and Memcached to store
and retrieve frequently used data quickly.
Feature: Hashing allows mapping of cached objects to specific keys for rapid access,
reducing the load on primary data stores.
5) Compiler Design
Application: Compilers use hashing for symbol tables, which store identifiers such as
variable names and function names for fast lookups.
Feature: A hash table allows quick access to variables and functions when compiling
the code, improving the speed of the compilation process.
6) Data Deduplication
Application: Hashing is used in data deduplication to identify and eliminate duplicate
files or blocks of data.
Feature: Hashes of files or blocks are compared, and if two hashes match, the system
identifies the data as a duplicate.
7) Load Balancing
Application: Hashing is used in load balancing algorithms to distribute tasks or
requests evenly across multiple servers.
Feature: Consistent hashing ensures that requests for a specific resource are always
directed to the same server, optimizing resource usage.
8) File Systems
Page No
Data Structure (3130702) 230170132038
Application: File systems use hashing to locate file blocks and manage directories
efficiently.
Feature: A hash table can map file names or file blocks to their locations on disk for
faster file access.
9) Blockchain and Cryptocurrency
Application: Hashing is essential for verifying transactions and securing data in
blockchain and cryptocurrencies like Bitcoin and Ethereum.
Feature: Hashing ensures that each block of data in the blockchain is uniquely
identified and secure, making it resistant to tampering.
Observations:
Efficiency
Security
Scalability
Storage Optimization
Robustness
Conclusion:
Quiz:
Suggested Reference:
Page No
Data Structure (3130702) 230170132038
Rubric-wise marks obtained:
Page No