[PR #927] Rewrite the trie example #1461

Open
opened 2026-01-29 15:20:58 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/TheAlgorithms/C/pull/927

State: closed
Merged: Yes


The trie example had some issues;

  • It did not follow the code convention in CONTRIBUTING.md
  • The createTrieNode used an inefficient zeroing method (looping over
    the entries) which also does not zero out holes in the structure (e.g.
    an alternative would be to use "*node = &(TrieNode){0}", but calloc
    does all that anyway
  • It used an inefficient and clumsy printArray method
  • It used strlen inside the algorithm; this new method could get rid of
    any strlen/strnlen usage (inserts/searches could be sanitized by
    snprintf)
  • This version can allow for a custom mapping function, e.g. if NULL is
    a valid separator (say that you want a trie for certain binary
    packages)
  • The previous version actually contained out-of-bounds array indexing;
    there were no checks for out-of-bound indexing and words in the word
    list did contain out of bounds words. It's a surprise it was working
    so well.
  • This version just returns 'int' to allow for error checks (instead of
    a printf inside the algorithm), and uses double pointers for return
    values (good practice)
  • The usage example contained unnecessary mallocs, switched that out for
    scanf. The example is just an example after all, in real applications
    you'd have better input sanitazion.

Description of Change

References

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • 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:

**Original Pull Request:** https://github.com/TheAlgorithms/C/pull/927 **State:** closed **Merged:** Yes --- The trie example had some issues; * It did not follow the code convention in CONTRIBUTING.md * The createTrieNode used an inefficient zeroing method (looping over the entries) which also does not zero out holes in the structure (e.g. an alternative would be to use "*node = &(TrieNode){0}", but calloc does all that anyway * It used an inefficient and clumsy printArray method * It used strlen inside the algorithm; this new method could get rid of any strlen/strnlen usage (inserts/searches could be sanitized by snprintf) * This version can allow for a custom mapping function, e.g. if NULL is a valid separator (say that you want a trie for certain binary packages) * The previous version actually contained out-of-bounds array indexing; there were no checks for out-of-bound indexing and words in the word list did contain out of bounds words. It's a surprise it was working so well. * This version just returns 'int' to allow for error checks (instead of a printf inside the algorithm), and uses double pointers for return values (good practice) * The usage example contained unnecessary mallocs, switched that out for scanf. The example is just an example after all, in real applications you'd have better input sanitazion. #### Description of Change <!-- Thank you for your Pull Request. Please provide a description above and review the requirements below. Contributors guide: https://github.com/TheAlgorithms/C/blob/master/CONTRIBUTING.md --> #### References <!-- Add any reference to previous pull-request or issue --> #### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [x] Added description of change - [x] Added file name matches [File name guidelines](https://github.com/TheAlgorithms/C/blob/master/CONTRIBUTING.md#File-Name-guidelines) - [ ] Added tests and example, test must pass - [x] Relevant documentation/comments is changed or added - [ ] PR title follows semantic [commit guidelines](https://github.com/TheAlgorithms/C/blob/master/CONTRIBUTING.md#Commit-Guidelines) - [x] Search previous suggestions before making a new one, as yours may be a duplicate. - [x] I acknowledge that all my contributions will be made under the project's license. Notes: <!-- Please add a one-line description for developers or pull request viewers --> <a href="https://gitpod.io/#https://github.com/TheAlgorithms/C/pull/927"><img src="https://gitpod.io/button/open-in-gitpod.svg"/></a>
claunia added the pull-request label 2026-01-29 15:20:58 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/C#1461