Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3876802
feat: implement SplayTree
sozelfist May 3, 2024
08f223f
chore(docs): update docstring
sozelfist May 10, 2024
6a61536
ref: add `traverse` method
sozelfist May 10, 2024
a357336
ref: update tests
sozelfist May 10, 2024
35a77ab
ref: refactor
sozelfist May 23, 2024
897633d
chore: fix checkstyle warning
sozelfist May 23, 2024
3d9cf0e
ref: add tests
sozelfist May 23, 2024
79dad80
ref: update implementation
sozelfist May 26, 2024
9fbd1c0
chore(fix:style): fix Maven checkstyle
sozelfist May 26, 2024
12effaf
ref: add default pattern to switch statement
sozelfist May 26, 2024
d07f7bb
chore: fix clang-format issue
sozelfist May 26, 2024
d069405
ref: refactor SplayTree implementation
sozelfist May 31, 2024
0c44838
chore: fix clang-format issue
sozelfist May 31, 2024
a0b9fd3
chore(tests): update tests
sozelfist May 31, 2024
3935b05
ref: refactor implementation
sozelfist Jun 1, 2024
95dc5c9
chore(fix[check-style]): use braces in `if` statement
sozelfist Jun 15, 2024
0fe1dd9
Merge branch 'master' into feat/ds/splay_tree
sozelfist Jun 30, 2024
e5a39ae
chore: update splaytree initialization
sozelfist Jun 30, 2024
d2546e7
ref: update tests
sozelfist Jun 30, 2024
16df17c
chore: add tests `testZigZagCaseWithNullChild()`
sozelfist Jun 30, 2024
c9f0696
ref: improve splay tree
sozelfist Aug 31, 2024
7125270
Update directory
Aug 31, 2024
ec4e304
Merge branch 'master' into feat/ds/splay_tree
sozelfist Aug 31, 2024
8306158
Update directory
Aug 31, 2024
99e6f97
chore: format code
sozelfist Aug 31, 2024
1402ab9
chore: remove redundant `final`
sozelfist Aug 31, 2024
a917047
ref: improve splay tree
sozelfist Sep 1, 2024
5431d3e
chore: reorganize code structure
sozelfist Sep 1, 2024
155e54b
chore: remove redundant test
sozelfist Sep 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
sozelfist committed Jun 15, 2024
commit 35a77ab0ad5c9b67ea560523040e866343dc63fb
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,22 @@ public void insert(int key) {
}

/**
* Recursive function to insert a key.
* Recursive function to insert a key into a subtree.
*
* @param root The root of the subtree to insert the key into.
* @param key The key to insert.
* @return The root of the modified subtree.
* @return The root of the modified subtree after insertion.
* @throws IllegalArgumentException If the key to be inserted already exists in the subtree.
*/
private Node insertRec(Node root, int key) {
Comment thread
sozelfist marked this conversation as resolved.
Outdated
if (root == null) return new Node(key);

if (root.key > key) {
if (key < root.key) {
root.left = insertRec(root.left, key);
} else if (root.key < key) {
} else if (key > root.key) {
root.right = insertRec(root.right, key);
} else {
throw new IllegalArgumentException("Duplicate key: " + key);
Comment thread
sozelfist marked this conversation as resolved.
Outdated
}

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

/**
* Delete a key from the SplayTree.
* Deletes a key from the SplayTree.
*
* @param key The key to delete.
* @throws IllegalArgumentException If the tree is empty.
*/
public void delete(int key) {
Comment thread
sozelfist marked this conversation as resolved.
Outdated
root = deleteRec(root, key);
}

/**
* Recursive function to delete a key.
*
* @param root The root of the subtree to delete the key from.
* @param key The key to delete.
* @return The root of the modified subtree.
*/
private Node deleteRec(Node root, int key) {
if (root == null) return null;

if (root.key > key) {
root.left = deleteRec(root.left, key);
} else if (root.key < key) {
root.right = deleteRec(root.right, key);
} else {
// Found the node to delete
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
if (root == null) {
throw new IllegalArgumentException("Cannot delete from an empty tree");
Comment thread
sozelfist marked this conversation as resolved.
Outdated
}
Comment thread
sozelfist marked this conversation as resolved.
Outdated

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

// Delete the inorder successor
root.right = deleteRec(root.right, root.key);
// If the key is not found at the root, return without deleting
if (root.key != key) {
return;
}

return root;
}

/**
* Find the minimum value in a subtree.
*
* @param root The root of the subtree to find the minimum value in.
* @return The minimum value in the subtree.
*/
private int minValue(Node root) {
int minValue = root.key;
while (root.left != null) {
minValue = root.left.key;
root = root.left;
// Handle deletion
if (root.left == null) {
root = root.right;
} else {
Node temp = root;
// Splay to bring the largest key in left subtree to root
root = splay(root.left, key);
root.right = temp.right;
}
return minValue;
}

/**
Expand Down