study.com work

This commit is contained in:
Zakaria
2025-11-28 12:06:47 -05:00
commit f2b14c75c2
86 changed files with 2525 additions and 0 deletions
BIN
View File
Binary file not shown.
+3
View File
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
+7
View File
@@ -0,0 +1,7 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
</code_scheme>
</component>
+5
View File
@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ComputerScience201.iml" filepath="$PROJECT_DIR$/ComputerScience201.iml" />
</modules>
</component>
</project>
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" />
</project>
+217
View File
@@ -0,0 +1,217 @@
import java.util.Scanner;
// Node class representing each element in the Binary Search Tree (BST)
class Node {
int value;
Node left, right;
// Constructor to initialize a node with a given value
public Node(int item) {
value = item;
left = right = null;
}
}
// BinarySearchTree class that implements the BST operations
class BinarySearchTree {
Node root;
// Method to create a balanced BST from a sorted array
public void createBalancedBST(int[] sortedValues) {
root = buildBalancedBST(sortedValues, 0, sortedValues.length - 1);
}
// Helper method to recursively build a balanced BST
private Node buildBalancedBST(int[] arr, int start, int end) {
if (start > end)
return null;
int mid = (start + end) / 2; // Find the middle index
Node node = new Node(arr[mid]); // Create a new node with the middle value
// Recursively build left and right subtrees
node.left = buildBalancedBST(arr, start, mid - 1);
node.right = buildBalancedBST(arr, mid + 1, end);
return node;
}
// Method to insert a new value into the BST
public void insert(int value) {
root = insertNode(root, value);
}
// Helper method to recursively insert a new value
private Node insertNode(Node root, int value) {
if (root == null)
return new Node(value);
if (value < root.value)
root.left = insertNode(root.left, value);
else if (value > root.value)
root.right = insertNode(root.right, value);
return root;
}
// Method to delete a value from the BST
public void delete(int value) {
root = deleteNode(root, value);
}
// Helper method to recursively delete a value
private Node deleteNode(Node root, int value) {
if (root == null)
return null;
if (value < root.value)
root.left = deleteNode(root.left, value);
else if (value > root.value)
root.right = deleteNode(root.right, value);
else {
// If the node to delete has no children or one child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// If the node has two children, replace with the minimum value in the right subtree
root.value = minValue(root.right);
root.right = deleteNode(root.right, root.value);
}
return root;
}
private int minValue(Node root) {
int min = root.value;
while (root.left != null) {
min = root.left.value;
root = root.left;
}
return min;
}
// Method to print the BST in InOrder traversal (Left -> Root -> Right)
public void printInOrder() {
System.out.print("InOrder: ");
inOrder(root);
System.out.println();
}
// Method to print the BST in PreOrder traversal (Root -> Left -> Right)
public void printPreOrder() {
System.out.print("PreOrder: ");
preOrder(root);
System.out.println();
}
// Method to print the BST in PostOrder traversal (Left -> Right -> Root)
public void printPostOrder() {
System.out.print("PostOrder: ");
postOrder(root);
System.out.println();
}
// Helper method for InOrder traversal
private void inOrder(Node root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.value + " ");
inOrder(root.right);
}
}
// Helper method for PreOrder traversal
private void preOrder(Node root) {
if (root != null) {
System.out.print(root.value + " ");
preOrder(root.left);
preOrder(root.right);
}
}
// Helper method for PostOrder traversal
private void postOrder(Node root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.value + " ");
}
}
}
// Main application class to run the BST program
public class BinarySearchTreeApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BinarySearchTree bst = new BinarySearchTree();
while (true) {
System.out.println("\n--- Binary Search Tree Menu ---");
System.out.println("1. Create a binary search tree");
System.out.println("2. Add a node");
System.out.println("3. Delete a node");
System.out.println("4. Print nodes by InOrder");
System.out.println("5. Print nodes by PreOrder");
System.out.println("6. Print nodes by PostOrder");
System.out.println("7. Exit program");
System.out.print("Enter your choice: ");
int choice = -1;
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (Exception ignored) {
}
switch (choice) {
case 1:
// Create a balanced BST from a sorted array
int[] data = {1, 2, 3, 4, 5, 6, 7};
bst.createBalancedBST(data);
System.out.println("Binary search tree created.");
break;
case 2:
// Insert a new node into the BST
System.out.print("Enter value to insert: ");
int valueToInsert = Integer.parseInt(scanner.nextLine());
bst.insert(valueToInsert);
System.out.println("Node inserted.");
break;
case 3:
// Delete a node from the BST
System.out.print("Enter value to delete: ");
int valueToDelete = Integer.parseInt(scanner.nextLine());
bst.delete(valueToDelete);
System.out.println("Node deleted (if it existed).");
break;
case 4:
// Print the BST in InOrder traversal
bst.printInOrder();
break;
case 5:
// Print the BST in PreOrder traversal
bst.printPreOrder();
break;
case 6:
// Print the BST in PostOrder traversal
bst.printPostOrder();
break;
case 7:
// Exit the program
System.out.println("Exiting program.");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+105
View File
@@ -0,0 +1,105 @@
import java.util.*;
public class TheStates {
// Let's first create a list of all states.
private static final String[] STATES = {
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
"New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia",
"Wisconsin", "Wyoming"
};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
while (true) {
System.out.println("");
System.out.println("Menu:");
System.out.println("1 - Display the text");
System.out.println("2 - Search");
System.out.println("3 - Exit program");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
System.out.println("\nText containing all states:\n");
System.out.println(STATESLIST);
break;
case 2:
System.out.print("\nEnter a pattern to search: ");
String pattern = scanner.nextLine();
List<Integer> results = boyerBadCharSearch(STATESLIST, pattern);
if (results.isEmpty()) {
System.out.println("Pattern not found.");
} else {
System.out.println("Pattern found at indices: " + results);
}
break;
case 3:
System.out.println("Exiting program.");
scanner.close();
return;
default:
System.out.println("Invalid option. Try again.");
}
}
}
// Create a single string containing all state names separated by a dash
private static final String STATESLIST = String.join(" - ", STATES);
// Boyer-Moyer bad character search
public static List<Integer> boyerBadCharSearch(String text, String pattern) {
List<Integer> result = new ArrayList<>();
int[] badChar = buildBadCharTable(pattern);
int m = pattern.length();
int n = text.length();
int shift = 0;
while (shift <= (n - m)) {
int j = m - 1;
// Move from right to left of the pattern
while (j >= 0 && pattern.charAt(j) == text.charAt(shift + j))
j--;
if (j < 0) {
result.add(shift);
if (shift + m < text.length()) {
shift += m - badChar[text.charAt(shift + m)];
} else {
shift += 1;
}
} else {
shift += Math.max(1, j - badChar[text.charAt(shift + j)]);
}
}
return result;
}
// Build bad character table
public static int[] buildBadCharTable(String pattern) {
final int SIZE = 256;
int[] table = new int[SIZE];
Arrays.fill(table, -1);
for (int i = 0; i < pattern.length(); i++) {
table[pattern.charAt(i)] = i;
}
return table;
}
}
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
@@ -0,0 +1,7 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
</code_scheme>
</component>
@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ComputerScience201.iml" filepath="$PROJECT_DIR$/ComputerScience201.iml" />
</modules>
</component>
</project>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" />
</project>
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>