From e6689a70c480348480471527a7c085028d25d2cc Mon Sep 17 00:00:00 2001 From: Huanxin Hu Date: Sat, 3 Mar 2018 16:52:22 -0800 Subject: [PATCH] Change TrieNode method Use closure for TrieNode will cause memory out, use constructor function to implement the TrieNode --- Implement Trie (Prefix Tree).js | 48 ++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Implement Trie (Prefix Tree).js b/Implement Trie (Prefix Tree).js index 481c918..f8c9f6a 100644 --- a/Implement Trie (Prefix Tree).js +++ b/Implement Trie (Prefix Tree).js @@ -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(); }; /** @@ -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); @@ -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() + } }; /**