Practicle File Dsa 1
Practicle File Dsa 1
Course Instructor
Dr Antima Jain
Assistant Professor Senior Grade 1
School of Computing Science and Engineering
VIT Bhopal University, Sehore
Madhya Pradesh
Input-
Array implementation-
-
Linked list implementation of queue-
Output-
Aim-Implementation of Binary Tree Traversals.
Algorithm- preorder-
PreOrderTraversal(root)
2 if root == NULL then
3 return
4 end if
5 Visit(root)
6 PreOrderTraversal(root->left)
7 PreOrderTraversal(root->right)
8end PreOrderTraversal
Inorder-
InOrderTraversal(root)
2 if root == NULL then
3 return
4 end if
5 InOrderTraversal(root->left)
6 Visit(root)
7 InOrderTraversal(root->right)
8end InOrderTraversal
Postorder-
PostOrderTraversal(root)
2 if root == NULL then
3 return
4 end if
5 PostOrderTraversal(root->left)
6 PostOrderTraversal(root->right)
7 Visit(root)
8end PostOrderTraversal
Input-
-
Output-
Aim- Implementation of Binary Search Tree.
Algorithm-
1. Insert Node
• If root is NULL, create a new node with data and return it.
• If data is less than root's value, recursively call InsertNode on
the left subtree.
• If data is greater than root's value, recursively
call InsertNode on the right subtree.
• If data is equal to root's value, return root (no duplicates
allowed).
2. Search Node
• If root is NULL, return FALSE (node not found).
• If data is equal to root's value, return TRUE (node found).
• If data is less than root's value, recursively call SearchNode on
the left subtree.
• If data is greater than root's value, recursively
call SearchNode on the right subtree.
3. Delete Node
• If root is NULL, return root (node not found).
• If data is less than root's value, recursively call DeleteNode on
the left subtree.
• If data is greater than root's value, recursively
call DeleteNode on the right subtree.
• If data is equal to root's value, delete the node and return the
updated root node.
4. Traversal
• In-Order Traversal: Recursively traverse the left subtree, visit
the current node, and recursively traverse the right subtree.
• Pre-Order Traversal: Visit the current node, recursively traverse
the left subtree, and recursively traverse the right subtree.
• Post-Order Traversal: Recursively traverse the left subtree,
recursively traverse the right subtree, and visit the current node
Input-
Output-
Aim- Implementation of BFS and DFS
Algorithm-
Breadth first search
1. Create an empty queue Q
2. Initialize Q with the root node R as the first element
3. While Q is not empty a. Dequeue a
node N from Q b. Process N (e.g., print its value) c. Enqueue all
children of N into Q (if any) i. If N has a left child, enqueue it
into Q ii. If N has a right child, enqueue it into Q
4. Repeat step 3 until Q is empty
Depth first search-
1. Create a recursive function DFS
2. Call DFS with the root node R as the argument
3. In the DFS function a. Process the current node N (e.g., print its
value) b. Recursively call DFS on the left subtree of N (if any)
c. Recursively call DFS on the right subtree of N (if any)
4. Return from the DFS function
Input-
output-
Aim- Implementation of Dijkstra Shortest Path Algorithms.
Algorithm:
1. Create a distance array dist and a visited array visited of size V.
2. Initialize the distance array with infinity (INT_MAX) and the
visited array with 0.
3. Set the distance to the source vertex to 0.
4. Repeat the following steps until all vertices are visited: a. Find
the vertex u with the minimum distance value that has not
been visited. b. Mark u as visited. c. Update the distance values
of all adjacent vertices v of u if the distance to v through u is
less than the current distance value of v.
5. Print the shortest distance array.
Output-
Aim- Implementation of Prims and Kruskal Algorithms.
Algorithm-
Prims algorithm-
1. Choose an arbitrary vertex s in V as the starting vertex.
2. Initialize an empty tree T and a set U of vertices that have been
included in T.
3. Initialize a priority queue Q with all edges incident on s, where
the priority of each edge is its weight.
4. While Q is not empty: a. Extract the edge e with the minimum
priority (i.e., the edge with the minimum weight) from Q. b.
If e connects a vertex u in U to a vertex v not in U, then: i.
Add e to T. ii. Add v to U. iii. Add all edges incident on v to Q.
5. Return T as the minimum spanning tree of G.
Kruskal algorithm-
1. Sort all edges in E in non-decreasing order of their weights.
2. Initialize an empty tree T and a disjoint set D of vertices.
3. For each edge e in the sorted order: a. Find the roots u and v of
the trees in D that contain the endpoints of e. b. If u and v are
in different trees, then: i. Add e to T. ii. Merge the trees
containing u and v into a single tree in D.
4. Return T as the minimum spanning tree of G.
Input-
Prim’s algorithm
Kruskal's Algorithm
1.prims Output-
2.kruskal
Aim- Implementation Hashing Techniques
Chaining (Separate Chaining) Algorithm:
Hash Table Insertion Algorithm:
1. Input: key and value to be inserted into the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the index i of the hash table where
the key will be stored: i = h % TABLE_SIZE.
4. Collision Resolution: If the bucket at index i is empty, create a
new node with the key and value and store it in the bucket.
Otherwise, traverse the linked list at the bucket and append a
new node with the key and value to the end of the list.
5. Return: Return successfully inserted.
Hash Table Search Algorithm:
1. Input: key to be searched in the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the index i of the hash table where
the key might be stored: i = h % TABLE_SIZE.
4. Collision Resolution: Traverse the linked list at the bucket and
search for a node with the matching key. If found, return the
associated value. If not found, return a special value indicating
not found (e.g., -1).
5. Return: Return the search result.
Open Addressing (Linear Probing) Algorithm:
Hash Table Insertion Algorithm:
1. Input: key and value to be inserted into the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the initial index i of the hash table
where the key will be stored: i = h % TABLE_SIZE.
4. Collision Resolution: If the bucket at index i is empty, store
the key and value in the bucket. Otherwise, probe other
buckets in a linear sequence (e.g., i = (i + 1) % TABLE_SIZE) until
an empty bucket is found. Store the key and value in the empty
bucket.
5. Return: Return successfully inserted.
Hash Table Search Algorithm:
1. Input: key to be searched in the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the initial index i of the hash table
where the key might be stored: i = h % TABLE_SIZE.
4. Collision Resolution: Probe other buckets in a linear sequence
(e.g., i = (i + 1) % TABLE_SIZE) until a bucket with a
matching key is found or an empty bucket is found. If a
matching key is found, return the associated value. If an empty
bucket is found, return a special value indicating not found
(e.g., -1).
5. Return: Return the search result.
Input- Chaining (Separate Chaining)
Open Addressing (Linear Probing)
Output-