{"id":155,"date":"2010-11-07T11:30:34","date_gmt":"2010-11-07T11:30:34","guid":{"rendered":"http:\/\/odhyan.com\/blog\/?p=155"},"modified":"2017-04-04T15:29:16","modified_gmt":"2017-04-04T09:59:16","slug":"trie-implementation-in-javascript","status":"publish","type":"post","link":"https:\/\/odhyan.com\/blog\/2010\/11\/trie-implementation-in-javascript\/","title":{"rendered":"Trie implementation in Javascript"},"content":{"rendered":"<p>A trie, or prefix tree, is a multi-way tree structure useful for storing strings over an alphabet. It is very efficient and handy and can be used for various purposes like auto-complete, spell-check, dictionary search, etc.<\/p>\n<p>\nThe basic idea of a Trie goes as below:<\/p>\n<ul>\n<li>We have a root node which represents an empty string.<\/li>\n<li>Every other node either represents a word or a prefix of one or more words.<\/li>\n<li>Each node can have atmost as many children as the size of the alphabet.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/File:Trie_example.svg\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 1px solid #000;\" src=\"http:\/\/odhyan.com\/blog\/wp-content\/uploads\/2010\/11\/trie-example.png\" alt=\"Trie example\" width=\"401\" height=\"379\" \/><\/a><\/p>\n<p>The above diagram shows a trie which stores the words &#8220;A&#8221;, &#8220;to&#8221;, &#8220;tea&#8221;, &#8220;ted&#8221;, &#8220;ten&#8221;, &#8220;i&#8221;, &#8220;in&#8221;, and &#8220;inn&#8221;.<\/p>\n<p><strong>Below is my implementation of Trie in javascript:<\/strong><\/p>\n<p>At each node of Trie, I store the word-count, prefix-count and the link to children.<\/p>\n<pre lang=\"javascript\">\r\nTrie = function() {\r\n    this.words = 0;\r\n    this.prefixes = 0;\r\n    this.children = [];\r\n};\r\n<\/pre>\n<p>To insert a word, I recursively traverse through the trie nodes if they already exist, else I create new nodes. While traversing the nodes, I keep on storing the information in the nodes as I encounter each character in the given string.<\/p>\n<pre lang=\"javascript\">\r\ninsert: function(str, pos) {\r\n    if(str.length == 0) { \/\/blank string cannot be inserted\r\n        return;\r\n    }\r\n    var T = this,\r\n        k,\r\n        child;\r\n    if(pos === undefined) {\r\n        pos = 0;\r\n    }\r\n    if(pos === str.length) {\r\n        T.words ++;\r\n        return;\r\n    }\r\n    T.prefixes ++;\r\n    k = str[pos];\r\n    if(T.children[k] === undefined) { \/\/if node for this char doesn't exist, create one\r\n        T.children[k] = new Trie();\r\n    }\r\n    child = T.children[k];\r\n    child.insert(str, pos + 1);\r\n}\r\n<\/pre>\n<p>Removing a word follows the similar idea as insertion.<\/p>\n<p>To update, I simply remove the existing word and insert the new word.<\/p>\n<p>Searching a word is trivial as we just need to check if the word count at the terminating node of the input string is greater than zero or not.<\/p>\n<pre  lang=\"javascript\">\r\nfind: function(str) {\r\n    if(str.length == 0) {\r\n        return false;\r\n    }\r\n    if(this.countWord(str) > 0) {\r\n        return true;\r\n    } else {\r\n        return false;\r\n    }\r\n}\r\n<\/pre>\n<p>You can get the full source code in <a href=\"https:\/\/github.com\/odhyan\/trie\">my GitHub repository<\/a><\/p>\n<p>Meanwhile you can see <a href=\"http:\/\/odhyan.com\/trie\/autocomplete.html\">the auto-complete demo using my trie implementation<\/a><\/p>\n<p><strong>Links:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Trie\">Trie on Wikipedia<\/a><\/li>\n<li><a href=\"http:\/\/www.csse.monash.edu.au\/~lloyd\/tildeAlgDS\/Tree\/Trie\/\">Tries by Lloyd Allison<\/a><\/li>\n<li><a href=\"http:\/\/www.topcoder.com\/tc?module=Static&amp;d1=tutorials&amp;d2=usingTries\">TopCoder tutorial on Tries<\/a><\/li>\n<li><a href=\"http:\/\/odhyan.com\/trie\/autocomplete.html\">AutoComplete demo using Trie<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A trie, or prefix tree, is a multi-way tree structure useful for storing strings over an alphabet. It is very efficient and handy and can be used for various purposes like auto-complete, spell-check, dictionary search, etc. The basic idea of a Trie goes as below: We have a root node which represents an empty string.&hellip;<\/p>\n <a href=\"https:\/\/odhyan.com\/blog\/2010\/11\/trie-implementation-in-javascript\/\" title=\"Trie implementation in Javascript\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">Trie implementation in Javascript<\/span><\/a>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[19,10],"class_list":["entry","author-saurabh","post-155","post","type-post","status-publish","format-standard","category-programming","tag-algorithms","tag-javascript"],"_links":{"self":[{"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":42,"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/posts\/155\/revisions\/402"}],"wp:attachment":[{"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/odhyan.com\/blog\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}