Advertisement
Guest User

Untitled

a guest
May 24th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. /* Trie implementation in JavaScript
  2. * @version 0.1
  3. * Author - Jeff Long
  4. */
  5.  
  6. (function() {
  7. /**
  8. * Trie(value) Initialize a new tree with base node x.
  9. *
  10. * @param {String} value
  11. * @return {Trie} trie
  12. */
  13. var Trie = function(value) {
  14. var value = value || '';
  15. this.children = [];
  16. this.endpoint = false;
  17.  
  18. // Add a word if it's passed in
  19. if (value.length > 1) {
  20. this.value = '';
  21. this.add(value);
  22. } else {
  23. this.value = value;
  24. }
  25. return this;
  26. };
  27.  
  28. Trie.prototype = {
  29.  
  30. /**
  31. * add(word) adds a word to the trie
  32. *
  33. * @param {String} word
  34. * @return {Trie} trie
  35. */
  36. add: function(word) {
  37. if (word.forEach) {
  38. word.forEach(function(w) {
  39. this.add(w);
  40. }.bind(this));
  41. return this;
  42. }
  43. var node = this;
  44. for (var i in word) {
  45. var code = word.charCodeAt(i);
  46. if (node.children[code] !== undefined) {
  47. node = node.children[code];
  48. } else {
  49. node = node.children[code] = new Trie(code);
  50. }
  51. }
  52. node.endpoint = true;
  53. return this;
  54. },
  55.  
  56. /**
  57. * getParent(word) returns a parent trie
  58. * based on the passed word
  59. *
  60. * @param {String} word
  61. * @return {Trie|Boolean} parentNode
  62. */
  63. getParent: function(word) {
  64. var parentNode = this;
  65. for (var i in word) {
  66. parentNode = parentNode.children[word.charCodeAt(i)];
  67. if (node === undefined) {
  68. return false;
  69. } else if (parseInt(i) === (word.length - 2)) {
  70. return parentNode;
  71. }
  72. }
  73. return false;
  74. },
  75.  
  76. /**
  77. * remove(word) deletes a word from the trie
  78. *
  79. * @param {String} word
  80. * @return {Boolean} success
  81. */
  82. remove: function(word) {
  83. var parent = this.getParent(word);
  84. var charCode = word.charCodeAt(word.length - 1);
  85. if (!parent) {
  86. return false;
  87. }
  88.  
  89. var node = parent.children[charCode];
  90. if (node.getChildren().length > 0) {
  91. node.endpoint = false;
  92. } else {
  93. delete parent.children[charCode];
  94. }
  95.  
  96. if (parent.getChildren().length === 0) {
  97. node = parent;
  98. var level = 1;
  99. while (node.getChildren().length === 0 && !node.endpoint) {
  100. parent = this.getParent(word.substr(0, (word.length - level))) || this;
  101. delete parent.children[node.value];
  102. node = parent;
  103. level++;
  104. }
  105. }
  106.  
  107. return true;
  108. },
  109.  
  110. /**
  111. * contains(word) determines if a word is in the trie
  112. *
  113. * @param {String} word
  114. * @return {Boolean}
  115. */
  116. contains: function(word) {
  117. var parent = this.getParent(word);
  118. if (!parent || parent.children.length === 0) {
  119. return false;
  120. }
  121. var node = parent.children[word.charCodeAt(word.length - 1)] || {};
  122. return node.hasOwnProperty('endpoint') && node.endpoint !== false;
  123. },
  124.  
  125.  
  126. /**
  127. * getChildren() returns children of current node.
  128. * When you push an element to an array in Javascript at position x,
  129. * all indexes < x are automatically initialized as undefined. This filters
  130. * them out so we can use character codes for indexes.
  131. *
  132. * @return {Array} children
  133. */
  134. getChildren: function() {
  135. return this.children.filter(function(child) { return child !== undefined; });
  136. },
  137.  
  138. /**
  139. * wordCount() returns a count of words from the trie.
  140. *
  141. * @return {Integer} count
  142. */
  143. wordCount: function() {
  144. var count = 0;
  145.  
  146. function countWords(node) {
  147. for (var idx in node.children) {
  148. if (node.children[idx].endpoint) {
  149. count++;
  150. }
  151. countWords(node.children[idx]);
  152. }
  153. }
  154.  
  155. countWords(this);
  156. return count;
  157. },
  158.  
  159. /**
  160. * getJSON() returns a more readable representation of current data.
  161. *
  162. * @return {Object} data
  163. */
  164. getJSON: function() {
  165. var output = {
  166. wordcount: this.wordCount(),
  167. words: []
  168. };
  169.  
  170. function getValues(node, depth) {
  171. for (var i in node.children) {
  172. var child = node.children[i] || false;
  173. if (child) {
  174. output.words.push({ char: String.fromCharCode(child.value),
  175. depth: depth,
  176. endpoint: child.endpoint });
  177. getValues(child, (depth + 1));
  178. }
  179. }
  180. }
  181.  
  182. getValues(this, 0);
  183. return output;
  184. }
  185.  
  186. };
  187.  
  188. module.exports = Trie;
  189. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement