fix: improve avl to take an interface as a key #2519
Closed
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.
This pull request introduces the following changes and fixes issue #2505
Updated Node Structure:
Modified the Node structure to accommodate a generic Key interface for AVL tree keys. This allows the AVL tree to support any type of key instead of being restricted to strings.
Key Interface:
Introduced a Key interface with two methods: LessThan(Key) bool and Equals(Key) bool. These methods allow comparison operations on keys of any type.
Node Methods:
NewNode: Updated to use the generic Key interface.
Get: Updated to use the generic Key interface for searching nodes.
Set: Modified to insert new nodes using the generic Key interface.
setLeaf: Updated to handle leaf nodes with the generic Key interface.
Remove: Adapted to remove nodes based on the generic Key interface.
Traversal Methods:
Updated TraverseInRange, TraverseByOffset, and their helper methods to work with the generic Key interface for traversing nodes within a range or by offset.
Balance and Rotation:
Updated balancing and rotation methods (rotateRight, rotateLeft, balance) to ensure the AVL tree remains balanced after insertions and deletions.
Contributors' checklist...
Fix Issue #2505 Added new tests, or not needed, or not feasible Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory Updated the official documentation or not needed No breaking changes were made Added references to related issues and PRs