fix(data_structures): handle exact prefix match in RadixNode.insert to fix IndexError (#11316)#14835
Open
maitriupadhyay03-cell wants to merge 3 commits into
Conversation
…gorithms#7854) The split() function in treap.py uses `<` comparison but the docstring states that the right subtree should contain values "greater or equal" to the split value. This fix changes `elif value < root.value:` to `elif value <= root.value:` so that equal values go to the right subtree as documented. Fixes TheAlgorithms#7854
…o fix IndexError Fixes TheAlgorithms#11316 The insert() method's Case 3 (remaining_prefix == "") would call self.nodes[matching_string[0]].insert(remaining_word) even when remaining_word is empty string. This causes an IndexError in Case 2 when word[0] is accessed on an empty string. Fix: Check if remaining_word is non-empty before recursing. If remaining_word is empty, it means the inserted word exactly matches the prefix of the incoming node, so we just mark that node as a leaf. Also adds a doctest for the reported scenario: insert('fooaaa'), insert('foobbb'), insert('foo') And adds assertions to test_trie() to cover this case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your change:
Fixes #11316
Fixes an
IndexErrorwhen inserting a word that is a prefix of an already-inserted word (e.g. inserting"foo"after"fooaaa"and"foobbb").Root cause: In
insert(), Case 3 (remaining_prefix == "") unconditionally calledself.nodes[matching_string[0]].insert(remaining_word)even whenremaining_wordwas"". The recursive call then hit Case 2 which accessedword[0]on an empty string, raisingIndexError: string index out of range.Fix: Check
if remaining_word:before recursing. Whenremaining_wordis empty the inserted word exactly matches the incoming node's prefix, so we mark that nodeis_leaf = Truedirectly.Checklist:
Fixes: #11316.