Fix radix_tree.py
insertion fail in ["*X", "*XX"] cases
#8870
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.
Fixes: #8888
Describe your change:
Consider a word, and a copy of that word, but with the last letter repeated twice. (e.g., ["ABC", "ABCC"])
When adding the second word's last letter, it only compares the previous word's prefix—the last letter of the word already in the Radix Tree: 'C'—and the letter to be added—the last letter of the word we're currently adding: 'C'. So it wrongly passes the "Case 1" check, marks the current node as a leaf node when it already was, then returns when there's still one more letter to add.
The issue arises because
prefix
includes the letters of the node itself. (e.g.,nodes: {'C' : RadixNode()}, is_leaf: True, prefix: 'C'
) It can be easily circumvented by simply adding theis_leaf
check, as the code already assumes you only input new words."A AA AAA AAAA AAAAA AAAAAA"
"A" == "A"
. Fails to insert. No change."A" != "AA"
. Insert "AAA". Node prefix = "AA"."AA" != "A"
. Case 2: Since "AAA" exists, branch and insert "AAAA". Node prefix = "A". Follows the Step 1 pattern.N.B. It passed test cases for Croatian Open Competition in Informatics 2012/2013 Contest #3 Task 5 HERKABE
Checklist: