Implement a Class that stores words in a Trie data structure
Do your work in the lib/Trie.js
file and follow the instructions in the comments.
-
Create a class called
Trie
. -
Create two instance methods called
add
andexists
. -
The internal data storage must be a trie data structure.
pronouced "trie" as in
reTRIEve
Trie, a.k.a. a digital tree, is a search tree in which we can store data in a tree-like structure. In the case of this exercise, nodes (letters) will chain upon each other to build out a word. The last node of a word is called a leaf, signifying the end of a valid word.
For example, the word, cat
, catch
, and car
.
root
|
└── c
|
└── a
|
├─- t (leaf)
| |
| └── c
| |
| └── h (leaf)
|
└── r (leaf)
Running node on the index.js
file located in the root of the repository will be your interactive test (in addition to the tests spec'd out in the tests
directory).
let trie = new Trie();
trie.add('cat');
trie.add('dog');
trie.exists('cat'); // returns true
trie.exists('dog'); // returns true
trie.exists('mouse'); //returns false
- Fork and clone this repository from your personal GitHub Account
- In the terminal, navigate to the directory of the repository
- Install dependencies by running the command:
npm install
- Run your tests by running the command
npm test
- Your work will be done in the file
Trie.js
in thelib
directory. - Make your tests pass!
Commit often!