DEV Community

Discussion on: Learning Tries in Javascript

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

This code is very confused. You define a Node class and only use it once. Further code reverts to using plain objects. You might want to revise this example to either use the Node class throughout (and its data, children properties etc.), or just use plain objects throughout (remove the Node class, and make root {}).

You've also stated that:

A Trie is a data structure thats main purpose is retrieval

But you haven't shown an example of how that can be done! (Hint: the original Node class might be useful for this, if fully integrated)

Other issues:

  • The search function returns null or true instead of true/false as stated
  • The node !== null part of the return expression is entirely redundant, since it is always true
  • Even if you remove the redundant first term of the expression, the remainder still has redundancy. You could simply just return node.isEndOfWord (although this would still be returning null/true - but this isn't a big issue as JS has the concept of truthiness and falsiness)
Collapse
 
jonrandy profile image
Jon Randy 🎖️

UPDATE: On further inspection, it's more luck than judgement that the search method even works - especially where you are checking for === null - you won't find a null anywhere. Overall, your code will be finding a lot of undefineds everywhere