{"id":2941,"date":"2016-11-15T13:05:53","date_gmt":"2016-11-15T07:35:53","guid":{"rendered":"http:\/\/theoryofprogramming.azurewebsites.net\/?p=2941"},"modified":"2023-11-20T09:08:15","modified_gmt":"2023-11-20T09:08:15","slug":"compressed-trie-tree","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2016\/11\/15\/compressed-trie-tree\/","title":{"rendered":"Compressed Trie Tree"},"content":{"rendered":"\n<p>Hello, people! In this post, we will discuss a commonly used data structure to store strings, the Compress Trie Tree, also known as Radix Tree or Patricia (<em>Practical Algorithm to Retrieve Information Coded in Alphanumeric<\/em>) Tree. If you remember, the problem with a <a href=\"https:\/\/theoryofcoding.com\/index.php\/2015\/01\/16\/trie-tree-implementation\/\" target=\"_blank\" rel=\"noreferrer noopener\">Trie Tree<\/a> is that it consumes a lot\u00a0of memory. So, due to its memory consumption, developers prefer using a compressed trie tree to get the same functionality at the same runtime complexity in memory-critical situations such as in android apps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">&#8220;Compress&#8221;-ing the tree<\/h2>\n\n\n\n<p>Now, the idea of the compressed trie tree is to convert long chains of single-child edges to one single edge. Example, suppose we have two words &#8220;facebook&#8221; and &#8220;facepalm&#8221; in our regular trie&nbsp;tree, it would look like &#8211;<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"1307\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-1.png?resize=468%2C1307&#038;ssl=1\" alt=\"Trie Tree\" class=\"wp-image-2955\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-1.png?w=468&amp;ssl=1 468w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-1.png?resize=107%2C300&amp;ssl=1 107w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-1.png?resize=367%2C1024&amp;ssl=1 367w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-1.png?resize=230%2C642&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-1.png?resize=350%2C977&amp;ssl=1 350w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" data-recalc-dims=\"1\" \/><\/figure>\n\n\n\n<p>It&#8217;s a pretty long one, isn&#8217;t it? Now, how about this one below?<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"479\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-2.png?resize=445%2C479&#038;ssl=1\" alt=\"compressed trie tree\" class=\"wp-image-2959\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-2.png?w=445&amp;ssl=1 445w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-2.png?resize=279%2C300&amp;ssl=1 279w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-2.png?resize=230%2C248&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-2.png?resize=350%2C377&amp;ssl=1 350w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" data-recalc-dims=\"1\" \/><\/figure>\n\n\n\n<p>This one surely looks a lot compact! This is the compressed trie tree. As you can see what we do here is to make long chains of single-child edges into one edge. Unlike a regular trie tree, in a compressed trie tree, the edge labels are strings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Node in Compressed Trie Tree<\/h2>\n\n\n\n<p>For a regular trie tree, our tree\u00a0node looked something like this &#8211;<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/6f2ef90e90090bc4f2c790e0e8b5d13c.js\"><\/script>\n\n\n\n<p>So, in every node, we had an array of references. The first references corresponded to &#8216;a&#8217;, the second to &#8216;b&#8217;, and so on. So, essentially, we had a mapping of alphabets to references. We had a way of saying, &#8220;An edge &#8216;a&#8217; is denoted by this particular element in the array of references&#8221;. Now, in a compressed trie tree, the edges are labeled by strings. Now, we need a way of saying, &#8220;An edge &#8216;face&#8217; is denoted by this\u00a0particular element in the array of references&#8221;. To accomplish this, we re-design our tree node as follows &#8211;<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/f2a26ed99f414e75787d91ba58a52284.js\"><\/script>\n\n\n\n<p>So, what we did is that we added an additional array of Strings along with the array of references. Now <strong>edgeLabel[0]<\/strong> will denote the string starting with &#8216;a&#8217;, <strong>edgeLabel[1]<\/strong> will denote the string starting with &#8216;b&#8217;, and correspondingly, <strong>children[0]<\/strong> will denote the edge with the label <strong>edgeLabel[0]<\/strong>.<\/p>\n\n\n\n<p>Example, in the above diagram, the root node will have <strong>edgeLabel[5]<\/strong> = &#8220;face&#8221; and <strong>children[5]<\/strong> will point to the internal node. The internal node will have <strong>edgeLabel[1]<\/strong> = &#8220;book&#8221; and <strong>children[1]<\/strong> will point to the leaf node which will denote the occurrence of the word &#8220;facebook&#8221;. The same internal node will also have <strong>edgeLabel[15]<\/strong> = &#8220;palm&#8221; and <strong>children[15]<\/strong> will point to the leaf node which will denote the occurrence of the word &#8220;facepalm&#8221;. The rest of the values of <strong>edgeLabel<\/strong> and <strong>children<\/strong> in the internal node will be null.<\/p>\n\n\n\n<p>The above code is written in Java. For Java, it is much better to use StringBuilder rather than String because we would be doing a lot of string manipulation. Using String will heavily slow down your program. If you are not familiar with StringBuilder, you can refer to my <a href=\"https:\/\/theoryofcoding.com\/index.php\/2015\/06\/05\/java-tutorials-strings-string-buffer-builder-in-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">post<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">insert() operation<\/h2>\n\n\n\n<p>All operations in the compressed trie tree are similar to what we would do in a regular trie tree. Insert operation is the one which will differ the most. In the insert operation, we need to take care of a few cases, they are &#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Inserting new node for a new word &#8211;&nbsp;This occurs when&nbsp;the starting character of the word is new and there&#8217;s no edge corresponding to it. This may occur at root, or after traversing to an internal node.<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-3028 aligncenter\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-4.png?resize=758%2C920&#038;ssl=1\" alt=\"compressed-trie-tree-4\" width=\"758\" height=\"920\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-4.png?w=758&amp;ssl=1 758w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-4.png?resize=247%2C300&amp;ssl=1 247w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-4.png?resize=230%2C279&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-4.png?resize=350%2C425&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-4.png?resize=480%2C583&amp;ssl=1 480w\" sizes=\"auto, (max-width: 758px) 100vw, 758px\" data-recalc-dims=\"1\" \/><\/li>\n\n\n\n<li>Inserting a prefix of an existing word &#8211;&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-3032 aligncenter\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-5.png?resize=731%2C626&#038;ssl=1\" alt=\"Inserting prefix into compressed trie tree\" width=\"731\" height=\"626\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-5.png?w=731&amp;ssl=1 731w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-5.png?resize=300%2C257&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-5.png?resize=230%2C197&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-5.png?resize=350%2C300&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-5.png?resize=480%2C411&amp;ssl=1 480w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" data-recalc-dims=\"1\" \/><\/li>\n\n\n\n<li>Inserting a word which has a partial match to an existing edge &#8211; This occurs when we are trying to insert &#8220;this&#8221; when &#8220;there&#8221; is already inserted into the tree. Remember that &#8220;there&#8221; can have further children too, like if &#8220;thereafter&#8221; and &#8220;therein&#8221; are already inserted.<a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?ssl=1\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3109 size-full\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?resize=858%2C1299&#038;ssl=1\" alt=\"breaking words during compressed trie tree insertion\" width=\"858\" height=\"1299\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?w=858&amp;ssl=1 858w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?resize=198%2C300&amp;ssl=1 198w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?resize=676%2C1024&amp;ssl=1 676w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?resize=768%2C1163&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?resize=230%2C348&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?resize=350%2C530&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/compressed-trie-tree-7.png?resize=480%2C727&amp;ssl=1 480w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" data-recalc-dims=\"1\" \/><\/a><\/li>\n<\/ol>\n\n\n\n<p>So, for these cases, we would have to break the existing word or the newly inserted word accordingly. The faster we perform these string operations, the faster&nbsp;the insert operation will be.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">search() operation<\/h2>\n\n\n\n<p>Searching in a compressed trie tree&nbsp;is much like searching. Here, instead of comparing a single character, we compare strings. The following cases will arise &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The string we want to search does not exist. Example, searching for &#8220;owl&#8221; when the tree only has &#8220;crow&#8221; in it.<\/li>\n\n\n\n<li>The string we want to search exists as a prefix. Example, searching for &#8220;face&#8221; when the tree only has &#8220;facebook&#8221;.<\/li>\n\n\n\n<li>Only the prefix of the target string exists. Converse of the previous case. Searching for &#8220;facebook&#8221; when the tree only has &#8220;face&#8221;.<\/li>\n\n\n\n<li>The string we want to search matches partially with an existing string. Example, searching for &#8220;this&#8221; where the tree only has &#8220;there&#8221;.<\/li>\n\n\n\n<li>Another case is when the edge label fully matches to the starting portion of the string. Example, searching for &#8220;thereafter&#8221; when &#8220;thereafter&#8221; and &#8220;therein&#8221; exist in the tree. For this, after a full match with &#8220;there&#8221;, we traverse to the node which corresponds to that label and then resume our search (searching for &#8220;after&#8221;).<\/li>\n<\/ul>\n\n\n\n<p>If we are able to fully traverse the tree via the target string and arrive on a tree node, we check if that node is a word ending or not. If yes, we return true or, we return false. For rest of the cases, return false.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">startsWith() operation<\/h2>\n\n\n\n<p>The startsWith() operation is a popular operation performed on the compressed trie tree which checks if there&#8217;s any word in the tree which starts with the target word. This method would be exactly as the searching method. The minor change with the search operation would be, in this operation, we will just check if we are able to fully traverse the tree via the target string and arrive on a node (which may be the root). If we can we return true, regardless of whether the current node is a word ending or not. This is because, even if it is not a word ending, its children will lead to a node which would be a word ending.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Printing the compressed trie tree<\/h2>\n\n\n\n<p>For each edge traversed, add its label to a variable and recursively do this for the child node. While traversing another edge, remove the previously added label and add the new label of the new edge traversing. Print the variable only if the current node is a word ending.<\/p>\n\n\n\n<p>This recursive method should print all the words in the compressed trie tree in a lexicographical order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Start with your existing trie tree code, modify the node definition and then work on the insert method cases. Once you get the insert correctly, then the rest will work out easily. For the insert cases, you just have to&nbsp;do the string operations and change the references carefully. Try to code those cases. Come back and have a look at the diagrams if you need to.<\/p>\n\n\n\n<p>You can check your code&#8217;s correctness with <a href=\"https:\/\/leetcode.com\/problems\/implement-trie-prefix-tree\/\" target=\"_blank\" rel=\"noopener\">LeetCode&#8217;s Implement Trie<\/a> problem. Try to solve that question using a compressed trie tree. Once you solve it, try to reduce the runtime of your program.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/27e8058208079e5c3b15e974f0b4dc9f.js\"><\/script>\n\n\n\n<p>This is the Java implementation. I will update this post with the C\/C++ implementation soon.<\/p>\n\n\n\n<p>In terms of runtime complexity, compressed trie tree is same as that of a regular trie tree. In terms of memory, a compressed trie tree uses very few amount of nodes which gives you a huge memory advantage especially for long strings with long common prefixes. In terms of speed, a regular trie tree would be slightly faster because its operations don&#8217;t involve any string operations, they are simple loops.<\/p>\n\n\n\n<p>I hope my post has demystified everything regarding a compressed trie tree. Tutorial and code for a compressed trie tree are hard to find. I hope my post saved you the effort of finding further tutorials. Do comment below if you have any doubts. Keep practising! Happy Coding!! \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, people! In this post, we will discuss a commonly used data structure to store strings, the Compress Trie Tree, also known [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[14],"tags":[38,40,92,97,110,112],"class_list":["post-2941","post","type-post","status-publish","format-standard","hentry","category-tree-data-structures","tag-compressed-trie-tree","tag-data-structures","tag-patricia-tree","tag-radix-tree","tag-strings","tag-trie-tree"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/2941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=2941"}],"version-history":[{"count":2,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/2941\/revisions"}],"predecessor-version":[{"id":3884,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/2941\/revisions\/3884"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=2941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=2941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=2941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}