Skip to content

Commit

Permalink
add trie
Browse files Browse the repository at this point in the history
  • Loading branch information
sontran1 committed Mar 15, 2018
1 parent ca921f3 commit 101e5f3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Hash tables](#htable)
* [Map](#map)
* [Linked list](#llist)
* [Trie](#trie)

### Stack <a name="stack"></a>
![Stack](pics/stack.jpeg)
Expand Down Expand Up @@ -748,4 +749,10 @@
console.log(util.inspect(myll.head(), {showHidden: false, depth: null})); // Node {element: 'lalala',next: Node {element: 12,next:Node {element: 'lolo',next: Node { element: 34, next: Node { element: 43, next: null } } } } }
console.log(myll.removeAt(2)); // undefined
console.log(util.inspect(myll.head(), {showHidden: false, depth: null})); // Node {element: 'lalala',next: Node {element: 12,next: Node { element: 34, next: Node { element: 43, next: null } } } }
```
```


### Trie <a name="trie"></a>
![Trie](pics/trie.png)
* An use case is to validate if a word in the dictionary
```javascript
Binary file added pics/trie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions trie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const Node = function() {
this.keys = new Map();
this.end = false;
this.setEnd = function() { this.end = true };
this.isEnd = function() { return this.end };
}

let Trie = function() {
this.root = new Node();
this.add = function(input, node = this.root) { // add word to trie
if (input.length == 0) {
node.setEnd();
return;
} else if (!node.keys.has(input[0])) {
node.keys.set(input[0], new Node());
return this.add(input.substr(1), node.keys.get(input[0]));
} else {
return this.add(input.substr(1), node.keys.get(input[0]));
};
};
this.isWord = function(word) { // check if word is in trie
let node = this.root;
while (word.length > 1) {
if (!node.keys.has(word[0])) {
return false;
} else {
node = node.keys.get(word[0]);
word = word.substr(1);
};
};
return (node.keys.has(word) && node.keys.get(word).isEnd()) ?
true : false;
};

this.print = function() {
let words = new Array();
let search = function(node, string) {
if (node.keys.size != 0) {
for (let letter of node.keys.keys()) {
search(node.keys.get(letter), string.concat(letter));
};
if (node.isEnd()) {
words.push(string);
};
} else {
string.length > 0 ? words.push(string) : undefined;
return;
};
};
search(this.root, new String());
return words.length > 0 ? words : mo;
};

};

myTrie = new Trie()
myTrie.add('ball');
myTrie.add('bat');
myTrie.add('doll');
myTrie.add('dork');
myTrie.add('do');
myTrie.add('dorm')
myTrie.add('send')
myTrie.add('sense')
console.log(myTrie.isWord('doll')); // true
console.log(myTrie.isWord('dor')) // false
console.log(myTrie.isWord('dorf')) // false
console.log(myTrie.print()); // [ 'ball', 'bat', 'doll', 'dork', 'dorm', 'do', 'send', 'sense' ]

0 comments on commit 101e5f3

Please sign in to comment.