Python/data_structures
Sangmin Jeon a03b739d23
Fix radix_tree.py insertion fail in ["*X", "*XX"] cases (#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
2023-07-24 11:29:05 +02:00
..
arrays Algorithm: Calculating Product Sum from a Special Array with Nested Structures (#8761) 2023-06-23 10:26:05 +02:00
binary_tree [Upgrade Ruff] Fix all errors raised from ruff (#8879) 2023-07-22 12:05:10 +02:00
disjoint_set Add more ruff rules (#8767) 2023-05-26 09:34:17 +02:00
hashing Bloom Filter (#8615) 2023-04-08 19:39:24 +02:00
heap Solving the Top k most frequent words problem using a max-heap (#8685) 2023-04-27 23:02:07 +05:30
linked_list Add more ruff rules (#8767) 2023-05-26 09:34:17 +02:00
queue [pre-commit.ci] pre-commit autoupdate (#8853) 2023-07-04 00:20:35 +02:00
stacks Add more ruff rules (#8767) 2023-05-26 09:34:17 +02:00
trie Fix radix_tree.py insertion fail in ["*X", "*XX"] cases (#8870) 2023-07-24 11:29:05 +02:00
__init__.py Add __init__.py files in all the directories (#2503) 2020-09-28 19:42:36 +02:00