Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix radix_tree.py insertion fail in ["*X", "*XX"] cases #8870

Merged
merged 3 commits into from
Jul 24, 2023

Conversation

furthermares
Copy link
Contributor

@furthermares furthermares commented Jul 16, 2023

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 the is_leaf check, as the code already assumes you only input new words.

  • Test Case: "A AA AAA AAAA AAAAA AAAAAA"
    • Fixed correct output:
    Words:['A', 'AA', 'AAA', 'AAAA', 'AAAAA', 'AAAAAA']
    Tree:
    - A   (leaf)
    -- A   (leaf)
    --- A   (leaf)
    ---- A   (leaf)
    ----- A   (leaf)
    ------ A   (leaf)
    
    • Current incorrect output:
    Words: ['A', 'AA', 'AAA', 'AAAA', 'AAAAA', 'AAAAAA']
    Tree:
    - A   (leaf)
    -- AA   (leaf)
    --- A   (leaf)
    ---- AA   (leaf)
    
  1. ✅ Insert "A". Node is empty. Insert "A". Node prefix = "A".
  2. ❌ Insert "AA". Compare node prefix "A" to "AA". "A" == "A". Fails to insert. No change.
  3. ✅ Insert "AAA". Compare node prefix "A" to "AAA". "A" != "AA". Insert "AAA". Node prefix = "AA".
  4. ✅ Insert "AAAA". Compare node prefix "AA" to "AAAA". "AA" != "A". Case 2: Since "AAA" exists, branch and insert "AAAA". Node prefix = "A". Follows the Step 1 pattern.
  5. ❌ Insert "AAAAA". Follows the Step 2 pattern.
  6. ✅ Insert "AAAAAA". Follows the Step 3 pattern. (And the next step would follow Step 1's.)

N.B. It passed test cases for Croatian Open Competition in Informatics 2012/2013 Contest #3 Task 5 HERKABE

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Consider a word, and a copy of that word, but with the last letter repeating 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 letter of the node itself. (e.g., `nodes: {'C' : RadixNode()}, is_leaf: True, prefix: 'C'`) It can be easily fixed by simply adding the `is_leaf` check, asking if there are more letters to be added.

- Test Case: `"A AA AAA AAAA"`
  - Fixed correct output:
  ```
  Words: ['A', 'AA', 'AAA', 'AAAA']
  Tree:
  - A   (leaf)
  -- A   (leaf)
  --- A   (leaf)
  ---- A   (leaf)
  ```
  - Current incorrect output:
  ```
  Words: ['A', 'AA', 'AAA', 'AAAA']
  Tree:
  - A   (leaf)
  -- AA   (leaf)
  --- A   (leaf)
  ```

*N.B.* This passed test cases for [Croatian Open Competition in Informatics 2012/2013 Contest TheAlgorithms#3 Task 5 HERKABE](https://hsin.hr/coci/archive/2012_2013/)
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Jul 24, 2023
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Thanks for doing this.

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Jul 24, 2023
@cclauss cclauss merged commit a03b739 into TheAlgorithms:master Jul 24, 2023
@cclauss cclauss mentioned this pull request Jul 24, 2023
14 tasks
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
…hms#8870)

* Fix insertion fail in ["*X", "*XX"] cases

Consider a word, and a copy of that word, but with the last letter repeating 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 letter of the node itself. (e.g., `nodes: {'C' : RadixNode()}, is_leaf: True, prefix: 'C'`) It can be easily fixed by simply adding the `is_leaf` check, asking if there are more letters to be added.

- Test Case: `"A AA AAA AAAA"`
  - Fixed correct output:
  ```
  Words: ['A', 'AA', 'AAA', 'AAAA']
  Tree:
  - A   (leaf)
  -- A   (leaf)
  --- A   (leaf)
  ---- A   (leaf)
  ```
  - Current incorrect output:
  ```
  Words: ['A', 'AA', 'AAA', 'AAAA']
  Tree:
  - A   (leaf)
  -- AA   (leaf)
  --- A   (leaf)
  ```

*N.B.* This passed test cases for [Croatian Open Competition in Informatics 2012/2013 Contest #3 Task 5 HERKABE](https://hsin.hr/coci/archive/2012_2013/)

* Add a doctest for previous fix

* improve doctest readability
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants