Create a Trie Search Tree Solution

What is your hint or solution suggestion?

Summary
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
var Node = function () {
  this.keys = new Map();
  this.end = false;
  this.setEnd = function () {
    this.end = true;
  };
  this.isEnd = function () {
    return this.end;
  };
};
var Trie = function () {
  // Only change code below this line
  this.root = new Node();

  this.add = (word) => {
    if (!word || !word.trim()) return;
    const charQueque = word.trim().split("");

    const doAdd = (node) => {
      if (charQueque.length === 0) {
        node.setEnd(true);
        return;
      }
      const char = charQueque.shift();
      let childNode = node.keys.get(char);

      if (!childNode) {
        childNode = new Node();
        node.keys.set(char, childNode);
      }
      doAdd(childNode);
    };
    doAdd(this.root);
  };

  this.isWord = (word) => {
    if (!word || !word.trim()) return false;
    const charQueque = word.trim().split("");

    const doIsWord = (node) => {
      if (charQueque.length === 0) {
        return node.isEnd();
      }
      let childNode = node.keys.get(charQueque.shift());
      return childNode ? doIsWord(childNode) : false;
    };
    return doIsWord(this.root);
  };

  this.print = () => {
    const words = [];
    const chars = [];

    const doPrint = (node) => {
      if (node.isEnd()) {
        words.push(chars.join(""));
      }
      for (const [char, child] of node.keys) {
        chars.push(char);
        doPrint(child);
        chars.pop();
      }
    };
    doPrint(this.root);
    return words;
  };
  // Only change code above this line
};

Challenge: Create a Trie Search Tree

Link to the challenge:

Hello there.

Thank you, for your contribution. For future contributions, please wrap your solution within :

[details]
```
code goes here...
```
[/details]

Also, provide all of the necessary code to pass the challenge.

Also, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.