Skip to content

fix(data_structures): handle exact prefix match in RadixNode.insert to fix IndexError (#11316)#14835

Open
maitriupadhyay03-cell wants to merge 3 commits into
TheAlgorithms:masterfrom
maitriupadhyay03-cell:fix/radix-tree-exact-prefix-insert-11316
Open

fix(data_structures): handle exact prefix match in RadixNode.insert to fix IndexError (#11316)#14835
maitriupadhyay03-cell wants to merge 3 commits into
TheAlgorithms:masterfrom
maitriupadhyay03-cell:fix/radix-tree-exact-prefix-insert-11316

Conversation

@maitriupadhyay03-cell

Copy link
Copy Markdown

Describe your change:

Fixes #11316

Fixes an IndexError when 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 called self.nodes[matching_string[0]].insert(remaining_word) even when remaining_word was "". The recursive call then hit Case 2 which accessed word[0] on an empty string, raising IndexError: string index out of range.

Fix: Check if remaining_word: before recursing. When remaining_word is empty the inserted word exactly matches the incoming node's prefix, so we mark that node is_leaf = True directly.

  • Fix a bug or typo in an existing algorithm?
  • * [x] Add or change doctests?

Checklist:

  • I have read CONTRIBUTING.md.
  • * [x] This pull request is all my own work -- I have not plagiarized.
  • * [x] I know that pull requests will not be merged if they fail the automated tests.
  • * [x] This PR only changes one algorithm file.
  • * [x] All functions have doctests that pass the automated testing.
  • * [x] If this pull request resolves one or more open issues then the commit message contains Fixes: #11316.

…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.
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

data_structures/trie/radix_tree.py wont really end up in 'case 1' for insert

1 participant