diff --git a/.github/workflows/codeql.yaml b/.github/workflows/codeql.yaml new file mode 100644 index 0000000..7a82612 --- /dev/null +++ b/.github/workflows/codeql.yaml @@ -0,0 +1,14 @@ +name: CodeQL analysis + +on: + push: + branches: [main] + pull_request: + branches: [main] + schedule: + - cron: '0 3 * * 0' + +jobs: + call-codeQL-analysis: + name: CodeQL analysis + uses: actions/reusable-workflows/.github/workflows/codeql-analysis.yml@main diff --git a/.github/workflows/label_pr.yaml b/.github/workflows/label_pr.yaml new file mode 100644 index 0000000..c1e6c07 --- /dev/null +++ b/.github/workflows/label_pr.yaml @@ -0,0 +1,22 @@ +# This workflow will triage pull requests and apply a label based on the +# paths that are modified in the pull request. +# +# To use this workflow, you will need to set up a .github/labeler.yml +# file with configuration. For more information, see: +# https://github.com/actions/labeler + +name: Pull Request Labeler +on: [pull_request_target] + +jobs: + label: + + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + + steps: + - uses: actions/labeler@v5 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/maven.yaml b/.github/workflows/maven.yaml new file mode 100644 index 0000000..2dc23ad --- /dev/null +++ b/.github/workflows/maven.yaml @@ -0,0 +1,36 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Java CI with Maven + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 23 + uses: actions/setup-java@v4 + with: + java-version: '23' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn -B package --file pom.xml + + # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive + - name: Update dependency graph + if: github.event_name == 'push' + uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 diff --git a/.github/workflows/pmd.yaml b/.github/workflows/pmd.yaml new file mode 100644 index 0000000..22f4573 --- /dev/null +++ b/.github/workflows/pmd.yaml @@ -0,0 +1,41 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: pmd + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + pmd-code-scan: + permissions: + contents: read # for actions/checkout to fetch code + security-events: write # for github/codeql-action/upload-sarif to upload SARIF results + actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 11 + uses: actions/setup-java@v4 + with: + java-version: '11' + distribution: 'temurin' + - name: Run PMD + id: pmd + uses: pmd/pmd-github-action@v2 + with: + rulesets: 'rulesets/java/quickstart.xml' + sourcePath: 'src/main/java' + analyzeModifiedFilesOnly: false + - name: Upload SARIF file + uses: github/codeql-action/upload-sarif@v3 + with: + sarif_file: pmd-report.sarif diff --git a/.github/workflows/summarize_new_issue.yaml b/.github/workflows/summarize_new_issue.yaml new file mode 100644 index 0000000..d072763 --- /dev/null +++ b/.github/workflows/summarize_new_issue.yaml @@ -0,0 +1,33 @@ +name: Summarize new issues + +on: + issues: + types: [opened] + +jobs: + summary: + runs-on: ubuntu-latest + permissions: + issues: write + contents: read + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Run AI inference + id: inference + uses: actions/ai-inference@v1 + with: + prompt: | + Summarize the following GitHub issue in one paragraph: + Title: ${{ github.event.issue.title }} + Body: ${{ github.event.issue.body }} + + - name: Comment with AI summary + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + RESPONSE: ${{ steps.inference.outputs.response }} + run: | + gh issue comment "$ISSUE_NUMBER" --body "$RESPONSE" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4057c31..3fc95da 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,7 +8,7 @@ Thank you for taking the time to contribute to this project! Here are a few guid 2. **Clone the repository**: Clone your forked repository to your local machine using the following command: ```sh - git clone https://github.com/your-username/java-alda.git + git clone https://github.com/your-username/java-dsa.git ``` 3. **Create a branch**: Create a new branch for your work using the following command: diff --git a/README.md b/README.md index d9c919a..3e89e94 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Java Algorithms and Data Structures +# Java Data Structures and Algorithms This repository contains implementations of various algorithms and data structures in Java. It is intended as a resource for learning and reference. @@ -94,11 +94,11 @@ Implementation and usage of graphs. 1. Clone the repository: ```sh - git clone https://github.com/bcExpt1123/java-alda.git + git clone https://github.com/bcExpt1123/java-dsa.git ``` 2. Navigate to the project directory: ```sh - cd java-alda + cd java-dsa ``` ## Usage @@ -107,11 +107,21 @@ Run code: ```sh mvn clean compile exec:java ``` + Run test: ```sh mvn test ``` +Run Check style: +```sh + mvn checkstyle:check +``` +Or +```sh + mvn checkstyle:checkstyle +``` + ## Contributing Contributions are welcome! Please read the [contributing guidelines](CONTRIBUTING.md) for more details. diff --git a/checkstyle.xml b/checkstyle.xml new file mode 100644 index 0000000..d41ff10 --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 8a76594..9f14d47 100644 --- a/pom.xml +++ b/pom.xml @@ -5,14 +5,14 @@ 4.0.0 org.example - algo-datastructure + java-dsa 1.0-SNAPSHOT 23 23 UTF-8 - org.alda.Main + org.dsa.Main @@ -28,5 +28,27 @@ test - + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.3.0 + + + verify + + check + + + + + checkstyle.xml + UTF-8 + true + true + + + + \ No newline at end of file diff --git a/src/main/java/org/alda/Main.java b/src/main/java/org/alda/Main.java deleted file mode 100644 index f9e7ba6..0000000 --- a/src/main/java/org/alda/Main.java +++ /dev/null @@ -1,236 +0,0 @@ -package org.alda; - -import org.alda.structure.linkedList.circular.CircularLinkedList; -import org.alda.structure.linkedList.deque.DoublyEndedList; -import org.alda.structure.linkedList.deque.IDoublyEndedList; -import org.alda.structure.linkedList.doubly.DoublyLinkedList; -import org.alda.structure.linkedList.simple.ISimpleLinkedList; -import org.alda.structure.linkedList.simple.Node; -import org.alda.structure.linkedList.simple.SimpleLinkedList; -import org.alda.structure.linkedList.sorted.ISortedLinkedList; -import org.alda.structure.linkedList.sorted.SortedLinkedList; -import org.alda.structure.queue.IQueue; -import org.alda.structure.queue.Queue; -import org.alda.structure.queue.array.QueueArray; -import org.alda.structure.queue.linkedList.QueueLinkedList; -import org.alda.structure.queue.priority.IPriorityQueue; -import org.alda.structure.queue.priority.PriorityQueue; -import org.alda.structure.stack.array.IStackArray; -import org.alda.structure.stack.array.StackArray; -import org.alda.structure.stack.linkedList.IStackLinkedList; -import org.alda.structure.stack.linkedList.StackLinkedList; -import org.alda.structure.tree.bst.BinarySearchTree; -import org.alda.structure.tree.bst.bbt.AVL; - -import java.util.Arrays; -import java.util.List; - -public class Main { - public static void main(String[] args) { - testBST(); - } - public static void testSimpleLinkedList() { - ISimpleLinkedList intList = new SimpleLinkedList(); - intList.append(2); - intList.append(3); - intList.append(4); - intList.append(5); - - intList.insertAfter(intList.getHead(), 22); - - Node head = intList.delete(3); - - intList.print(); - - head.print(); - - System.out.println(); - System.out.println(intList.search(2)); - System.out.println(intList.search(3)); - System.out.println(intList.search(4)); - System.out.println(intList.search(5)); - } - - public static void testDoublyLinkedList() { - DoublyLinkedList intList = new DoublyLinkedList(); - intList.append(2); - intList.append(3); - intList.append(4); - intList.append(5); - - intList.print(); - } - - public static void testCircularLinkedList() { - CircularLinkedList intList = new CircularLinkedList(); - - intList.append(2); - intList.append(3); - intList.append(4); - intList.append(5); - - intList.print(); - } - - public static void testSortedLinkedList() { - ISortedLinkedList intList = new SortedLinkedList(); - - intList.sortedInsert(10); - intList.sortedInsert(50); - intList.sortedInsert(30); - intList.sortedInsert(70); - intList.sortedInsert(20); - - intList.print(); - } - - public static void testDoublyEndedList(){ - IDoublyEndedList intList = new DoublyEndedList<>(); - - intList.appendEnd(1); - intList.appendEnd(2); - intList.appendFront(3); - intList.appendFront(4); - intList.appendEnd(5); - intList.appendFront(6); - intList.appendEnd(7); - - intList.print(); - } - - public static void testStackArray(){ - IStackArray intStack = new StackArray<>(); - - intStack.push(1); - intStack.push(2); - intStack.push(3); - - System.out.println(intStack.pop()); - System.out.println(intStack.peek()); - System.out.println(intStack.pop()); - } - - public static void testStackLinkedList(){ - IStackLinkedList intStack = new StackLinkedList<>(); - - intStack.push(1); - intStack.push(2); - intStack.push(3); - - System.out.println(intStack.pop()); - System.out.println(intStack.peek()); - System.out.println(intStack.pop()); - } - - public static void testQueue(){ - IQueue intQueue = new Queue<>(); - - intQueue.enqueue(1); - intQueue.enqueue(2); - intQueue.enqueue(3); - intQueue.enqueue(4); - intQueue.enqueue(5); - - Integer item = intQueue.dequeue(); - Integer item1 = intQueue.dequeue(); - Integer item2 = intQueue.dequeue(); - - Integer front = intQueue.front(); - Integer rear = intQueue.rear(); - - System.out.println(item); - System.out.println(item1); - System.out.println(item2); - System.out.println(front); - System.out.println(rear); - } - - public static void testQueueArray(){ - IQueue intQueue = new QueueArray<>(); - - intQueue.enqueue(1); - intQueue.enqueue(2); - intQueue.enqueue(3); - intQueue.enqueue(4); - intQueue.enqueue(5); - - Integer item = intQueue.dequeue(); - Integer item1 = intQueue.dequeue(); - Integer item2 = intQueue.dequeue(); - - Integer front = intQueue.front(); - Integer rear = intQueue.rear(); - - System.out.println(item); - System.out.println(item1); - System.out.println(item2); - System.out.println(front); - System.out.println(rear); - } - - public static void testQueueLinkedList(){ - IQueue intQueue = new QueueLinkedList<>(); - - intQueue.enqueue(1); - intQueue.enqueue(2); - intQueue.enqueue(3); - intQueue.enqueue(4); - intQueue.enqueue(5); - - Integer item = intQueue.dequeue(); - Integer item1 = intQueue.dequeue(); - Integer item2 = intQueue.dequeue(); - - Integer front = intQueue.front(); - Integer rear = intQueue.rear(); - - System.out.println(item); - System.out.println(item1); - System.out.println(item2); - System.out.println(front); - System.out.println(rear); - } - - public static void testPriorityQueue(){ - IPriorityQueue intQueue = new PriorityQueue<>(); - - intQueue.enqueue(2, 1); - intQueue.enqueue(32, 25); - intQueue.enqueue(48, 0); - intQueue.enqueue(15, 3); - intQueue.enqueue(36, 2); - intQueue.enqueue(27, 4); - - Integer item = intQueue.dequeue(); - Integer item1 = intQueue.dequeue(); - Integer item2 = intQueue.dequeue(); - Integer item3 = intQueue.dequeue(); - - System.out.println(item); - System.out.println(item1); - System.out.println(item2); - System.out.println(item3); - } - - public static void testBST(){ - BinarySearchTree bst = new BinarySearchTree<>(); - bst.insert(5); - bst.insert(3); - bst.insert(7); - bst.insert(1); - bst.insert(20); - bst.insert(4); - - System.out.println(bst.inorderTraversal()); - System.out.println(bst.search(4)); - } - - public static void testAVL(){ - AVL avl = new AVL<>(); - AVL.Node root = null; - List keys = Arrays.asList(10, 20, 30, 40, 50, 25); - for(Integer key : keys){ - avl.insert(root, key); - } - } -} \ No newline at end of file diff --git a/src/main/java/org/alda/structure/tree/bst/Node.java b/src/main/java/org/alda/structure/tree/bst/Node.java deleted file mode 100644 index 5e2e701..0000000 --- a/src/main/java/org/alda/structure/tree/bst/Node.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.alda.structure.tree.bst; - -/** - * Node for binary search tree - * @param - */ -public class Node { - T key; - Node left; - Node right; - public Node(T key){ - this.key = key; - left = null; - right = null; - } -} diff --git a/src/main/java/org/dsa/Main.java b/src/main/java/org/dsa/Main.java new file mode 100644 index 0000000..4dbcd97 --- /dev/null +++ b/src/main/java/org/dsa/Main.java @@ -0,0 +1,516 @@ +package org.dsa; + +import org.dsa.structure.linkedList.circular.CircularLinkedList; +import org.dsa.structure.linkedList.deque.DoublyEndedList; +import org.dsa.structure.linkedList.deque.IDoublyEndedList; +import org.dsa.structure.linkedList.doubly.DoublyLinkedList; +import org.dsa.structure.linkedList.simple.ISimpleLinkedList; +import org.dsa.structure.linkedList.simple.Node; +import org.dsa.structure.linkedList.simple.SimpleLinkedList; +import org.dsa.structure.linkedList.sorted.ISortedLinkedList; +import org.dsa.structure.linkedList.sorted.SortedLinkedList; +import org.dsa.structure.queue.IQueue; +import org.dsa.structure.queue.Queue; +import org.dsa.structure.queue.array.QueueArray; +import org.dsa.structure.queue.linkedList.QueueLinkedList; +import org.dsa.structure.queue.priority.IPriorityQueue; +import org.dsa.structure.queue.priority.PriorityQueue; +import org.dsa.structure.stack.array.IStackArray; +import org.dsa.structure.stack.array.StackArray; +import org.dsa.structure.stack.linkedList.IStackLinkedList; +import org.dsa.structure.stack.linkedList.StackLinkedList; +import org.dsa.structure.tree.bst.BinarySearchTree; +import org.dsa.structure.tree.bst.bbt.AVLTest; + +import java.util.Scanner; + +/** + * Main class demonstrating various data structures with an interactive menu. + */ +public class Main { + /** + * Launches the interactive console application for demonstrating various data structures. + * + * Displays a main menu allowing users to select and explore different data structure categories, each with its own submenu and test demonstrations. The application continues running until the user chooses to exit. + * + * @param args command-line arguments (not used) + */ + public static void main(String[] args) { + try (Scanner scanner = new Scanner(System.in)) { + boolean running = true; + + while (running) { + displayMainMenu(); + int choice = getMenuChoice(scanner, 5); + + switch (choice) { + case 1 -> linkedListMenu(scanner); + case 2 -> stackMenu(scanner); + case 3 -> queueMenu(scanner); + case 4 -> treeMenu(scanner); + case 5 -> { + System.out.println("\nThank you for using Data Structure Demo!"); + running = false; + } + } + } + } + } + /** + * Displays the main menu options for selecting a data structure category or exiting the program. + */ + private static void displayMainMenu() { + System.out.println("\n=== Data Structure Demonstration ==="); + System.out.println("1. Linked Lists"); + System.out.println("2. Stacks"); + System.out.println("3. Queues"); + System.out.println("4. Trees"); + System.out.println("5. Exit"); + System.out.print("\nEnter your choice (1-5): "); + } + + /** + * Display a submenu to select and run linked list demonstrations. + * + * Loops until the user selects "Back to Main Menu". For each valid selection, invokes the corresponding linked list test and waits for the user to press Enter before showing the submenu again. + */ + private static void linkedListMenu(Scanner scanner) { + while (true) { + System.out.println("\n=== Linked List Types ==="); + System.out.println("1. Simple Linked List"); + System.out.println("2. Doubly Linked List"); + System.out.println("3. Circular Linked List"); + System.out.println("4. Sorted Linked List"); + System.out.println("5. Doubly Ended List (Deque)"); + System.out.println("6. Back to Main Menu"); + System.out.print("\nEnter your choice (1-6): "); + + int choice = getMenuChoice(scanner, 6); + if (choice == 6) break; + + System.out.println("\n=== Test Results ==="); + switch (choice) { + case 1 -> testSimpleLinkedList(); + case 2 -> testDoublyLinkedList(); + case 3 -> testCircularLinkedList(); + case 4 -> testSortedLinkedList(); + case 5 -> testDoublyEndedList(); + } + pressEnterToContinue(scanner); + } + } + + /** + * Displays an interactive submenu for selecting and testing different stack implementations. + * + * Allows the user to choose between array-based and linked list-based stacks, runs the corresponding test demonstration, and returns to the main menu upon request. + */ + private static void stackMenu(Scanner scanner) { + while (true) { + System.out.println("\n=== Stack Types ==="); + System.out.println("1. Array-based Stack"); + System.out.println("2. Linked List-based Stack"); + System.out.println("3. Back to Main Menu"); + System.out.print("\nEnter your choice (1-3): "); + + int choice = getMenuChoice(scanner, 3); + if (choice == 3) break; + + System.out.println("\n=== Test Results ==="); + switch (choice) { + case 1 -> testStackArray(); + case 2 -> testStackLinkedList(); + } + pressEnterToContinue(scanner); + } + } + + /** + * Displays an interactive submenu for selecting and testing different queue implementations. + * + * Presents options for simple queue, array-based queue, linked list-based queue, and priority queue. + * Executes the corresponding test method based on user selection and waits for user input before returning to the submenu. + */ + private static void queueMenu(Scanner scanner) { + while (true) { + System.out.println("\n=== Queue Types ==="); + System.out.println("1. Simple Queue"); + System.out.println("2. Array-based Queue"); + System.out.println("3. Linked List-based Queue"); + System.out.println("4. Priority Queue"); + System.out.println("5. Back to Main Menu"); + System.out.print("\nEnter your choice (1-5): "); + + int choice = getMenuChoice(scanner, 5); + if (choice == 5) break; + + System.out.println("\n=== Test Results ==="); + switch (choice) { + case 1 -> testQueue(); + case 2 -> testQueueArray(); + case 3 -> testQueueLinkedList(); + case 4 -> testPriorityQueue(); + } + pressEnterToContinue(scanner); + } + } + + /** + * Displays a submenu for tree data structure demonstrations and runs the selected test. + * + * Presents options for Binary Search Tree and AVL Tree, executes the corresponding test method based on user input, and waits for user confirmation before returning. + */ + private static void treeMenu(Scanner scanner) { + System.out.println("\n=== Tree Types ==="); + System.out.println("1. Binary Search Tree"); + System.out.println("2. AVL Tree"); + System.out.println("3. Back to Main Menu"); + System.out.print("\nEnter your choice (1-3): "); + + int choice = getMenuChoice(scanner, 3); + if (choice == 3) return; + + System.out.println("\n=== Test Results ==="); + if (choice == 1) testBST(); + else if (choice == 2) testAVL(); + + pressEnterToContinue(scanner); + } + + /** + * Prompts the user to enter a menu choice and returns a valid integer within the specified range. + * + * @param max the maximum valid menu option (inclusive) + * @return the user's validated menu choice as an integer between 1 and {@code max} + */ + private static int getMenuChoice(Scanner scanner, int max) { + while (true) { + try { + int choice = Integer.parseInt(scanner.nextLine().trim()); + if (choice >= 1 && choice <= max) { + return choice; + } + System.out.printf("Please enter a number between 1 and %d: ", max); + } catch (NumberFormatException e) { + System.out.print("Invalid input. Please enter a number: "); + } + } + } + + /** + * Prompts the user to press Enter and waits for input before continuing. + */ + private static void pressEnterToContinue(Scanner scanner) { + System.out.print("\nPress Enter to continue..."); + scanner.nextLine(); + } + + /** + * Demonstrates basic operations on a simple linked list of integers. + * + * Performs appends, an insertion after the head, a deletion, prints the list and head node, and prints search results for selected values. + */ + public static void testSimpleLinkedList() { + System.out.println("\n=== Initialize Simple Linked List ==="); + ISimpleLinkedList intList = new SimpleLinkedList(); + System.out.println("Actions: \n- Append 2, 3, 4, 5"); + intList.append(2); + intList.append(3); + intList.append(4); + intList.append(5); + + System.out.println("- Insert after Head with 22"); + intList.insertAfter(intList.getHead(), 22); + + System.out.println("- Delete 3"); + Node head = intList.delete(3); + + System.out.print("\nPrint List:"); + intList.print(); + + System.out.print("\n\nHead:"); + head.print(); + + System.out.println("\n\nSearch for 2: " + intList.search(2)); + System.out.println("Search for 3: " + intList.search(3)); + System.out.println("Search for 4: " + intList.search(4)); + System.out.println("Search for 5: " + intList.search(5)); + } + + /** + * Demonstrates basic operations on a doubly linked list of integers. + * + * Initializes a doubly linked list, appends several integer values, and prints the list in forward order. + */ + public static void testDoublyLinkedList() { + System.out.println("\n=== Initialize Doubly Linked List ==="); + DoublyLinkedList intList = new DoublyLinkedList(); + + System.out.println("Actions: \nAppend 2, 3, 4, 5"); + intList.append(2); + intList.append(3); + intList.append(4); + intList.append(5); + + System.out.print("\nPrint List (Forward):"); + intList.print(); + } + + /** + * Demonstrates basic operations on a circular linked list of integers. + * + * Initializes a circular linked list, appends several integer values, and prints the list contents. + */ + public static void testCircularLinkedList() { + System.out.println("\n=== Initialize Circular Linked List ==="); + CircularLinkedList intList = new CircularLinkedList(); + + System.out.println("Actions: \nAppend 2, 3, 4, 5"); + intList.append(2); + intList.append(3); + intList.append(4); + intList.append(5); + + System.out.print("\nPrint List (Forward):"); + intList.print(); + } + + /** + * Demonstrates the usage of a sorted linked list by inserting integers in non-sorted order and printing the sorted result. + * + * Initializes a sorted linked list, inserts several integer values using sorted insertion, and prints the list to show the maintained order. + */ + public static void testSortedLinkedList() { + System.out.println("\n=== Initialize Sorted Linked List ==="); + ISortedLinkedList intList = new SortedLinkedList(); + + System.out.println("Actions: \nSorted Insert 10, 50, 30, 70, 20"); + intList.sortedInsert(10); + intList.sortedInsert(50); + intList.sortedInsert(30); + intList.sortedInsert(70); + intList.sortedInsert(20); + + System.out.print("\nPrint List (Forward):"); + intList.print(); + } + + /** + * Demonstrates insertions at both front and end on a doubly ended linked list of integers. + * + * Initializes a doubly-ended list, performs a sequence of appendFront and appendEnd operations, + * and prints the list in forward order. + */ + public static void testDoublyEndedList(){ + System.out.println("\n=== Initialize Doubly Ended Linked List ==="); + IDoublyEndedList intList = new DoublyEndedList<>(); + + System.out.println("Actions: \nAppend 1, 2, 3, 4, 5, 6, 7"); + intList.appendEnd(1); + intList.appendEnd(2); + intList.appendFront(3); + intList.appendFront(4); + intList.appendEnd(5); + intList.appendFront(6); + intList.appendEnd(7); + + System.out.print("\nPrint List (Forward):"); + intList.print(); + } + + /** + * Demonstrates basic operations on an array-based stack of integers. + * + * Initializes a stack, pushes several values, then performs pop and peek operations while printing the results. + */ + public static void testStackArray(){ + System.out.println("\n=== Initialize Stack Array ==="); + IStackArray intStack = new StackArray<>(); + + System.out.println("Actions: \nPush 1, 2, 3"); + intStack.push(1); + intStack.push(2); + intStack.push(3); + + System.out.println("\nPop: " + intStack.pop()); + System.out.println("Peek: " + intStack.peek()); + System.out.println("Pop: " + intStack.pop()); + } + + /** + * Demonstrates stack operations using a linked list implementation. + * + * Initializes a linked list-based stack, performs push, pop, and peek operations, and prints the results to the console. + */ + public static void testStackLinkedList(){ + System.out.println("\n=== Initialize Stack Linked List ==="); + IStackLinkedList intStack = new StackLinkedList<>(); + + System.out.println("Actions: \nPush 1, 2, 3"); + intStack.push(1); + intStack.push(2); + intStack.push(3); + + System.out.println("\nPop: " + intStack.pop()); + System.out.println("Peek: " + intStack.peek()); + System.out.println("Pop: " + intStack.pop()); + } + + /** + * Demonstrates basic queue operations including enqueue, dequeue, and retrieving front and rear elements. + * + * Initializes a queue of integers, enqueues several values, dequeues three elements, and prints the results along with the current front and rear values. + */ + public static void testQueue(){ + System.out.println("\n=== Initialize Queue ==="); + IQueue intQueue = new Queue<>(); + + System.out.println("Actions: \nEnqueue 1, 2, 3, 4, 5"); + intQueue.enqueue(1); + intQueue.enqueue(2); + intQueue.enqueue(3); + intQueue.enqueue(4); + intQueue.enqueue(5); + + Integer item = intQueue.dequeue(); + Integer item1 = intQueue.dequeue(); + Integer item2 = intQueue.dequeue(); + + Integer front = intQueue.front(); + Integer rear = intQueue.rear(); + + System.out.println("\nDequeue: " + item); + System.out.println("Dequeue: " + item1); + System.out.println("Dequeue: " + item2); + System.out.println("Front: " + front); + System.out.println("Rear: " + rear); + } + + /** + * Demonstrates the usage of an array-based queue by performing enqueue, dequeue, and inspection operations. + * + * Initializes an integer queue, enqueues several elements, dequeues three elements, and prints the dequeued values along with the current front and rear elements. + */ + public static void testQueueArray(){ + System.out.println("\n=== Initialize Queue Array ==="); + IQueue intQueue = new QueueArray<>(); + + System.out.println("Actions: \nEnqueue 1, 2, 3, 4, 5"); + intQueue.enqueue(1); + intQueue.enqueue(2); + intQueue.enqueue(3); + intQueue.enqueue(4); + intQueue.enqueue(5); + + Integer item = intQueue.dequeue(); + Integer item1 = intQueue.dequeue(); + Integer item2 = intQueue.dequeue(); + + Integer front = intQueue.front(); + Integer rear = intQueue.rear(); + + System.out.println("\nDequeue: " + item); + System.out.println("Dequeue: " + item1); + System.out.println("Dequeue: " + item2); + System.out.println("Front: " + front); + System.out.println("Rear: " + rear); + } + + /** + * Demonstrates the usage of a linked list-based queue by performing enqueue, dequeue, and inspection operations. + * + * Initializes a queue, enqueues several integers, dequeues multiple elements, and prints the results along with the current front and rear elements. + */ + public static void testQueueLinkedList(){ + System.out.println("\n=== Initialize Queue Linked List ==="); + IQueue intQueue = new QueueLinkedList<>(); + + System.out.println("Actions: \nEnqueue 1, 2, 3, 4, 5"); + intQueue.enqueue(1); + intQueue.enqueue(2); + intQueue.enqueue(3); + intQueue.enqueue(4); + intQueue.enqueue(5); + + Integer item = intQueue.dequeue(); + Integer item1 = intQueue.dequeue(); + Integer item2 = intQueue.dequeue(); + + Integer front = intQueue.front(); + Integer rear = intQueue.rear(); + + System.out.println("\nDequeue: " + item); + System.out.println("Dequeue: " + item1); + System.out.println("Dequeue: " + item2); + System.out.println("Front: " + front); + System.out.println("Rear: " + rear); + } + + /** + * Demonstrates priority-based ordering by enqueuing integer elements with associated priorities and dequeuing several elements to show removal order. + * + * This method prints the enqueue actions and the first four dequeued values to illustrate how the priority queue selects items. + */ + public static void testPriorityQueue(){ + System.out.println("\n=== Initialize Priority Queue ==="); + IPriorityQueue intQueue = new PriorityQueue<>(); + + System.out.println("Actions: \n- Enqueue 2 with priority 1"); + intQueue.enqueue(2, 1); + System.out.println("- Enqueue 32 with priority 25"); + intQueue.enqueue(32, 25); + System.out.println("- Enqueue 48 with priority 0"); + intQueue.enqueue(48, 0); + System.out.println("- Enqueue 15 with priority 3"); + intQueue.enqueue(15, 3); + System.out.println("- Enqueue 36 with priority 2"); + intQueue.enqueue(36, 2); + System.out.println("- Enqueue 27 with priority 4"); + intQueue.enqueue(27, 4); + + Integer item = intQueue.dequeue(); + Integer item1 = intQueue.dequeue(); + Integer item2 = intQueue.dequeue(); + Integer item3 = intQueue.dequeue(); + + System.out.println("\nDequeue: " + item); + System.out.println("Dequeue: " + item1); + System.out.println("Dequeue: " + item2); + System.out.println("Dequeue: " + item3); + } + + /** + * Demonstrates insertion, inorder traversal, and search on a binary search tree. + * + * Builds a BinarySearchTree, inserts the values 5, 3, 7, 1, 20, and 4, + * prints the tree's inorder traversal, and prints whether the value 4 is found. + */ + public static void testBST(){ + System.out.println("\n=== Initialize Binary Search Tree ==="); + BinarySearchTree bst = new BinarySearchTree<>(); + + System.out.println("Actions: \n- Insert 5, 3, 7, 1, 20, 4"); + bst.insert(5); + bst.insert(3); + bst.insert(7); + bst.insert(1); + bst.insert(20); + bst.insert(4); + + System.out.println("- Inorder Traversal "); + System.out.println(bst.inorderTraversal()); + System.out.print("\nSearch for 4: "); + org.dsa.structure.tree.bst.Node node = bst.search(4); + if (node != null) System.out.println("true"); + else System.out.println("false"); + } + + /** + * Demonstrates insertion of multiple integers into an AVL tree. + * + * Initializes an AVL tree and inserts the values 10, 20, 30, 40, 50, and 25 to showcase automatic balancing during insertion. + */ + public static void testAVL(){ + AVLTest.main(null); + } +} \ No newline at end of file diff --git a/src/main/java/org/alda/common/Comparer.java b/src/main/java/org/dsa/common/Comparer.java similarity index 99% rename from src/main/java/org/alda/common/Comparer.java rename to src/main/java/org/dsa/common/Comparer.java index 3977588..8977a4f 100644 --- a/src/main/java/org/alda/common/Comparer.java +++ b/src/main/java/org/dsa/common/Comparer.java @@ -1,4 +1,4 @@ -package org.alda.common; +package org.dsa.common; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/common/Printable.java b/src/main/java/org/dsa/common/Printable.java similarity index 96% rename from src/main/java/org/alda/common/Printable.java rename to src/main/java/org/dsa/common/Printable.java index 47a1aeb..bbc2c81 100644 --- a/src/main/java/org/alda/common/Printable.java +++ b/src/main/java/org/dsa/common/Printable.java @@ -1,4 +1,4 @@ -package org.alda.common; +package org.dsa.common; /** * @author bcExpt1123 diff --git a/src/main/java/org/dsa/common/Utils.java b/src/main/java/org/dsa/common/Utils.java new file mode 100644 index 0000000..d7733cc --- /dev/null +++ b/src/main/java/org/dsa/common/Utils.java @@ -0,0 +1,29 @@ +package org.dsa.common; + +import java.util.Arrays; + +public class Utils { + /** + * Join the elements of an int array into a single string using the specified delimiter. + * + * @param arr the array of integers to join + * @param delimiter the string placed between adjacent elements in the result + * @return the array elements separated by the delimiter; an empty string if the array has no elements + */ + public static String intArrToStr(int[] arr, String delimiter) { + return Arrays.stream(arr) + .mapToObj(String::valueOf) + .reduce((a, b) -> a + delimiter + b) + .orElse(""); + } + + /** + * Convert an integer array into a single string with elements separated by ", ". + * + * @param arr the integers to join + * @return a string with the array elements separated by ", ", or an empty string if the array is empty + */ + public static String intArrToStr(int[] arr) { + return intArrToStr(arr, ", "); + } +} diff --git a/src/main/java/org/alda/sample/stack/BalancedParenthesesCheck.java b/src/main/java/org/dsa/sample/stack/BalancedParenthesesCheck.java similarity index 97% rename from src/main/java/org/alda/sample/stack/BalancedParenthesesCheck.java rename to src/main/java/org/dsa/sample/stack/BalancedParenthesesCheck.java index a7ed29f..f8313c8 100644 --- a/src/main/java/org/alda/sample/stack/BalancedParenthesesCheck.java +++ b/src/main/java/org/dsa/sample/stack/BalancedParenthesesCheck.java @@ -1,4 +1,4 @@ -package org.alda.sample.stack; +package org.dsa.sample.stack; import java.util.ArrayList; import java.util.HashMap; diff --git a/src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java b/src/main/java/org/dsa/structure/linkedList/circular/CircularLinkedList.java similarity index 87% rename from src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java rename to src/main/java/org/dsa/structure/linkedList/circular/CircularLinkedList.java index 060ee78..eb4fbf0 100644 --- a/src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java +++ b/src/main/java/org/dsa/structure/linkedList/circular/CircularLinkedList.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.circular; +package org.dsa.structure.linkedList.circular; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 @@ -63,14 +63,13 @@ public void append(T data) { } /** - * Prints the data stored in each node of the circular linked list. - * The traversal stops when the list has looped back to the head. + * Prints the data of each node in the circular linked list in sequence, starting from the head. + * Traversal continues until the list loops back to the head node or if the list is empty. */ public void print() { Node current = head; while (current != null) { current.print(); - System.out.println("\n"); current = current.next; if(current == head) { break; diff --git a/src/main/java/org/alda/structure/linkedList/circular/Node.java b/src/main/java/org/dsa/structure/linkedList/circular/Node.java similarity index 80% rename from src/main/java/org/alda/structure/linkedList/circular/Node.java rename to src/main/java/org/dsa/structure/linkedList/circular/Node.java index f6f7cc6..b351797 100644 --- a/src/main/java/org/alda/structure/linkedList/circular/Node.java +++ b/src/main/java/org/dsa/structure/linkedList/circular/Node.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.circular; +package org.dsa.structure.linkedList.circular; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 @@ -49,11 +49,12 @@ public Node(T data) { } /** - * Prints the data stored in this node and, if present, the data in the next node. - * The output format is: {@code Data: , Next: } + * Prints this node's data and, if available, the data of the next node to standard output. + * + * The output begins with a newline and follows the format: {@code Data: , Next: }. */ public void print() { - System.out.print("Data: " + data); + System.out.print("\nData: " + data); if (next != null) { System.out.print(", Next: " + next.data); } diff --git a/src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java b/src/main/java/org/dsa/structure/linkedList/deque/DoublyEndedList.java similarity index 86% rename from src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java rename to src/main/java/org/dsa/structure/linkedList/deque/DoublyEndedList.java index 12e69a4..9ad6e0c 100644 --- a/src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java +++ b/src/main/java/org/dsa/structure/linkedList/deque/DoublyEndedList.java @@ -1,4 +1,4 @@ -package org.alda.structure.linkedList.deque; +package org.dsa.structure.linkedList.deque; /** * @author bcExpt1123 @@ -63,19 +63,22 @@ public Node getTail() { return tail; } + /** + * Set the list's tail node. + * + * @param tail the node to use as the new last element of the list, or `null` to indicate there is no tail + */ public void setTail(Node tail) { this.tail = tail; } /** - * Prints the contents of the list. - * Each node's data is printed, followed by a new line. + * Prints the data of each node in the list from head to tail. */ public void print() { Node current = head; while (current != null) { current.print(); - System.out.println("\n"); current = current.next; } } diff --git a/src/main/java/org/alda/structure/linkedList/deque/IDoublyEndedList.java b/src/main/java/org/dsa/structure/linkedList/deque/IDoublyEndedList.java similarity index 94% rename from src/main/java/org/alda/structure/linkedList/deque/IDoublyEndedList.java rename to src/main/java/org/dsa/structure/linkedList/deque/IDoublyEndedList.java index a1d262e..958ef32 100644 --- a/src/main/java/org/alda/structure/linkedList/deque/IDoublyEndedList.java +++ b/src/main/java/org/dsa/structure/linkedList/deque/IDoublyEndedList.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.deque; +package org.dsa.structure.linkedList.deque; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/linkedList/deque/Node.java b/src/main/java/org/dsa/structure/linkedList/deque/Node.java similarity index 70% rename from src/main/java/org/alda/structure/linkedList/deque/Node.java rename to src/main/java/org/dsa/structure/linkedList/deque/Node.java index 706c863..38e5277 100644 --- a/src/main/java/org/alda/structure/linkedList/deque/Node.java +++ b/src/main/java/org/dsa/structure/linkedList/deque/Node.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.deque; +package org.dsa.structure.linkedList.deque; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 @@ -44,19 +44,22 @@ public Node(T data){ } /** - * Constructs a new {@code Node} with {@code null} data. This is a default constructor - * that calls the main constructor with {@code null} as the data. + * Creates a new Node whose stored data is {@code null}. + * + * The created node's {@code next} and {@code prev} references are {@code null}. */ public Node(){ this(null); } /** - * Prints the data stored in this node, and if present, the data in the next and previous nodes. - * The output format is: {@code Data: , Next: , Prev: } + * Prints this node's data, along with the data of adjacent nodes if available, to standard output. + * + * The output includes the node's data, and, if present, the data of the next and previous nodes in the format: + * {@code Data: , Next: , Prev: }. */ public void print(){ - System.out.print("Data: " + data); + System.out.print("\nData: " + data); if (next != null) { System.out.print(", Next: " + next.data); } diff --git a/src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java b/src/main/java/org/dsa/structure/linkedList/doubly/DoublyLinkedList.java similarity index 87% rename from src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java rename to src/main/java/org/dsa/structure/linkedList/doubly/DoublyLinkedList.java index a221db6..191f5bf 100644 --- a/src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java +++ b/src/main/java/org/dsa/structure/linkedList/doubly/DoublyLinkedList.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.doubly; +package org.dsa.structure.linkedList.doubly; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 @@ -78,14 +78,14 @@ public void prepend(T data) { } /** - * Prints the data stored in each node of the doubly linked list from head to tail. - * Each node's data is printed along with the data in its next and previous nodes, if available. + * Traverses the list from head to tail and prints each node's data. + * + * The traversal invokes each node's print method in sequence. */ public void print() { Node current = head; while (current != null) { current.print(); - System.out.println("\n"); current = current.next; } } diff --git a/src/main/java/org/alda/structure/linkedList/doubly/Node.java b/src/main/java/org/dsa/structure/linkedList/doubly/Node.java similarity index 65% rename from src/main/java/org/alda/structure/linkedList/doubly/Node.java rename to src/main/java/org/dsa/structure/linkedList/doubly/Node.java index 3604b71..afa6213 100644 --- a/src/main/java/org/alda/structure/linkedList/doubly/Node.java +++ b/src/main/java/org/dsa/structure/linkedList/doubly/Node.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.doubly; +package org.dsa.structure.linkedList.doubly; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 @@ -32,10 +32,9 @@ public class Node implements Printable { public Node prev; /** - * Constructs a new {@code Node} with the specified data. - * The {@code next} and {@code prev} references are initialized to {@code null}. + * Create a new Node holding the specified data with its adjacent references set to null. * - * @param data The data to store in the node. + * @param data the value to store in this node */ public Node(final T data) { this.data = data; @@ -44,11 +43,14 @@ public Node(final T data) { } /** - * Prints the data stored in this node, as well as the data in the next and previous nodes, if available. - * The output format is: {@code Data: , Next: , Prev: } + * Prints this node's data, along with the data of adjacent nodes if they exist. + * + * The output begins with a newline and follows the format: + * {@code Data: , Next: , Prev: }. + * The "Next" and "Prev" fields are included only if the corresponding nodes are present. */ public void print() { - System.out.print("Data: " + data); + System.out.print("\nData: " + data); if (next != null) { System.out.print(", Next: " + next.data); } diff --git a/src/main/java/org/alda/structure/linkedList/simple/ISimpleLinkedList.java b/src/main/java/org/dsa/structure/linkedList/simple/ISimpleLinkedList.java similarity index 95% rename from src/main/java/org/alda/structure/linkedList/simple/ISimpleLinkedList.java rename to src/main/java/org/dsa/structure/linkedList/simple/ISimpleLinkedList.java index 93bb6d4..70cbec7 100644 --- a/src/main/java/org/alda/structure/linkedList/simple/ISimpleLinkedList.java +++ b/src/main/java/org/dsa/structure/linkedList/simple/ISimpleLinkedList.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.simple; +package org.dsa.structure.linkedList.simple; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/linkedList/simple/Node.java b/src/main/java/org/dsa/structure/linkedList/simple/Node.java similarity index 67% rename from src/main/java/org/alda/structure/linkedList/simple/Node.java rename to src/main/java/org/dsa/structure/linkedList/simple/Node.java index c654082..4a1a19d 100644 --- a/src/main/java/org/alda/structure/linkedList/simple/Node.java +++ b/src/main/java/org/dsa/structure/linkedList/simple/Node.java @@ -1,6 +1,6 @@ -package org.alda.structure.linkedList.simple; +package org.dsa.structure.linkedList.simple; -import org.alda.common.Printable; +import org.dsa.common.Printable; /** * @author bcExpt1123 @@ -26,10 +26,11 @@ public class Node implements Printable { public Node next; /** - * Constructs a new {@code Node} with the specified data. + * Create a new Node containing the specified data. + * * The {@code next} reference is initialized to {@code null}. * - * @param data The data to store in the node. + * @param data the data element to store in the node */ public Node(T data) { this.data = data; @@ -37,11 +38,12 @@ public Node(T data) { } /** - * Prints the data stored in this node and, if present, the data in the next node. - * The output format is: {@code Data: , Next: } + * Prints the data of this node, and if a next node exists, also prints its data. + * + * The output is formatted as: {@code Data: , Next: }, with a preceding newline. */ public void print() { - System.out.print("Data: " + data); + System.out.print("\nData: " + data); if(next != null) { System.out.print(", Next: " + next.data); } diff --git a/src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java b/src/main/java/org/dsa/structure/linkedList/simple/SimpleLinkedList.java similarity index 95% rename from src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java rename to src/main/java/org/dsa/structure/linkedList/simple/SimpleLinkedList.java index 6a37658..012d4d9 100644 --- a/src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java +++ b/src/main/java/org/dsa/structure/linkedList/simple/SimpleLinkedList.java @@ -1,4 +1,4 @@ -package org.alda.structure.linkedList.simple; +package org.dsa.structure.linkedList.simple; /** * @author bcExpt1123 @@ -136,13 +136,12 @@ public void setHead(Node head) { } /** - * Prints the data stored in each node of the linked list from head to tail. + * Outputs the data of each node in the linked list sequentially from head to tail. */ public void print() { Node current = head; while (current != null) { current.print(); - System.out.println("\n"); current = current.next; } } diff --git a/src/main/java/org/alda/structure/linkedList/sorted/ISortedLinkedList.java b/src/main/java/org/dsa/structure/linkedList/sorted/ISortedLinkedList.java similarity index 91% rename from src/main/java/org/alda/structure/linkedList/sorted/ISortedLinkedList.java rename to src/main/java/org/dsa/structure/linkedList/sorted/ISortedLinkedList.java index 9640b87..b0e5886 100644 --- a/src/main/java/org/alda/structure/linkedList/sorted/ISortedLinkedList.java +++ b/src/main/java/org/dsa/structure/linkedList/sorted/ISortedLinkedList.java @@ -1,7 +1,7 @@ -package org.alda.structure.linkedList.sorted; +package org.dsa.structure.linkedList.sorted; -import org.alda.common.Printable; -import org.alda.structure.linkedList.simple.Node; +import org.dsa.common.Printable; +import org.dsa.structure.linkedList.simple.Node; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/linkedList/sorted/SortedLinkedList.java b/src/main/java/org/dsa/structure/linkedList/sorted/SortedLinkedList.java similarity index 90% rename from src/main/java/org/alda/structure/linkedList/sorted/SortedLinkedList.java rename to src/main/java/org/dsa/structure/linkedList/sorted/SortedLinkedList.java index aefb9a8..f1de392 100644 --- a/src/main/java/org/alda/structure/linkedList/sorted/SortedLinkedList.java +++ b/src/main/java/org/dsa/structure/linkedList/sorted/SortedLinkedList.java @@ -1,8 +1,8 @@ -package org.alda.structure.linkedList.sorted; +package org.dsa.structure.linkedList.sorted; -import org.alda.common.Comparer; -import org.alda.structure.linkedList.simple.Node; -import org.alda.structure.linkedList.simple.SimpleLinkedList; +import org.dsa.common.Comparer; +import org.dsa.structure.linkedList.simple.Node; +import org.dsa.structure.linkedList.simple.SimpleLinkedList; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/queue/IQueue.java b/src/main/java/org/dsa/structure/queue/IQueue.java similarity index 96% rename from src/main/java/org/alda/structure/queue/IQueue.java rename to src/main/java/org/dsa/structure/queue/IQueue.java index 9e6a4cf..4c02d95 100644 --- a/src/main/java/org/alda/structure/queue/IQueue.java +++ b/src/main/java/org/dsa/structure/queue/IQueue.java @@ -1,4 +1,4 @@ -package org.alda.structure.queue; +package org.dsa.structure.queue; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/queue/Queue.java b/src/main/java/org/dsa/structure/queue/Queue.java similarity index 97% rename from src/main/java/org/alda/structure/queue/Queue.java rename to src/main/java/org/dsa/structure/queue/Queue.java index 38fe816..b9c1316 100644 --- a/src/main/java/org/alda/structure/queue/Queue.java +++ b/src/main/java/org/dsa/structure/queue/Queue.java @@ -1,4 +1,4 @@ -package org.alda.structure.queue; +package org.dsa.structure.queue; import java.util.ArrayList; diff --git a/src/main/java/org/alda/structure/queue/array/QueueArray.java b/src/main/java/org/dsa/structure/queue/array/QueueArray.java similarity index 93% rename from src/main/java/org/alda/structure/queue/array/QueueArray.java rename to src/main/java/org/dsa/structure/queue/array/QueueArray.java index f0680d4..f92dfc1 100644 --- a/src/main/java/org/alda/structure/queue/array/QueueArray.java +++ b/src/main/java/org/dsa/structure/queue/array/QueueArray.java @@ -1,6 +1,6 @@ -package org.alda.structure.queue.array; +package org.dsa.structure.queue.array; -import org.alda.structure.queue.IQueue; +import org.dsa.structure.queue.IQueue; import java.util.ArrayList; diff --git a/src/main/java/org/alda/structure/queue/linkedList/QueueLinkedList.java b/src/main/java/org/dsa/structure/queue/linkedList/QueueLinkedList.java similarity index 91% rename from src/main/java/org/alda/structure/queue/linkedList/QueueLinkedList.java rename to src/main/java/org/dsa/structure/queue/linkedList/QueueLinkedList.java index d33293f..b5eaf3f 100644 --- a/src/main/java/org/alda/structure/queue/linkedList/QueueLinkedList.java +++ b/src/main/java/org/dsa/structure/queue/linkedList/QueueLinkedList.java @@ -1,7 +1,7 @@ -package org.alda.structure.queue.linkedList; +package org.dsa.structure.queue.linkedList; -import org.alda.structure.linkedList.simple.Node; -import org.alda.structure.queue.IQueue; +import org.dsa.structure.linkedList.simple.Node; +import org.dsa.structure.queue.IQueue; import java.util.ArrayList; diff --git a/src/main/java/org/alda/structure/queue/priority/IPriorityQueue.java b/src/main/java/org/dsa/structure/queue/priority/IPriorityQueue.java similarity index 70% rename from src/main/java/org/alda/structure/queue/priority/IPriorityQueue.java rename to src/main/java/org/dsa/structure/queue/priority/IPriorityQueue.java index 8a6a32d..2fff489 100644 --- a/src/main/java/org/alda/structure/queue/priority/IPriorityQueue.java +++ b/src/main/java/org/dsa/structure/queue/priority/IPriorityQueue.java @@ -1,4 +1,4 @@ -package org.alda.structure.queue.priority; +package org.dsa.structure.queue.priority; public interface IPriorityQueue { void enqueue(T element, Integer priority); diff --git a/src/main/java/org/alda/structure/queue/priority/Node.java b/src/main/java/org/dsa/structure/queue/priority/Node.java similarity index 82% rename from src/main/java/org/alda/structure/queue/priority/Node.java rename to src/main/java/org/dsa/structure/queue/priority/Node.java index b11be77..6458986 100644 --- a/src/main/java/org/alda/structure/queue/priority/Node.java +++ b/src/main/java/org/dsa/structure/queue/priority/Node.java @@ -1,4 +1,4 @@ -package org.alda.structure.queue.priority; +package org.dsa.structure.queue.priority; public class Node { public T data; diff --git a/src/main/java/org/alda/structure/queue/priority/PriorityQueue.java b/src/main/java/org/dsa/structure/queue/priority/PriorityQueue.java similarity index 92% rename from src/main/java/org/alda/structure/queue/priority/PriorityQueue.java rename to src/main/java/org/dsa/structure/queue/priority/PriorityQueue.java index f82a525..5ec4c2c 100644 --- a/src/main/java/org/alda/structure/queue/priority/PriorityQueue.java +++ b/src/main/java/org/dsa/structure/queue/priority/PriorityQueue.java @@ -1,6 +1,6 @@ -package org.alda.structure.queue.priority; +package org.dsa.structure.queue.priority; -import org.alda.common.Comparer; +import org.dsa.common.Comparer; public class PriorityQueue implements IPriorityQueue { private Node head; diff --git a/src/main/java/org/alda/structure/stack/array/IStackArray.java b/src/main/java/org/dsa/structure/stack/array/IStackArray.java similarity index 90% rename from src/main/java/org/alda/structure/stack/array/IStackArray.java rename to src/main/java/org/dsa/structure/stack/array/IStackArray.java index 9b32775..5cc1765 100644 --- a/src/main/java/org/alda/structure/stack/array/IStackArray.java +++ b/src/main/java/org/dsa/structure/stack/array/IStackArray.java @@ -1,4 +1,4 @@ -package org.alda.structure.stack.array; +package org.dsa.structure.stack.array; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/stack/array/StackArray.java b/src/main/java/org/dsa/structure/stack/array/StackArray.java similarity index 94% rename from src/main/java/org/alda/structure/stack/array/StackArray.java rename to src/main/java/org/dsa/structure/stack/array/StackArray.java index ed56215..516f338 100644 --- a/src/main/java/org/alda/structure/stack/array/StackArray.java +++ b/src/main/java/org/dsa/structure/stack/array/StackArray.java @@ -1,4 +1,4 @@ -package org.alda.structure.stack.array; +package org.dsa.structure.stack.array; import java.util.ArrayList; diff --git a/src/main/java/org/alda/structure/stack/linkedList/IStackLinkedList.java b/src/main/java/org/dsa/structure/stack/linkedList/IStackLinkedList.java similarity index 95% rename from src/main/java/org/alda/structure/stack/linkedList/IStackLinkedList.java rename to src/main/java/org/dsa/structure/stack/linkedList/IStackLinkedList.java index 2c48cdc..966debc 100644 --- a/src/main/java/org/alda/structure/stack/linkedList/IStackLinkedList.java +++ b/src/main/java/org/dsa/structure/stack/linkedList/IStackLinkedList.java @@ -1,4 +1,4 @@ -package org.alda.structure.stack.linkedList; +package org.dsa.structure.stack.linkedList; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/stack/linkedList/StackLinkedList.java b/src/main/java/org/dsa/structure/stack/linkedList/StackLinkedList.java similarity index 91% rename from src/main/java/org/alda/structure/stack/linkedList/StackLinkedList.java rename to src/main/java/org/dsa/structure/stack/linkedList/StackLinkedList.java index 72e3c01..99edf73 100644 --- a/src/main/java/org/alda/structure/stack/linkedList/StackLinkedList.java +++ b/src/main/java/org/dsa/structure/stack/linkedList/StackLinkedList.java @@ -1,6 +1,6 @@ -package org.alda.structure.stack.linkedList; +package org.dsa.structure.stack.linkedList; -import org.alda.structure.linkedList.simple.Node; +import org.dsa.structure.linkedList.simple.Node; /** * @author bcExpt1123 diff --git a/src/main/java/org/alda/structure/tree/bst/BinarySearchTree.java b/src/main/java/org/dsa/structure/tree/bst/BinarySearchTree.java similarity index 97% rename from src/main/java/org/alda/structure/tree/bst/BinarySearchTree.java rename to src/main/java/org/dsa/structure/tree/bst/BinarySearchTree.java index adfbab2..bc75233 100644 --- a/src/main/java/org/alda/structure/tree/bst/BinarySearchTree.java +++ b/src/main/java/org/dsa/structure/tree/bst/BinarySearchTree.java @@ -1,4 +1,4 @@ -package org.alda.structure.tree.bst; +package org.dsa.structure.tree.bst; import java.util.ArrayList; diff --git a/src/main/java/org/dsa/structure/tree/bst/Node.java b/src/main/java/org/dsa/structure/tree/bst/Node.java new file mode 100644 index 0000000..39badcd --- /dev/null +++ b/src/main/java/org/dsa/structure/tree/bst/Node.java @@ -0,0 +1,38 @@ +package org.dsa.structure.tree.bst; + +import org.dsa.common.Printable; + +/** + * Node for binary search tree + * @param + */ +public class Node implements Printable { + T key; + Node left; + Node right; + /** + * Constructs a new node with the specified key and no child nodes. + * + * @param key the value to store in this node + */ + public Node(T key){ + this.key = key; + left = null; + right = null; + } + + /** + * Print the node's key and the keys of its left and right children to standard output. + * + * The output is "Data: [key]" with ", Left: [left.key]" and ", Right: [right.key]" appended only when those children are present. + */ + public void print() { + System.out.print("\nData: " + key); + if (left != null) { + System.out.print(", Left: " + left.key); + } + if (right != null) { + System.out.print(", Right: " + right.key); + } + } +} diff --git a/src/main/java/org/alda/structure/tree/bst/bbt/AVL.java b/src/main/java/org/dsa/structure/tree/bst/bbt/AVL.java similarity index 68% rename from src/main/java/org/alda/structure/tree/bst/bbt/AVL.java rename to src/main/java/org/dsa/structure/tree/bst/bbt/AVL.java index 8df37c9..39c8ad1 100644 --- a/src/main/java/org/alda/structure/tree/bst/bbt/AVL.java +++ b/src/main/java/org/dsa/structure/tree/bst/bbt/AVL.java @@ -1,4 +1,4 @@ -package org.alda.structure.tree.bst.bbt; +package org.dsa.structure.tree.bst.bbt; /** * @author bcExpt1123 @@ -38,13 +38,24 @@ */ public class AVL> { + /** + * Inserts a key into the subtree rooted at the given node and restores AVL balance. + * + * If the key already exists in the subtree, the tree is left unchanged (duplicates are not inserted). + * + * @param root the root of the subtree where the key should be inserted; may be null + * @param key the value to insert + * @return the root of the subtree after insertion and any necessary rebalancing + */ public Node insert(Node root, T key) { if (root == null) { return new Node<>(key); } else if (key.compareTo(root.key) < 0) { root.left = insert(root.left, key); - } else { + } else if (key.compareTo(root.key) > 0) { root.right = insert(root.right, key); + } else { + return root; // Duplicate keys not allowed } root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right)); @@ -67,16 +78,36 @@ public Node insert(Node root, T key) { return root; } + /** + * Returns the height of the given node in the AVL subtree. + * + * @param root the node whose height is requested; may be {@code null} + * @return the node's height, or 0 if {@code root} is {@code null} + */ public Integer getHeight(Node root) { if(root == null) return 0; return root.height; } + /** + * Computes the balance factor of the given node. + * + * @param root the node whose balance factor to compute + * @return the balance factor defined as height(left subtree) minus height(right subtree); returns 0 if {@code root} is null + */ public Integer getBalance(Node root) { - if(root == null) return 0; - return getHeight(root.left) + getHeight(root.right); + if (root == null) return 0; + return getHeight(root.left) - getHeight(root.right); } + /** + * Performs a right rotation around the given subtree root. + * + *

