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);
+ }
+}