Skip to content

Commit 38b6103

Browse files
committed
Add AVL tests
1 parent 9534874 commit 38b6103

File tree

5 files changed

+115
-9
lines changed

5 files changed

+115
-9
lines changed

src/main/java/org/alda/Main.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.alda.structure.stack.linkedList.StackLinkedList;
2222
import org.alda.structure.tree.bst.BinarySearchTree;
2323
import org.alda.structure.tree.bst.bbt.AVL;
24+
import org.alda.structure.tree.bst.bbt.AVLTest;
2425

2526
import java.util.Arrays;
2627
import java.util.List;
@@ -407,11 +408,6 @@ public static void testBST(){
407408
}
408409

409410
public static void testAVL(){
410-
AVL<Integer> avl = new AVL<>();
411-
AVL.Node<Integer> root = null;
412-
List<Integer> keys = Arrays.asList(10, 20, 30, 40, 50, 25);
413-
for(Integer key : keys){
414-
avl.insert(root, key);
415-
}
411+
AVLTest.main(null);
416412
}
417413
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.alda.common;
2+
3+
import java.util.Arrays;
4+
5+
public class Utils {
6+
public static String intArrToStr(int[] arr, String delimiter) {
7+
return Arrays.stream(arr)
8+
.mapToObj(String::valueOf)
9+
.reduce((a, b) -> a + delimiter + b)
10+
.orElse("");
11+
}
12+
13+
public static String intArrToStr(int[] arr) {
14+
return intArrToStr(arr, ", ");
15+
}
16+
}

src/main/java/org/alda/structure/tree/bst/bbt/AVL.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ public Node<T> insert(Node<T> root, T key) {
4343
return new Node<>(key);
4444
} else if (key.compareTo(root.key) < 0) {
4545
root.left = insert(root.left, key);
46-
} else {
46+
} else if (key.compareTo(root.key) > 0) {
4747
root.right = insert(root.right, key);
48+
} else {
49+
return root; // Duplicate keys not allowed
4850
}
4951

5052
root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right));
@@ -73,8 +75,8 @@ public Integer getHeight(Node<T> root) {
7375
}
7476

7577
public Integer getBalance(Node<T> root) {
76-
if(root == null) return 0;
77-
return getHeight(root.left) + getHeight(root.right);
78+
if (root == null) return 0;
79+
return getHeight(root.left) - getHeight(root.right);
7880
}
7981

8082
public Node<T> rotateRight(Node<T> z) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.alda.structure.tree.bst.bbt;
2+
3+
import org.alda.common.Utils;
4+
5+
public class AVLTest {
6+
public static void main(String[] args) {
7+
System.out.println("\n=== Initialize AVL Tree ===");
8+
AVL<Integer> avl = new AVL<>();
9+
AVL.Node<Integer> root = null;
10+
11+
// Insert elements to test rotations
12+
int[] elements = {10, 20, 30, 40, 50, 25};
13+
14+
System.out.println("Actions: \n- Insert " + Utils.intArrToStr(elements));
15+
for (int el : elements) {
16+
root = avl.insert(root, el);
17+
}
18+
19+
// Print tree in in-order
20+
System.out.println("\nIn-order traversal:");
21+
inOrder(root);
22+
23+
// Print tree structure
24+
System.out.println("\n\nTree structure:");
25+
printTree(root, "", true);
26+
}
27+
28+
// In-order traversal (left-root-right)
29+
public static <T extends Comparable<T>> void inOrder(AVL.Node<T> node) {
30+
if (node != null) {
31+
inOrder(node.left);
32+
System.out.print(node.key + " ");
33+
inOrder(node.right);
34+
}
35+
}
36+
37+
// Pretty-print the tree
38+
public static <T extends Comparable<T>> void printTree(AVL.Node<T> node, String prefix, boolean isTail) {
39+
if (node == null) return;
40+
41+
System.out.println(prefix + (isTail ? "└── " : "├── ") + node.key + " (h=" + node.height + ")");
42+
43+
if (node.left != null || node.right != null) {
44+
if (node.right != null)
45+
printTree(node.right, prefix + (isTail ? " " : "│ "), node.left == null);
46+
if (node.left != null)
47+
printTree(node.left, prefix + (isTail ? " " : "│ "), true);
48+
}
49+
}
50+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.alda.common;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class UtilsTest {
8+
@Test
9+
void testNormalArray() {
10+
int[] arr = {1, 2, 3, 4, 5};
11+
String result = Utils.intArrToStr(arr, ",");
12+
assertEquals("1,2,3,4,5", result);
13+
}
14+
15+
@Test
16+
void testSingleElementArray() {
17+
int[] arr = {42};
18+
String result = Utils.intArrToStr(arr, ",");
19+
assertEquals("42", result);
20+
}
21+
22+
@Test
23+
void testEmptyArray() {
24+
int[] arr = {};
25+
String result = Utils.intArrToStr(arr, ",");
26+
assertEquals("", result);
27+
}
28+
29+
@Test
30+
void testDifferentDelimiter() {
31+
int[] arr = {10, 20, 30};
32+
String result = Utils.intArrToStr(arr, " | ");
33+
assertEquals("10 | 20 | 30", result);
34+
}
35+
36+
@Test
37+
void testNegativeNumbers() {
38+
int[] arr = {-1, -2, -3};
39+
String result = Utils.intArrToStr(arr, ";");
40+
assertEquals("-1;-2;-3", result);
41+
}
42+
}

0 commit comments

Comments
 (0)