ADS lab programs
ADS lab programs
import java.util.Scanner;
// BST class containing methods for insertion, in-order traversal, and the main function
class BST {
static Node root=null;
return root;
}
System.out.println("\nMenu:");
System.out.println("1. Insert a node");
System.out.println("2. Display in-order traversal");
System.out.println("3. Exit");
while (true) {
System.out.print("Enter your choice: ");
case 2:
System.out.println("In-order traversal:");
bst.inOrder(root);
System.out.println(); // For better formatting
break;
case 3:
System.out.println("Exiting program. Goodbye!");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
// merge sort
import java.util.Scanner;
// Merge function
public static void merge(int[] arr, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left, j = mid + 1, k = 0;
mergeSort(arr, 0, n - 1);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
// heap sort
import java.util.Scanner;
// Creating the array and taking user input for its elements
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.println("Unsorted array:");
printArray(arr);
heapSort(arr);
System.out.println("Sorted array:");
printArray(arr);
scanner.close();
}
}
// quick sort
import java.util.Scanner;
class QuickSort {
void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1); // Left of pivot
quickSort(arr, pivot + 1, high); // Right of pivot
}
}
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
sorter.quickSort(arr, 0, n - 1);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
// Avl
import java.util.Scanner;
class AVLTree {
class Node {
int key, height;
Node left, right;
Node(int d) {
key = d;
height = 1;
}
}
Node root;
int height(Node n) {
if (n == null) {
return 0;
}
return n.height;
}
int balanceFactor(Node n) {
if (n == null) {
return 0;
}
return height(n.left) - height(n.right);
}
Node rotateRight(Node y) {
Node x = y.left;
Node T = x.right;
x.right = y;
y.left = T;
y.height = Math.max(height(y.left), height(y.right)) + 1;
x.height = Math.max(height(x.left), height(x.right)) + 1;
return x;
}
Node rotateLeft(Node x) {
Node y = x.right;
Node T = y.left;
y.left = x;
x.right = T;
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;
return y;
}
return node;
}
// dictionary
import java.util.Scanner;
class HashDictionary {
private String[] words; // Array to store words
private String[] meanings; // Array to store meanings of words
private int size; // Size of the dictionary (capacity)
private int count; // Number of words currently in the dictionary
// Hash function to generate a hash value for the word (based on character values)
private int hashFunction(String key) {
return key.chars().sum() % size; // Sum of character codes mod size
}
// Insert a word with its meaning into the dictionary
public void insert(String word, String meaning) {
if (count == size) {
System.out.println("Dictionary is full. Cannot insert new words.");
return;
}
// Linear probing: if the slot is occupied, find the next available slot
while (words[index] != null) {
if (words[index].equals(word)) {
meanings[index] = meaning; // Update meaning if word already exists
System.out.println("Updated: " + word);
return;
}
index = (index + 1) % size; // Move to next index
}
// Search using linear probing until the word is found or the dictionary is fully searched
while (words[index] != null) {
if (words[index].equals(word)) {
System.out.println(word + " -> " + meanings[index]);
return;
}
index = (index + 1) % size; // Move to next index
if (index == start) break; // Prevent infinite loop if all slots are searched
}
System.out.println("Word not found.");
}
while (true) {
// Menu options for the user
System.out.println("\n1. Insert\n2. Display\n3. Search\n4. Exit");
int choice = scanner.nextInt(); // Get the user’s choice
scanner.nextLine(); // Consume the newline character after integer input
switch (choice) {
case 1:
System.out.print("Word: ");
String word = scanner.nextLine(); // Get the word to insert
System.out.print("Meaning: ");
String meaning = scanner.nextLine(); // Get the meaning of the word
dict.insert(word, meaning); // Insert the word and its meaning into the dictionary
break;
case 2:
dict.display(); // Display all the words in the dictionary
break;
case 3:
System.out.print("Search word: ");
word = scanner.nextLine(); // Get the word to search
dict.search(word); // Search for the word in the dictionary
break;
case 4:
scanner.close(); // Close the scanner and exit the program
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
// kmp
import java.util.Scanner;
class KMP {
static void constructLps(String pat, int[] lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < pat.length()) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
scanner.close();
}
}
// brute force
import java.util.Scanner;
scanner.close();
}
}
// boyer
import java.util.Scanner;
// Fill the bad character table with the index of each character in the pattern
for (int i = 0; i < pattern.length(); i++) {
badChar[pattern.charAt(i)] = i;
}
return badChar;
}
scanner.close();
}
}
// shortest path
import java.util.Scanner;
// Function to find the vertex with the minimum distance value that is not yet processed
private static int minDistance(int[] dist, boolean[] sptSet, int vertices) {
int min = Integer.MAX_VALUE;
int minIndex = -1;
// Update the distance values of the adjacent vertices of the selected vertex
for (int v = 0; v < vertices; v++) {
if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] <
dist[v]) {
dist[v] = dist[u] + graph[u][v];
parent[v] = u; // Set parent of v to u
}
}
}
// Print the shortest path and the total weight
if (dist[dest] == Integer.MAX_VALUE) {
System.out.println("No path exists from source to destination.");
} else {
System.out.println("Shortest path from " + src + " to " + dest + ": ");
printPath(parent, dest);
System.out.println("\nTotal weight: " + dist[dest]);
}
}
// Taking user input for the edges and their weights (source, destination, weight)
for (int i = 0; i < edges; i++) {
System.out.print("Enter the edge " + (i + 1) + " (format: source destination weight): ");
int start = scanner.nextInt();
int end = scanner.nextInt();
int weight = scanner.nextInt();
scanner.close();
}
}
class Graph {
private int[][] adjMatrix;
private boolean[] visited;
private int size;
visited[start] = true;
queue[rear++] = start;
System.out.println("DFS Traversal:");
graph.visited = new boolean[graph.size]; // Reset visited array
graph.dfs(startNode);
System.out.println("\nBFS Traversal:");
graph.bfs(startNode);
sc.close();
}
}