Lab Experiment ADSA_22SCSE1180071
Lab Experiment ADSA_22SCSE1180071
Lab File
For
BACHELOR OF
UTTAR PRADESH
Semester: 1
1 |P a g e
Lab Experiment
- ADSA (Advance data structures)
NAME :GAURAV KUMAR
22SCSE1420035
B.TECH CSE DATA SCIENCE
1.Find the Maximum and Minimum Elements in an Array: Write a function to find the maximum
and minimum elements in an array
:
public classMaxMinInArray {
return;
max = array[i];
min = array[i];
findMaxMin(array);
2.Reverse an Array:
public class ReverseArray {
public static void reverseArray(int[] array) {
return;
int start = 0;
array[start] = array[end];
array[end] = temp;
start++;
end--;
System.out.println("Original Array:");
printArray(array);
reverseArray(array);
System.out.println("Reversed Array:");
printArray(array);
}
System.out.println();
3.Find the Kth Smallest/Largest Element in an Array: Write a function to find the Kth smallest
class KthElementFinder {
IllegalArgumentException("Invalid input");
IllegalArgumentException("Invalid input");
}
Arrays.sort(array); // Sort the array in ascending order
k = 3;
4. Sort an Array of 0s, 1s, and 2s: Given an array containing only 0s, 1s, and 2s, sort the array in
linear time:
switch (array[mid])
{ case 0: // Swap
array[low] = array[mid];
array[mid] = temp0;
low++; mid++;
break;
mid++;
break;
array[high] = temp2;
high--;
break;
0};
System.out.println("Original Array:");
printArray(array);
sortArray(array);
System.out.println("Sorted Array:");
printArray(array);
System.out.println();
nonZeroIndex = 0;
(nums[i] != 0) { nums[nonZeroIndex+
+] = nums[i];
{ nums[i] = 0;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
nextTemp;
return prev;
true;
return false;
middleNode(ListNode head) {
fast.next.next;
return slow;
{ current.next = l1;
l1 = l1.next; } else
{ current.next = l2;
l2 = l2.next;
current = current.next;
return dummy.next;
fast = fast.next;
= fast.next;
slow.next = slow.next.next;
return dummy.next;
class StackUsingArrayList
{ private ArrayList<Integer>
stack; public
StackUsingArrayList() { stack =
new ArrayList<>();
{ stack.add(x);
IllegalStateException("Stack is empty");
IllegalStateException("Stack is empty");
{ return stack.isEmpty();
}
12. Implement a Stack Using Linked List:
int val;
ListNode next;
ListNode(int x) { val = x; }
StackUsingLinkedList() {
top = null;
IllegalStateException("Stack is empty");
value;
IllegalStateException("Stack is empty");
return top.val;
}
public boolean isEmpty() { return
top == null;
import java.util.Stack;
stack.push(c);
stack.pop();
stack.pop();
return stack.isEmpty();
import java.util.Stack;
public class PostfixEvaluation { public
return stack.pop();
java.util.HashMap;
{ map.put(stack.pop(), num);
stack.push(num);
{ nums[i] = map.getOrDefault(nums[i], -
1);
return nums;
import java.util.LinkedList;
public QueueUsingArrayList()
{ queue.addLast(x);
}
public int dequeue() { if (isEmpty()) { throw
return queue.removeFirst();
IllegalStateException("Queue is empty");
return queue.getFirst();
{ return queue.isEmpty();
int val;
ListNode next;
ListNode(int x) { val = x; }
public QueueUsingLinkedList()
}
public void enqueue(int x)
ListNode(x);
if (rear == null)
{ front = rear =
newNode;
return;
rear.next = newNode;
rear = newNode;
value;
IllegalStateException("Queue is empty");
return front.val;
}
}
size, capacity;
this.capacity = capacity;
queue[rear] = x; rear =
+; return true;
(front + 1) % capacity;
size--;
return value;
return queue[front];
}
{ return size == 0;
import java.util.LinkedList;
generateBinaryNumbers(int n)
LinkedList<>(); queue.add("1");
System.out.println(current);
+ "1");
stack2;
public QueueUsingStacks() {
{ stack1.push(x);
if (stack2.isEmpty())
{ while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
return stack2.pop();
}
21. Implement a Binary Tree:
{ class TreeNode {
int val;
TreeNode(int x) { val = x; }
public BinaryTree() {
root = null;
{ root = insertRecursive(root,
val);
if (root == null)
{ return new
TreeNode(val);
insertRecursive(root.left, val);
insertRecursive(root.right, val);
return root;
}
{ inorderRecursive(root);
if (root != null)
{ inorderRecursive(root.left);
inorderTraversal(TreeNode root) {
if (root != null)
{ inorderTraversal(root.left);
inorderTraversal(root.right);
preorderTraversal(TreeNode root) {
if (root != null) {
preorderTraversal(root.left);
preorderTraversal(root.right);
}
24. Postorder Traversal: public void
postorderTraversal(TreeNode root) {
if (root != null)
{ postorderTraversal(root.left);
postorderTraversal(root.right);
queue.add(root);
while (!queue.isEmpty()) {
int val;
TreeNode(int x) { val = x; }
}
public int height(TreeNode root) { if (root
height(root.right); return
Math.max(leftHeight, rightHeight) + 1;
class BinaryTree {
class TreeNode {
int val;
TreeNode(int x) { val = x; }
}
}
{ class TreeNode {
int val;
TreeNode(int x) { val = x; }
{ class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
return root;
import java.util.*;
List<Integer>> adjList;
new HashMap<>();
}
public void addEdge(int u, int v)
adjList.get(u).add(v); adjList.get(v).add(u); //
ArrayList<>());
import java.util.*;
List<Integer>> adjList;
new HashMap<>();
queue.add(start);
visited.add(start);
while (!queue.isEmpty()) {
if (!visited.contains(neighbor)) { queue.add(neighbor);
visited.add(neighbor);
java
java.util.*;
List<Integer>> adjList;
new HashMap<>();
dfsUtil(start, visited);
{ visited.add(vertex);
import java.util.*;
new HashMap<>();
{ if (!visited.contains(vertex)) {
return true;
return false;
}
private boolean hasCycleUtil(int vertex, Set<Integer> visited, int parent)
{ visited.add(vertex);
if (!visited.contains(neighbor)) { if (hasCycleUtil(neighbor,
return true;
return false;
import java.util.*;
List<Integer>> adjList;
new HashMap<>();
count = 0;
for (int vertex : adjList.keySet()) {
if (!visited.contains(vertex))
{ dfsUtil(vertex, visited);
count++;
return count;
import java.util.*;
{ int u, v, weight;
this.u = u; this.v = v;
this.weight = weight;
Arrays.fill(parent, -1);
rootV) { mstWeight +=
rootU, rootV);
return mstWeight;
{ parent[rootV] = rootU;
{ parent[rootU] = rootV;
} else
{ parent[rootV] =
rootU; rank[rootU]++;
}
}
import java.util.*;
boolean[vertices]; Arrays.fill(key,
Integer.MAX_VALUE); key[0] = 0;
if (mstSet[u]) continue;
+= edge.weight;
key[v]));
return mstWeight;
}
public class Edge { int u, v,
weight) { this.u = u;
this.v = v; this.weight =
weight;
if (n <= 1) return n;
dp[0] = 0; dp[1] = 1;
return dp[n];
if (n <= 2) return n;
dp[1] = 1; dp[2] = 2;
for (int i = 3; i <= n; i++) {
return dp[n];
dp[0] = dp[1] = 0;
return dp[n];
0) return 0; if (nums.length == 1)
return nums[0];
return prev1;
java
maxEndingHere = nums[0];
Math.max(maxSoFar, maxEndingHere);
return maxSoFar;
java.util.*;
n; i++) activities.add(i);
+) { if (start[activities.get(i)] >=
end[lastSelected]) { lastSelected
= activities.get(i); count++;
return count;
v; weight = w;
totalValue += item.value;
} else {
break;
return totalValue;
HuffmanCoding { class
int freq;
= freq;
frequencyMap.getOrDefault(c, 0) + 1);
Node(entry.getKey(), entry.getValue()));
}
while (pq.size() > 1) {
pq.offer(merged);
printCodes(root, "");
class JobSequencing {
this.deadline = deadline;
this.profit = profit;
if (!slots[j]) {
slots[j] = true;
profit += job.profit;
break;
return profit;
import java.util.*;
Integer.MAX_VALUE) { dp[i] =
}
47. N-Queens Problem: public class
n);
return false;
private boolean isSafe(int[] board, int row, int col, int n) { for (int i =
Math.abs(i - row)) {
return false;
return true;
System.out.println();
}
permuteHelper(nums, 0, result);
return result;
if (index == nums.length) {
{ perm.add(num);
result.add(perm);
return;
nums[j] = temp;
}
49. Subsets: import java.util.*; public class
subset.add(num); result.add(subset);
return result;