From 7ac7f515980ea832cb037cd3008fbbff5e752868 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Wed, 25 Jun 2025 16:40:00 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`test`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docstrings generation was requested by @bcExpt1123. * https://github.com/bcExpt1123/java-alda/pull/3#issuecomment-3005417343 The following files were modified: * `src/main/java/org/alda/Main.java` * `src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java` * `src/main/java/org/alda/structure/linkedList/circular/Node.java` * `src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java` * `src/main/java/org/alda/structure/linkedList/deque/Node.java` * `src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java` * `src/main/java/org/alda/structure/linkedList/doubly/Node.java` * `src/main/java/org/alda/structure/linkedList/simple/Node.java` * `src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java` * `src/main/java/org/alda/structure/tree/bst/Node.java` --- src/main/java/org/alda/Main.java | 106 ++++++++++++++++++ .../circular/CircularLinkedList.java | 4 +- .../structure/linkedList/circular/Node.java | 5 +- .../linkedList/deque/DoublyEndedList.java | 3 +- .../alda/structure/linkedList/deque/Node.java | 6 +- .../linkedList/doubly/DoublyLinkedList.java | 3 +- .../structure/linkedList/doubly/Node.java | 7 +- .../structure/linkedList/simple/Node.java | 5 +- .../linkedList/simple/SimpleLinkedList.java | 2 +- .../org/alda/structure/tree/bst/Node.java | 10 ++ 10 files changed, 136 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/alda/Main.java b/src/main/java/org/alda/Main.java index cafed5a..2f93eb9 100644 --- a/src/main/java/org/alda/Main.java +++ b/src/main/java/org/alda/Main.java @@ -32,6 +32,13 @@ public class Main { private static final Scanner scanner = new Scanner(System.in); + /** + * 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) { boolean running = true; @@ -52,6 +59,9 @@ public static void main(String[] args) { } scanner.close(); } + /** + * 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"); @@ -62,6 +72,11 @@ private static void displayMainMenu() { System.out.print("\nEnter your choice (1-5): "); } + /** + * Displays a submenu for selecting and testing different types of linked lists. + * + * Loops until the user chooses to return to the main menu. For each selection, runs the corresponding linked list demonstration and waits for user input before returning to the submenu. + */ private static void linkedListMenu() { while (true) { System.out.println("\n=== Linked List Types ==="); @@ -88,6 +103,11 @@ private static void linkedListMenu() { } } + /** + * 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() { while (true) { System.out.println("\n=== Stack Types ==="); @@ -108,6 +128,12 @@ private static void stackMenu() { } } + /** + * 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() { while (true) { System.out.println("\n=== Queue Types ==="); @@ -132,6 +158,11 @@ private static void queueMenu() { } } + /** + * 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() { System.out.println("\n=== Tree Types ==="); System.out.println("1. Binary Search Tree"); @@ -149,6 +180,12 @@ private static void treeMenu() { pressEnterToContinue(); } + /** + * 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(int max) { while (true) { try { @@ -163,11 +200,19 @@ private static int getMenuChoice(int max) { } } + /** + * Prompts the user to press Enter and waits for input before continuing. + */ private static void pressEnterToContinue() { System.out.print("\nPress Enter to continue..."); scanner.nextLine(); } + /** + * Demonstrates basic operations on a simple linked list of integers, including appending, inserting, deleting, printing, and searching for elements. + * + * Initializes a simple linked list, performs a sequence of modifications, prints the list and head node, and displays search results for specific values. + */ public static void testSimpleLinkedList() { System.out.println("\n=== Initialize Simple Linked List ==="); ISimpleLinkedList intList = new SimpleLinkedList(); @@ -195,6 +240,11 @@ public static void testSimpleLinkedList() { 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(); @@ -209,6 +259,11 @@ public static void testDoublyLinkedList() { 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(); @@ -223,6 +278,11 @@ public static void testCircularLinkedList() { 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(); @@ -238,6 +298,11 @@ public static void testSortedLinkedList() { intList.print(); } + /** + * Demonstrates basic operations on a doubly ended linked list (deque) of integers. + * + * Initializes a doubly ended list, performs a sequence of insertions at both the front and end, and prints the resulting list. + */ public static void testDoublyEndedList(){ System.out.println("\n=== Initialize Doubly Ended Linked List ==="); IDoublyEndedList intList = new DoublyEndedList<>(); @@ -255,6 +320,11 @@ public static void testDoublyEndedList(){ 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<>(); @@ -269,6 +339,11 @@ public static void testStackArray(){ 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<>(); @@ -283,6 +358,11 @@ public static void testStackLinkedList(){ 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<>(); @@ -308,6 +388,11 @@ public static void testQueue(){ 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<>(); @@ -333,6 +418,11 @@ public static void testQueueArray(){ 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<>(); @@ -358,6 +448,11 @@ public static void testQueueLinkedList(){ System.out.println("Rear: " + rear); } + /** + * Demonstrates the usage of a priority queue by enqueuing integer elements with specified priorities and dequeuing several elements to show priority-based removal order. + * + * This method initializes a priority queue, enqueues multiple integers with associated priorities, then dequeues and prints four elements to illustrate how the queue prioritizes elements. + */ public static void testPriorityQueue(){ System.out.println("\n=== Initialize Priority Queue ==="); IPriorityQueue intQueue = new PriorityQueue<>(); @@ -386,6 +481,12 @@ public static void testPriorityQueue(){ System.out.println("Dequeue: " + item3); } + /** + * Demonstrates basic operations on a binary search tree, including insertion, inorder traversal, and search. + * + * Initializes a binary search tree of integers, inserts several values, prints the inorder traversal, + * and searches for the value 4, displaying whether it is found. + */ public static void testBST(){ System.out.println("\n=== Initialize Binary Search Tree ==="); BinarySearchTree bst = new BinarySearchTree<>(); @@ -406,6 +507,11 @@ public static void testBST(){ 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(){ AVL avl = new AVL<>(); AVL.Node root = null; diff --git a/src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java b/src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java index acfdfd3..059bf43 100644 --- a/src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java +++ b/src/main/java/org/alda/structure/linkedList/circular/CircularLinkedList.java @@ -63,8 +63,8 @@ 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; diff --git a/src/main/java/org/alda/structure/linkedList/circular/Node.java b/src/main/java/org/alda/structure/linkedList/circular/Node.java index 90a38ad..7d38727 100644 --- a/src/main/java/org/alda/structure/linkedList/circular/Node.java +++ b/src/main/java/org/alda/structure/linkedList/circular/Node.java @@ -49,8 +49,9 @@ 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("\nData: " + data); diff --git a/src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java b/src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java index c7107a5..139206a 100644 --- a/src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java +++ b/src/main/java/org/alda/structure/linkedList/deque/DoublyEndedList.java @@ -68,8 +68,7 @@ public void setTail(Node 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; diff --git a/src/main/java/org/alda/structure/linkedList/deque/Node.java b/src/main/java/org/alda/structure/linkedList/deque/Node.java index 67671b7..15dd1f9 100644 --- a/src/main/java/org/alda/structure/linkedList/deque/Node.java +++ b/src/main/java/org/alda/structure/linkedList/deque/Node.java @@ -52,8 +52,10 @@ public Node(){ } /** - * 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("\nData: " + data); diff --git a/src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java b/src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java index 10f5e0e..5dcc123 100644 --- a/src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java +++ b/src/main/java/org/alda/structure/linkedList/doubly/DoublyLinkedList.java @@ -78,8 +78,7 @@ 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. + * Prints each node's data in the list from head to tail, including adjacent nodes' data if present. */ public void print() { Node current = head; diff --git a/src/main/java/org/alda/structure/linkedList/doubly/Node.java b/src/main/java/org/alda/structure/linkedList/doubly/Node.java index 2042583..31d793b 100644 --- a/src/main/java/org/alda/structure/linkedList/doubly/Node.java +++ b/src/main/java/org/alda/structure/linkedList/doubly/Node.java @@ -44,8 +44,11 @@ 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("\nData: " + data); diff --git a/src/main/java/org/alda/structure/linkedList/simple/Node.java b/src/main/java/org/alda/structure/linkedList/simple/Node.java index a2b1679..ef67119 100644 --- a/src/main/java/org/alda/structure/linkedList/simple/Node.java +++ b/src/main/java/org/alda/structure/linkedList/simple/Node.java @@ -37,8 +37,9 @@ 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("\nData: " + data); diff --git a/src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java b/src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java index a670f29..a430fa5 100644 --- a/src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java +++ b/src/main/java/org/alda/structure/linkedList/simple/SimpleLinkedList.java @@ -136,7 +136,7 @@ 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; diff --git a/src/main/java/org/alda/structure/tree/bst/Node.java b/src/main/java/org/alda/structure/tree/bst/Node.java index fa1c82d..80dfd71 100644 --- a/src/main/java/org/alda/structure/tree/bst/Node.java +++ b/src/main/java/org/alda/structure/tree/bst/Node.java @@ -10,12 +10,22 @@ 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; } + /** + * Prints the node's key and the keys of its left and right children, if present, to the standard output. + * + * The output format is: "Data: [key], Left: [left.key], Right: [right.key]". Child information is omitted if the respective child is null. + */ public void print() { System.out.print("\nData: " + key); if (left != null) {