-
Notifications
You must be signed in to change notification settings - Fork 19.8k
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
Added Heavy-Light Decomposition (HLD) for Efficient Tree Queries #6169
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #6169 +/- ##
============================================
+ Coverage 73.97% 74.12% +0.14%
- Complexity 5157 5191 +34
============================================
Files 662 663 +1
Lines 17681 17771 +90
Branches 3407 3422 +15
============================================
+ Hits 13079 13172 +93
+ Misses 4097 4096 -1
+ Partials 505 503 -2 ☔ 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.
The code looks good, please fix PR checks so that I could merge it
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.
Looks good, thanks a lot for adding this!
Description:
This PR introduces an implementation of Heavy-Light Decomposition (HLD) in Java, enabling efficient path queries (such as maximum, sum, or updates) on a tree using segment trees. HLD reduces query time complexity from O(N) to O(log N) by decomposing the tree into heavy and light chains. Instead of traversing the whole tree (O(N)), we use HLD to jump chains and use segment trees for fast path queries in O(logN).
✅ Features:
Example:
Consider the below Tree
Path Queries and Expected Results
Maximum in Path (4 → 5)
Path: 4 → 2 → 5
Values along the path: {40, 20, 50}
Max Value: 50
Maximum in Path (3 → 4)
Path: 3 → 1 → 2 → 4
Values along the path: {30, 10, 20, 40}
Max Value: 40
Note: Edges are added with indices to define the tree's structure, while node values are initialized separately. This separation allows for flexible updates and queries without altering the tree's topology, ensuring greater efficiency in handling updates and queries.
I have a strong passion for algorithms, particularly their efficiency, and am eager to contribute to this project. Let me know if any changes are needed! 🚀😊
clang-format -i --style=file path/to/your/file.java