Reassigns child pointers and updates node heights so the left child becomes the new root of the subtree.

+ * + * @param z the root of the subtree to rotate; must have a non-null left child + * @return the new root of the subtree after rotation + */ public Node rotateRight(Node z) { Node y = z.left; Node T = y.right; diff --git a/src/main/java/org/dsa/structure/tree/bst/bbt/AVLTest.java b/src/main/java/org/dsa/structure/tree/bst/bbt/AVLTest.java new file mode 100644 index 0000000..fa27da6 --- /dev/null +++ b/src/main/java/org/dsa/structure/tree/bst/bbt/AVLTest.java @@ -0,0 +1,50 @@ +package org.dsa.structure.tree.bst.bbt; + +import org.dsa.common.Utils; + +public class AVLTest { + public static void main(String[] args) { + System.out.println("\n=== Initialize AVL Tree ==="); + AVL avl = new AVL<>(); + AVL.Node root = null; + + // Insert elements to test rotations + int[] elements = {10, 20, 30, 40, 50, 25}; + + System.out.println("Actions: \n- Insert " + Utils.intArrToStr(elements)); + for (int el : elements) { + root = avl.insert(root, el); + } + + // Print tree in in-order + System.out.println("\nIn-order traversal:"); + inOrder(root); + + // Print tree structure + System.out.println("\n\nTree structure:"); + printTree(root, "", true); + } + + // In-order traversal (left-root-right) + public static > void inOrder(AVL.Node node) { + if (node != null) { + inOrder(node.left); + System.out.print(node.key + " "); + inOrder(node.right); + } + } + + // Pretty-print the tree + public static > void printTree(AVL.Node node, String prefix, boolean isTail) { + if (node == null) return; + + System.out.println(prefix + (isTail ? "└── " : "├── ") + node.key + " (h=" + node.height + ")"); + + if (node.left != null || node.right != null) { + if (node.right != null) + printTree(node.right, prefix + (isTail ? " " : "│ "), node.left == null); + if (node.left != null) + printTree(node.left, prefix + (isTail ? " " : "│ "), true); + } + } +} diff --git a/src/main/java/org/alda/structure/tree/bst/bbt/RBT.java b/src/main/java/org/dsa/structure/tree/bst/bbt/RBT.java similarity index 96% rename from src/main/java/org/alda/structure/tree/bst/bbt/RBT.java rename to src/main/java/org/dsa/structure/tree/bst/bbt/RBT.java index 380fb2a..6f36828 100644 --- a/src/main/java/org/alda/structure/tree/bst/bbt/RBT.java +++ b/src/main/java/org/dsa/structure/tree/bst/bbt/RBT.java @@ -1,4 +1,4 @@ -package org.alda.structure.tree.bst.bbt; +package org.dsa.structure.tree.bst.bbt; /** * @author bcExpt1123 diff --git a/src/test/java/org/alda/common/ComparerTest.java b/src/test/java/org/dsa/common/ComparerTest.java similarity index 98% rename from src/test/java/org/alda/common/ComparerTest.java rename to src/test/java/org/dsa/common/ComparerTest.java index fb27966..77be598 100644 --- a/src/test/java/org/alda/common/ComparerTest.java +++ b/src/test/java/org/dsa/common/ComparerTest.java @@ -1,4 +1,4 @@ -package org.alda.common; +package org.dsa.common; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/test/java/org/dsa/common/UtilsTest.java b/src/test/java/org/dsa/common/UtilsTest.java new file mode 100644 index 0000000..5d4516b --- /dev/null +++ b/src/test/java/org/dsa/common/UtilsTest.java @@ -0,0 +1,42 @@ +package org.dsa.common; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class UtilsTest { + @Test + void testNormalArray() { + int[] arr = {1, 2, 3, 4, 5}; + String result = Utils.intArrToStr(arr, ","); + assertEquals("1,2,3,4,5", result); + } + + @Test + void testSingleElementArray() { + int[] arr = {42}; + String result = Utils.intArrToStr(arr, ","); + assertEquals("42", result); + } + + @Test + void testEmptyArray() { + int[] arr = {}; + String result = Utils.intArrToStr(arr, ","); + assertEquals("", result); + } + + @Test + void testDifferentDelimiter() { + int[] arr = {10, 20, 30}; + String result = Utils.intArrToStr(arr, " | "); + assertEquals("10 | 20 | 30", result); + } + + @Test + void testNegativeNumbers() { + int[] arr = {-1, -2, -3}; + String result = Utils.intArrToStr(arr, ";"); + assertEquals("-1;-2;-3", result); + } +}