Skip to content

[ENHANCEMENT] Add Wavelet Tree Data Structure#7414

Open
space0032 wants to merge 3 commits intoTheAlgorithms:masterfrom
space0032:add-wavelet-tree
Open

[ENHANCEMENT] Add Wavelet Tree Data Structure#7414
space0032 wants to merge 3 commits intoTheAlgorithms:masterfrom
space0032:add-wavelet-tree

Conversation

@space0032
Copy link
Copy Markdown

Description

This Pull Request introduces the Wavelet Tree data structure to the datastructures/trees package in the repository. A Wavelet Tree is a highly efficient and versatile data structure used for compressed storage of sequences and answering complex range-based queries in O(log U) time.

The main operations implemented are:

  1. rank(x, i): Determines how many times the value x appears from the beginning of the array up to the index i.
  2. kthSmallest(l, r, k): Finds the k-th smallest element in the subarray [l, r].

The functionality is generalized to handle large input arrays with an arbitrary range of values (minValue and maxValue).

Files Added

  1. WaveletTree.java: Contains the implementation of the Wavelet Tree data structure.
  2. WaveletTreeTest.java: Contains JUnit test cases to validate the correctness of the methods and handle edge cases.

Complexity

  • Time Complexity:
    • Tree Construction: $O(N \log U)$, where $N$ is the length of the array and $U$ is the range of the values.
    • Query Operations: $O(\log U)$ per query.
  • Space Complexity:
    • $O(N \log U)$ for storing the bit vectors.

Copilot AI review requested due to automatic review settings May 6, 2026 09:48
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 6, 2026

Codecov Report

❌ Patch coverage is 94.28571% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.61%. Comparing base (2616e09) to head (04803ba).

Files with missing lines Patch % Lines
...healgorithms/datastructures/trees/WaveletTree.java 94.28% 0 Missing and 6 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7414      +/-   ##
============================================
+ Coverage     79.54%   79.61%   +0.07%     
- Complexity     7178     7218      +40     
============================================
  Files           798      799       +1     
  Lines         23467    23572     +105     
  Branches       4617     4639      +22     
============================================
+ Hits          18666    18767     +101     
+ Misses         4055     4054       -1     
- Partials        746      751       +5     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new WaveletTree data structure under com.thealgorithms.datastructures.trees, providing rank, select, and kthSmallest (quantile) queries over an integer sequence, along with JUnit tests validating core behavior.

Changes:

  • Introduces WaveletTree implementation with rank(x, i), select(x, k), and kthSmallest(l, r, k).
  • Adds JUnit test suite covering typical cases, invalid inputs, empty/singleton arrays, and negative values.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/main/java/com/thealgorithms/datastructures/trees/WaveletTree.java Adds the Wavelet Tree implementation and query methods.
src/test/java/com/thealgorithms/datastructures/trees/WaveletTreeTest.java Adds JUnit tests for rank/select/kthSmallest, including some edge cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +33
if (arr.length == 0 || low == high) {
return;
}

int mid = low + (high - low) / 2;
if (node.low == node.high) {
return count;
}
int mid = node.low + (node.high - node.low) / 2;
if (node.low == node.high) {
return k - 1; // 0-based index within the imaginary array at the leaf
}
int mid = node.low + (node.high - node.low) / 2;
Comment on lines +215 to +217
if (root == null || left > right || left < 0 || k < 1 || k > right - left + 1) {
return -1;
}
if (arr == null || arr.length == 0) {
this.n = 0;
return;
}
Comment on lines +47 to +52
@Test
public void testKthSmallest() {
int[] arr = {5, 1, 2, 5, 1};
WaveletTree wt = new WaveletTree(arr);

// Array: [5, 1, 2, 5, 1] -> Sorted: [1, 1, 2, 5, 5]
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.

3 participants