@@ -162,19 +162,22 @@ public void insert(int key) {
162
162
}
163
163
164
164
/**
165
- * Recursive function to insert a key.
165
+ * Recursive function to insert a key into a subtree .
166
166
*
167
167
* @param root The root of the subtree to insert the key into.
168
168
* @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.
170
171
*/
171
172
private Node insertRec (Node root , int key ) {
172
173
if (root == null ) return new Node (key );
173
174
174
- if (root . key > key ) {
175
+ if (key < root . key ) {
175
176
root .left = insertRec (root .left , key );
176
- } else if (root . key < key ) {
177
+ } else if (key > root . key ) {
177
178
root .right = insertRec (root .right , key );
179
+ } else {
180
+ throw new IllegalArgumentException ("Duplicate key: " + key );
178
181
}
179
182
180
183
return root ;
@@ -192,58 +195,33 @@ public boolean search(int key) {
192
195
}
193
196
194
197
/**
195
- * Delete a key from the SplayTree.
198
+ * Deletes a key from the SplayTree.
196
199
*
197
200
* @param key The key to delete.
201
+ * @throws IllegalArgumentException If the tree is empty.
198
202
*/
199
203
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
+ }
223
207
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 );
226
210
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 ;
229
214
}
230
215
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 ;
245
224
}
246
- return minValue ;
247
225
}
248
226
249
227
/**
0 commit comments