Skip to content

Commit 9818bff

Browse files
committed
ref: update implementation
- Add `isEmpty` method to check to check the entire tree root and continue using `root == null` checks within recursive calls - Add default branch to switch statement
1 parent 9aee128 commit 9818bff

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public SplayTree() {
4747
root = null;
4848
}
4949

50+
/**
51+
* Checks if the tree is empty.
52+
*
53+
* @return True if the tree is empty, otherwise false.
54+
*/
55+
public boolean isEmpty() {
56+
return root == null;
57+
}
58+
5059
/**
5160
* Zig operation.
5261
*
@@ -202,7 +211,7 @@ public boolean search(int key) {
202211
* @throws IllegalArgumentException If the tree is empty.
203212
*/
204213
public void delete(int key) {
205-
if (root == null) {
214+
if (isEmpty()) {
206215
throw new IllegalArgumentException("Cannot delete from an empty tree");
207216
}
208217

@@ -243,6 +252,8 @@ public List<Integer> traverse(TraverseOrder traverseOrder) {
243252
case POST_ORDER:
244253
postOrderRec(root, result);
245254
break;
255+
default:
256+
throw new IllegalArgumentException("Invalid traversal order: " + traverseOrder);
246257
}
247258
return result;
248259
}

0 commit comments

Comments
 (0)