Skip to content

Change TrieNode method #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions Implement Trie (Prefix Tree).js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,27 @@ You may assume that all inputs are consist of lowercase letters a-z.

// MEMORY LIMIT EXCEEDED
var TrieNode = function() {
var isEnd,
links = {};

return {
containsKey: function(n) {
return links[n] !== undefined;
},
get: function(ch) {
return links[ch];
},
put: function(ch, node) {
links[ch] = node;
},
setEnd: function() {
isEnd = true;
},
isEnd: function() {
return isEnd;
}
};
this.end = false;
this.links = {};
this.containsKey = function(n) {
return this.links[n] !== undefined;
}
this.get = function(ch) {
return this.links[ch];
}
this.put = function(ch, node) {
this.links[ch] = node;
}
this.setEnd = function() {
this.end = true;
}
this.isEnd = function() {
return this.end;
}
};

var Trie = function() {
this.root = TrieNode();
this.root = new TrieNode();
};

/**
Expand All @@ -52,7 +49,7 @@ Trie.prototype.insert = function(word) {
ch = word.charAt(i);

if (!node.containsKey(ch)) {
node.put(ch, TrieNode());
node.put(ch, new TrieNode());
}

node = node.get(ch);
Expand All @@ -68,8 +65,11 @@ Trie.prototype.insert = function(word) {
*/
Trie.prototype.search = function(word) {
var node = this.searchPrefix(word);

return node && node.isEnd();
if(!node){
return false;
}else{
return node.isEnd()
}
};

/**
Expand Down