Skip to content

Commit ac587d3

Browse files
committed
ref: refactor
- Do not allow duplicate keys and explicitly throw an exception if one is trying to include already existing key - Throw error on deletion if tree is empty - Performing the splay operation before recursion, ensuring that the node to be deleted is at the root or very close to it, simplifying the deletion process.
1 parent 975d263 commit ac587d3

File tree

1 file changed

+25
-47
lines changed

1 file changed

+25
-47
lines changed

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

+25-47
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,22 @@ public void insert(int key) {
162162
}
163163

164164
/**
165-
* Recursive function to insert a key.
165+
* Recursive function to insert a key into a subtree.
166166
*
167167
* @param root The root of the subtree to insert the key into.
168168
* @param key The key to insert.
169-
* @return The root of the modified subtree.
169+
* @return The root of the modified subtree after insertion.
170+
* @throws IllegalArgumentException If the key to be inserted already exists in the subtree.
170171
*/
171172
private Node insertRec(Node root, int key) {
172173
if (root == null) return new Node(key);
173174

174-
if (root.key > key) {
175+
if (key < root.key) {
175176
root.left = insertRec(root.left, key);
176-
} else if (root.key < key) {
177+
} else if (key > root.key) {
177178
root.right = insertRec(root.right, key);
179+
} else {
180+
throw new IllegalArgumentException("Duplicate key: " + key);
178181
}
179182

180183
return root;
@@ -192,58 +195,33 @@ public boolean search(int key) {
192195
}
193196

194197
/**
195-
* Delete a key from the SplayTree.
198+
* Deletes a key from the SplayTree.
196199
*
197200
* @param key The key to delete.
201+
* @throws IllegalArgumentException If the tree is empty.
198202
*/
199203
public void delete(int key) {
200-
root = deleteRec(root, key);
201-
}
202-
203-
/**
204-
* Recursive function to delete a key.
205-
*
206-
* @param root The root of the subtree to delete the key from.
207-
* @param key The key to delete.
208-
* @return The root of the modified subtree.
209-
*/
210-
private Node deleteRec(Node root, int key) {
211-
if (root == null) return null;
212-
213-
if (root.key > key) {
214-
root.left = deleteRec(root.left, key);
215-
} else if (root.key < key) {
216-
root.right = deleteRec(root.right, key);
217-
} else {
218-
// Found the node to delete
219-
if (root.left == null)
220-
return root.right;
221-
else if (root.right == null)
222-
return root.left;
204+
if (root == null) {
205+
throw new IllegalArgumentException("Cannot delete from an empty tree");
206+
}
223207

224-
// Node with two children: Get the inorder successor (smallest in the right subtree)
225-
root.key = minValue(root.right);
208+
// Splay the tree with the key to be deleted
209+
root = splay(root, key);
226210

227-
// Delete the inorder successor
228-
root.right = deleteRec(root.right, root.key);
211+
// If the key is not found at the root, return without deleting
212+
if (root.key != key) {
213+
return;
229214
}
230215

231-
return root;
232-
}
233-
234-
/**
235-
* Find the minimum value in a subtree.
236-
*
237-
* @param root The root of the subtree to find the minimum value in.
238-
* @return The minimum value in the subtree.
239-
*/
240-
private int minValue(Node root) {
241-
int minValue = root.key;
242-
while (root.left != null) {
243-
minValue = root.left.key;
244-
root = root.left;
216+
// Handle deletion
217+
if (root.left == null) {
218+
root = root.right;
219+
} else {
220+
Node temp = root;
221+
// Splay to bring the largest key in left subtree to root
222+
root = splay(root.left, key);
223+
root.right = temp.right;
245224
}
246-
return minValue;
247225
}
248226

249227
/**

0 commit comments

Comments
 (0)