Linear Probing
Linear Probing
import java.util.Scanner;
static int size = 10; // Define the size of the hash table
static int[] hashTable = new int[size];
scanner.close();
}
}
Output:
Enter the elements separated by space:
263149
Inserted 2 at index 2
Inserted 6 at index 6
Inserted 3 at index 3
Inserted 1 at index 1
Inserted 4 at index 4
Inserted 9 at index 9
Hash Table:
Index 0: -1
Index 1: 1
Index 2: 2
Index 3: 3
Index 4: 4
Index 5: -1
Index 6: 6
Index 7: -1
Index 8: -1
Index 9: 9
Quadratic Probing
import java.util.Scanner;
static int size = 10; // Define the size of the hash table
static int[] hashTable = new int[size];
// Hash function
static int hashFunction(int key) {
return key % size;
}
if (index == originalIndex) {
System.out.println("Hash table is full!");
return;
}
}
hashTable[index] = key;
System.out.println("Inserted " + key + " at index " + index);
}
scanner.close();
}
}
Output:
Enter the elements separated by space:
5 10 15 20 20 25
Inserted 5 at index 5
Inserted 10 at index 0
Inserted 15 at index 6
Inserted 20 at index 1
Inserted 20 at index 4
Inserted 25 at index 9
Hash Table:
Index 0: 10
Index 1: 20
Index 2: -1
Index 3: -1
Index 4: 20
Index 5: 5
Index 6: 15
Index 7: -1
Index 8: -1
Index 9: 25
BST To AVL
import java.util.Scanner;
class AVLTree {
// Node class defined as a static nested class
static class Node {
int val;
Node left, right;
int height;
// Perform rotation
x.right = y;
y.left = T;
// Update heights
y.height = Math.max(height(y.left), height(y.right)) + 1;
x.height = Math.max(height(x.left), height(x.right)) + 1;
// Perform rotation
y.left = x;
x.right = T;
// Update heights
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;
return avlRoot;
}
return avlRoot;
}
if (isLeft) {
System.out.println(indent + "L---- " + root.val);
indent += "| ";
} else {
System.out.println(indent + "R---- " + root.val);
indent += " ";
}
scanner.close();
}
}
Output:
Enter the number of nodes for the BST:
6
Enter the values of the nodes:
123456
In-order traversal of the AVL tree: 1 2 3 4 5 6
AVL Tree structure:
R---- 4
L---- 2
| L---- 1
| R---- 3
R---- 5
R---- 6
Number of rotations performed: 6
M-Way-Tree
import java.util.Arrays;
import java.util.Scanner;
class BTreeNode {
int[] keys; // Stores keys in the node
BTreeNode[] children; // Stores child pointers
int keyCount; // Number of keys currently in the node
boolean isLeaf; // Flag to check if it's a leaf node
class BTree {
private BTreeNode root;
private final int m; // Max number of children per node
public BTree(int m) {
this.m = m;
this.root = new BTreeNode(m, true); // Root starts as a leaf
}
// Get M value
int m;
while (true) {
System.out.print("Enter the value of m (3, 5, or 7): ");
m = scanner.nextInt();
if (m == 3 || m == 5 || m == 7) break;
System.out.println("Invalid input! Please enter 3, 5, or 7.");
}
while (true) {
System.out.println("\n1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit");
System.out.print("Choose an operation: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter key to insert: ");
int key = scanner.nextInt();
bTree.insert(key);
break;
case 2:
System.out.print("Enter key to delete: ");
key = scanner.nextInt();
bTree.delete(key);
break;
case 3:
System.out.print("Enter key to search: ");
key = scanner.nextInt();
System.out.println(bTree.search(key) ? "Key found." : "Key not found.");
break;
case 4:
System.out.println("B-tree structure:");
bTree.display();
break;
case 5:
System.out.println("Exiting.");
scanner.close();
return;
default:
System.out.println("Invalid choice! Try again.");
}
}
}
}
Output:
Enter the value of m (3, 5, or 7): 3
Enter space-separated integers to insert: 10 20 30 40 50
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Choose an operation: 4
B-tree structure:
Level 0: 20 40
Level 1: 10
Level 1: 30
Level 1: 50
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Choose an operation: 3
Enter key to search: 30
Key found.
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Choose an operation: 2
Enter key to delete: 10
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Choose an operation: 4
B-tree structure:
Level 0: 20 40
Level 1:
Level 1: 30
Level 1: 50
Knuth-Morris-Prath
import java.util.*;
while (i < M) {
if (pattern.charAt(i) == pattern.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
KMPSearch(text, pattern);
}
}
Output:
Enter the text: ABCDEFGHIJ
Enter the pattern to search: EFGHI
Pattern found at index 4
Boyer-Moore
import java.util.*;
if (j < 0) {
System.out.println("Pattern found at index " + shift);
shift += (shift + m < n) ? m - badChar[text.charAt(shift + m)] : 1;
} else {
shift += Math.max(1, j - badChar[text.charAt(shift + j)]);
}
}
}
Output:
Enter the text: ABCDABCDABCD
Enter the pattern to search: ABCD
Pattern found at index 0
Pattern found at index 4
Pattern found at index 8