Skip to content

Commit a917047

Browse files
committed
ref: improve splay tree
- Add `final` to the `key` - Add `serialVersionUID` field to the exception classes
1 parent 1402ab9 commit a917047

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public boolean isEmpty() {
4545
*
4646
* @param key The key to insert.
4747
*/
48-
public final void insert(int key) {
48+
public void insert(final int key) {
4949
root = insertRec(root, key);
5050
root = splay(root, key);
5151
}
@@ -67,29 +67,42 @@ public boolean search(int key) {
6767
* @param key The key to delete.
6868
* @throws IllegalArgumentException If the tree is empty.
6969
*/
70-
public final void delete(int key) {
70+
public void delete(final int key) {
7171
if (isEmpty()) {
7272
throw new EmptyTreeException("Cannot delete from an empty tree");
7373
}
7474

75-
// Splay the tree with the key to be deleted
7675
root = splay(root, key);
7776

78-
// If the key is not found at the root, return without deleting
7977
if (root.key != key) {
8078
return;
8179
}
8280

83-
// Handle deletion
8481
if (root.left == null) {
8582
root = root.right;
8683
} else {
87-
// Splay to bring the largest key in left subtree to root
8884
Node temp = root;
89-
root = splay(root.left, key);
85+
root = splay(root.left, findMax(root.left).key);
9086
root.right = temp.right;
9187
}
9288
}
89+
/**
90+
* Finds the node with the maximum key in a given subtree.
91+
*
92+
* <p>
93+
* This method traverses the right children of the subtree until it finds the
94+
* rightmost node, which contains the maximum key.
95+
* </p>
96+
*
97+
* @param root The root node of the subtree.
98+
* @return The node with the maximum key in the subtree.
99+
*/
100+
private Node findMax(Node root) {
101+
while (root.right != null) {
102+
root = root.right;
103+
}
104+
return root;
105+
}
93106

94107
/**
95108
* Perform a traversal of the SplayTree.
@@ -177,7 +190,7 @@ private Node rotateLeft(Node x) {
177190
* @param key The key to splay around.
178191
* @return The new root of the splayed subtree.
179192
*/
180-
private Node splay(Node root, int key) {
193+
private Node splay(Node root, final int key) {
181194
if (root == null || root.key == key) {
182195
return root;
183196
}
@@ -215,7 +228,7 @@ private Node splay(Node root, int key) {
215228
}
216229
}
217230

218-
private Node insertRec(Node root, int key) {
231+
private Node insertRec(Node root, final int key) {
219232
if (root == null) {
220233
return new Node(key);
221234
}
@@ -232,12 +245,16 @@ private Node insertRec(Node root, int key) {
232245
}
233246

234247
public static class EmptyTreeException extends RuntimeException {
248+
private static final long serialVersionUID = 1L;
249+
235250
public EmptyTreeException(String message) {
236251
super(message);
237252
}
238253
}
239254

240255
public static class DuplicateKeyException extends RuntimeException {
256+
private static final long serialVersionUID = 1L;
257+
241258
public DuplicateKeyException(String message) {
242259
super(message);
243260
}

0 commit comments

Comments
 (0)