{"record":{"id":"248489dfd222dfa9","repo":"TheAlgorithms/Java","slug":"cannot-delete-from-an-empty-tree","errorCode":null,"errorMessage":"Cannot delete from an empty tree","messagePattern":"Cannot delete from an empty tree","errorType":"exception","errorClass":"EmptyTreeException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java","lineNumber":72,"sourceCode":"     * Search for a key in the SplayTree.\n     *\n     * @param key The key to search for.\n     * @return True if the key is found, otherwise false.\n     */\n    public boolean search(int key) {\n        root = splay(root, key);\n        return root != null && root.key == key;\n    }\n\n    /**\n     * Deletes a key from the SplayTree.\n     *\n     * @param key The key to delete.\n     * @throws IllegalArgumentException If the tree is empty.\n     */\n    public void delete(final int key) {\n        if (isEmpty()) {\n            throw new EmptyTreeException(\"Cannot delete from an empty tree\");\n        }\n\n        root = splay(root, key);\n\n        if (root.key != key) {\n            return;\n        }\n\n        if (root.left == null) {\n            root = root.right;\n        } else {\n            Node temp = root;\n            root = splay(root.left, findMax(root.left).key);\n            root.right = temp.right;\n        }\n    }\n\n    /**","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java#L54-L90","documentation":"SplayTree.delete(int key) guards against operating on an empty tree by calling isEmpty() (root == null) at entry. Deleting from a tree with no nodes is a logic error — there is nothing to remove or splay — so it throws EmptyTreeException (a RuntimeException subclass) with a descriptive message.","triggerScenarios":"Calling delete(key) on a newly constructed SplayTree before any insert, or after all nodes have been deleted in a prior sequence of operations.","commonSituations":"Batch processing pipelines that delete keys from a shared tree without tracking whether it has been emptied; test teardown that deletes keys one-by-one and hits the last deletion edge case.","solutions":["Check !tree.isEmpty() before calling delete.","Maintain an element count alongside the tree and guard delete when count reaches zero.","Catch EmptyTreeException if the caller treats delete-on-empty as a no-op."],"exampleFix":"// before\ntree.delete(42); // throws if tree is empty\n\n// after\nif (!tree.isEmpty()) {\n    tree.delete(42);\n}","handlingStrategy":"validation","validationCode":"if (!tree.isEmpty()) {\n    tree.delete(key);\n}","typeGuard":null,"tryCatchPattern":"try {\n    tree.delete(key);\n} catch (SplayTree.EmptyTreeException e) {\n    // no-op: tree is already empty, nothing to delete\n}","preventionTips":["Track an element count alongside the tree and guard delete when count is zero.","Use isEmpty() as a precondition before every delete call."],"tags":["splay-tree","empty-state","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}