Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SplayTree #5142

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

TruongNhanNguyen
Copy link
Contributor

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented May 5, 2024

Codecov Report

Attention: Patch coverage is 96.42857% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 38.94%. Comparing base (ea4dc15) to head (d6264d3).

Files Patch % Lines
.../thealgorithms/datastructures/trees/SplayTree.java 96.42% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5142      +/-   ##
============================================
+ Coverage     38.63%   38.94%   +0.31%     
- Complexity     2379     2417      +38     
============================================
  Files           516      517       +1     
  Lines         15385    15469      +84     
  Branches       2957     2974      +17     
============================================
+ Hits           5944     6025      +81     
  Misses         9157     9157              
- Partials        284      287       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about introducing a method: List<Integer> traverse(TraverseOrder traverseOrder), where TraverseOrder is an enum? This will allow to make the other methods private.

Does your implementation work for duplicated keys?

Please add the missing tests and please update your branch (there were some changes regarding the static analysis).

@TruongNhanNguyen
Copy link
Contributor Author

Does your implementation work for duplicated keys?

The current implementation of the Splay Tree does not handle duplicate keys explicitly. Here are a few possible approaches:

  1. Allow Duplicates: We can modify the implementation to allow duplicate keys. When inserting a node with a duplicate key, we can insert it as a new node in the tree or update an existing node with the same key.
  2. Reject Duplicates: Alternatively, we can reject duplicate keys altogether. In this case, when inserting a node with a key already in the tree, we can either ignore the insertion or throw an exception to indicate that duplicates are not allowed.
  3. Especially handle Duplicates: We can define a specific behavior for handling duplicate keys, such as keeping track of the number of occurrences of each key or maintaining a list of values associated with each key.

I think the simplest and most used approach is that when inserting a node with a duplicated key, we should update the existing node with the same key.

What do you think about these approaches, we will choose the one to implement.

@vil02
Copy link
Member

vil02 commented May 22, 2024

I think we should not allow duplicate keys and explicitly throw an exception if one is trying to include already existing key. I looked at the existing implementations of BST in this repository and it is a total mess - each implementation reacts differently.

@TruongNhanNguyen
Copy link
Contributor Author

TruongNhanNguyen commented May 23, 2024

I have updated the test cases, but there are a few partially covered lines. If you have any suggestion that make the code to be 100% covered, feel free to propose.

@TruongNhanNguyen
Copy link
Contributor Author

I think we should not allow duplicate keys and explicitly throw an exception if one is trying to include an already existing key. I looked at the existing implementations of BST in this repository and it is a total mess - each implementation reacts differently.

I have refactored the implementation to disallow inserting duplicated keys and to throw an error if this occurs. I also handle deletion on an empty tree and simplify the deletion process.

Comment on lines 205 to 217
if (root == null) {
throw new IllegalArgumentException("Cannot delete from an empty tree");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an isEmpty method. Otherwise using this class might be very annoying.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can define the isEmpty method and use it to check the entire tree's root and continue using root == null checks within recursive calls because root is a local variable that represents the current node being processed in the recursion.

Comment on lines 236 to 258
switch (traverseOrder) {
case IN_ORDER:
inOrderRec(root, result);
break;
case PRE_ORDER:
preOrderRec(root, result);
break;
case POST_ORDER:
postOrderRec(root, result);
break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add default (cf. #5179).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

- 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.
- inner assignments should be avoided
- Add `isEmpty` method to check to check the entire tree root and continue using `root == null` checks within recursive calls
- Add default branch to switch statement
Each variable declaration must be in its own statement. [MultipleVariableDeclarations]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants