-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
base: master
Are you sure you want to change the base?
Implement SplayTree #5142
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5142 +/- ##
============================================
+ Coverage 38.72% 39.03% +0.31%
- Complexity 2383 2416 +33
============================================
Files 517 518 +1
Lines 15392 15472 +80
Branches 2954 2973 +19
============================================
+ Hits 5961 6040 +79
Misses 9144 9144
- Partials 287 288 +1 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this 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).
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/datastructures/trees/SplayTreeTest.java
Outdated
Show resolved
Hide resolved
cbe94ae
to
626ef44
Compare
The current implementation of the Splay Tree does not handle duplicate keys explicitly. Here are a few possible approaches:
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. |
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. |
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. |
cc5dc27
to
92a4b73
Compare
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. |
92a4b73
to
37ce557
Compare
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
63179e3
to
3e1353f
Compare
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/datastructures/trees/SplayTreeTest.java
Outdated
Show resolved
Hide resolved
d6264d3
to
6b3816b
Compare
src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java
Outdated
Show resolved
Hide resolved
if (root.left.right != null) { | ||
root.left = rotateLeft(root.left); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no test with root.left.right == null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will test the delete and search methods on more items. I am doing my best to ensure that the tests cover this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure how to get this line covered by the current tests. I've tried searching and deleting multiple keys to trigger the event played by this line, but it hasn't been successful. Is it necessary for this implementation to have a 100% coverage ratio? If so, can you propose a solution to make the tests cover this line? I would appreciate it if you give suggestions for tests that make the implementation perfect.
850c378
to
fb62dab
Compare
7024614
to
0b18bab
Compare
Can you have a look at the PR @vil02. |
@ParameterizedTest | ||
@MethodSource("traversalStrategies") | ||
public void testTraversal(SplayTree.TreeTraversal traversal, List<Integer> expected) { | ||
SplayTree tree = createComplexTree(); | ||
List<Integer> result = tree.traverse(traversal); | ||
assertEquals(expected, result); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to have the 100% coverage, I think that this this test should be also parametrized over the tree
. Could you please rewrite this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will do it
- 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]
- Implement 3 traversal class of the `TreeTraversal` interface - Addjust tests to better cover code.
- Make TreeTraversal interface implementations final - Update tests
0b18bab
to
f956794
Compare
clang-format -i --style=file path/to/your/file.java