feat: add self-balancing scapegoat tree implementation #3095
+1,648
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Change
This PR adds a comprehensive implementation of a scapegoat tree, a self-balancing binary search tree. Unlike AVL or Red-Black trees, a scapegoat tree does not require extra storage (like height or color bits) per node, making it memory-efficient while still guaranteeing$O(\log n)$ amortized performance for basic operations.
Changes:
Self-Balancing Logic: Implemented find_traitor and restructure_subtree to handle α-weight balancing.
Modern C++
Features:
Added a custom iterator class for STL-compatible traversal.
Provided support for range-based for loops.
Advanced Queries: Added methods for kth_smallest, sum_in_range, and values_in_range.
Bulk Operations: Included insert_batch and delete_batch for efficient data handling.
Robust Testing: Included a comprehensive test suite in scapegoat_tests.cpp using assert() to verify tree properties and balancing after various operations.
Checklist
Notes:$O(\log n)$ operations, and α-weight balancing without per-node memory overhead.
A high-performance, self-balancing Scapegoat Tree implementation featuring STL-compatible iterators, amortized