Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/main/java/org/alda/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ private static void displayMainMenu() {
}

/**
* Displays a submenu for selecting and testing different types of linked lists.
* Display a submenu to select and run linked list demonstrations.
*
* 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.
* 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() {
while (true) {
Expand Down Expand Up @@ -207,9 +207,9 @@ private static void pressEnterToContinue() {
}

/**
* Demonstrates basic operations on a simple linked list of integers, including appending, inserting, deleting, printing, and searching for elements.
* Demonstrates basic operations on a simple linked list of integers.
*
* Initializes a simple linked list, performs a sequence of modifications, prints the list and head node, and displays search results for specific values.
* 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 ===");
Expand Down Expand Up @@ -297,9 +297,10 @@ public static void testSortedLinkedList() {
}

/**
* Demonstrates basic operations on a doubly ended linked list (deque) of integers.
* Demonstrates insertions at both front and end on a doubly ended linked list of integers.
*
* Initializes a doubly ended list, performs a sequence of insertions at both the front and end, and prints the resulting list.
* 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 ===");
Expand Down Expand Up @@ -447,9 +448,9 @@ public static void testQueueLinkedList(){
}

/**
* Demonstrates the usage of a priority queue by enqueuing integer elements with specified priorities and dequeuing several elements to show priority-based removal order.
* Demonstrates priority-based ordering by enqueuing integer elements with associated priorities and dequeuing several elements to show 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.
* 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 ===");
Expand Down Expand Up @@ -480,10 +481,10 @@ public static void testPriorityQueue(){
}

/**
* Demonstrates basic operations on a binary search tree, including insertion, inorder traversal, and search.
* Demonstrates insertion, inorder traversal, and search on a binary search tree.
*
* 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.
* Builds a BinarySearchTree<Integer>, 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 ===");
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/alda/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
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, ", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public Node<T> 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<T> tail) {
this.tail = tail;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/alda/structure/linkedList/deque/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public void prepend(T data) {
}

/**
* Prints each node's data in the list from head to tail, including adjacent nodes' data if present.
* 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<T> current = head;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/alda/structure/linkedList/doubly/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public class Node<T> implements Printable {
public Node<T> 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;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/alda/structure/linkedList/simple/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public class Node<T> implements Printable {
public Node<T> 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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/alda/structure/tree/bst/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public Node(T key){
}

/**
* Prints the node's key and the keys of its left and right children, if present, to the standard output.
* Print the node's key and the keys of its left and right children to standard output.
*
* The output format is: "Data: [key], Left: [left.key], Right: [right.key]". Child information is omitted if the respective child is null.
* 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);
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/alda/structure/tree/bst/bbt/AVL.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
*/
public class AVL<T extends Comparable<T>> {

/**
* 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<T> insert(Node<T> root, T key) {
if (root == null) {
return new Node<>(key);
Expand Down Expand Up @@ -69,16 +78,36 @@ public Node<T> insert(Node<T> 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<T> 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<T> root) {
if (root == null) return 0;
return getHeight(root.left) - getHeight(root.right);
}

/**
* Performs a right rotation around the given subtree root.
*
* <p>Reassigns child pointers and updates node heights so the left child becomes the new root of the subtree.</p>
*
* @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<T> rotateRight(Node<T> z) {
Node<T> y = z.left;
Node<T> T = y.right;
Expand Down