Skip to content

Fixes #3136 The deleteString function was unconditionally resetting child pointers, corrupting words that share prefixes. Fixed by only pruning nodes when they have no children and are not the end of another word. Added a hasChildren() helper method and regression tests.#3142

Open
ckprojects77 wants to merge 1 commit into
TheAlgorithms:masterfrom
ckprojects77:master

Conversation

@ckprojects77
Copy link
Copy Markdown

Fixes #3136

The deleteString function had two bugs:

  1. The recursive call was made on this instead of arr[j], so the
    function never actually descended the trie.
  2. Child nodes were unconditionally reset after deletion, corrupting
    any words that shared a prefix (e.g. deleting "Hello" would destroy "Hell").

Fix: corrected the recursive call to arr[j]->deleteString(...) and added
conditional pruning — nodes are only removed when they have no children and
are not the end of another word. Added a hasChildren() helper and regression
tests covering the shared-prefix case.

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes: Corrects recursive traversal bug and shared-prefix corruption in trie::deleteString.

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.

[BUG] Trie deleteString corrupts words sharing prefixes

1 participant