Just had a new package published on CRAN...rtrie :).<br><br>The rtrie package allows you to quickly create <a href="https://en.wikipedia.org/wiki/Trie">Tries</a> from a list of strings.  Example use cases for the package are word searching in games like Boggle or Scrabble or implementation of applications that  autocomplete or autocorrect text.   Incidentally, besides being great for statistics, R is an excellent language for building algorithms and data structures from a pedagogical standpoint.  The language's Scheme influence makes the expression of algorithms straightforward and the plotting capabilities make it easy to visualize the data structures and processing involved.  The remainder of this post is intended to illustrate that fact as well as introduce rtrie's capabilities.<br><br>To create a new trie from a vector of words:<br><br><span>> library(rtrie)</span><br><span>> trie &lt;- char_tree(c('able', 'act', 'acts', 'across', 'act', 'bat', 'babble', 'bobble'), 'X')</span><br><br>The new trie is a list of lists... with each node being a letter.   So the list of lists above can be plotted using the <span>data.tree </span><span>package</span>.  This package has a wide range of tree related processing functions... including functionality related to all of the plots shown in this post.<br><br><span>> library(data.tree)</span><br><span>> data_trie </span>&lt;<span>- FromListSimple(trie)</span><br><span>> plot(data_trie)</span><br><br><br><br><div><a href="https://2.bp.blogspot.com/-IdBo9ioOvbg/WHGEhZmfJII/AAAAAAAAA3E/gFLE6rkU8rcYjfPIFvDQwR4mvCcrHqEyQCLcB/s1600/rtrie1.png"></a></div><br>The trie can also be displayed in text form which can be read left to right.<br><br><span>data_trie</span><br><span><br></span><span>> data_trie</span><br><span>                   levelName</span><br><span>1  Root                     </span><br><span>2   ¦--a                    </span><br><span>3   ¦   ¦--b                </span><br><span>4   ¦   ¦   °--l            </span><br><span>5   ¦   ¦       °--e        </span><br><span>6   ¦   °--c                </span><br><span>7   ¦       ¦--r            </span><br><span>8   ¦       ¦   °--o        </span><br><span>9   ¦       ¦       °--s    </span><br><span>10  ¦       ¦           °--s</span><br><span>11  ¦       °--t            </span><br><span>12  ¦           °--s        </span><br><span>13  °--b                    </span><br><span>14      ¦--a                </span><br><span>15      ¦   ¦--b            </span><br><span>16      ¦   ¦   °--b        </span><br><span>17      ¦   ¦       °--l    </span><br><span>18      ¦   ¦           °--e</span><br><span>19      ¦   °--t            </span><br><span>20      °--o                </span><br><span>21          °--b            </span><br><span>22              °--b        </span><br><span>23                  °--l    </span><br><span></span><br><span>24                      °--e</span><br><span><br></span><span>To check if a given word is in the tree, call </span>is_word <span>which returns a boolean indicating whether or not the word was found in the trie.</span><br><span><br></span><span>> is_word('across', trie)</span><br><span>[1] TRUE</span><br><br>The following diagram illustrates where the match was made in the trie.<br><div><a href="https://2.bp.blogspot.com/-_6LyyWHMJBE/WHGHt1yaxXI/AAAAAAAAA3c/7xUleM95L2kCLM4nVcn6bzHk_gbYcb3igCLcB/s1600/rtrie_match.png"></a></div><br><br>To obtain a list of suggested words based on a prefix, call matching_words which returns a vector of matching words.<br><span><br></span><span>> matching_words('ac', trie)</span><br><span> r.o.s.s        t      t.s </span><br><span>&quot;across&quot;    &quot;act&quot;   &quot;acts&quot; </span><br><br>Three words are returned, corresponding to the section of the tree where the prefix exists.  The section of the tree identified by the function is shown below.  Although not clear in the diagram, a termination character exists that allowed the function to return both &quot;act&quot; and &quot;acts&quot;. <br><br><div><a href="https://3.bp.blogspot.com/-r1JSnGR9_Vk/WHGHeOoMZXI/AAAAAAAAA3Y/Q0o8gC-4xm4yNvOKzD0aSzJ499HLFNlfQCLcB/s1600/trie2.png"></a></div>The highlighted graphs above were created by using data.tree's SetGraphStyle, SetNodeStyle, and SetEdgeStyle functions.<br><br>If you are interested in more information, the code is available on <a href="https://github.com/ezgraphs/rtrie">Github</a> and the <a href="https://cran.r-project.org/web/packages/rtrie/vignettes/trie_usage.html">vignette</a> includes additional discussion.<br><br><br><br>