Skip to content

Fix radix tree delete merging the root into its only child - #15795

Open
JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-radix-tree-delete-root-merge
Open

JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-radix-tree-delete-root-merge

Conversation

@JayPokale

@JayPokale JayPokale commented Nov 24, 2026 •

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

data_structures/trie/radix_tree.py::RadixNode.delete merges a node with its only child once the node has lost all but one edge and is not a word itself. The same code also runs when self is the tree root (empty prefix), so deleting a word from a tree whose root then has one edge folds that edge into the root: the root gets the child's prefix and find can no longer reach it. Reproducer on master: insert_many(["c", "abcd"]), delete("c") returns True, then find("abcd") is False. The merge now only applies to nodes that have a prefix, which excludes the root.

I added a doctest with that sequence to delete; it fails on master, and I kept it in this PR as the regression evidence for this one-condition fix. I also ran 1500 random insert, delete and find sequences over ten short words against a Python set, checking every word after each operation: with the change there are no mismatches over three seeds, and on master seed 1 produced 206 wrong find results. The existing tests in the file still pass. ruff check, ruff format --check and python -m doctest pass.

This is unrelated to #15673 (empty-string handling in find and delete); the two touch neighbouring lines of delete, so whichever lands second may need a trivial rebase.

Written with the help of Claude Code (Claude Sonnet 5.5).

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".

This branch has not been deployed

No deployments
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.

1 participant