<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 22191030258Yash</title>
    <description>The latest articles on DEV Community by 22191030258Yash (@22191030258yash).</description>
    <link>https://dev.to/22191030258yash</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F629917%2F74c38657-83d5-4eec-8b8a-92c531d34f2e.png</url>
      <title>DEV Community: 22191030258Yash</title>
      <link>https://dev.to/22191030258yash</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/22191030258yash"/>
    <language>en</language>
    <item>
      <title>trie data structure</title>
      <dc:creator>22191030258Yash</dc:creator>
      <pubDate>Wed, 12 May 2021 07:37:10 +0000</pubDate>
      <link>https://dev.to/22191030258yash/trie-data-structure-2m7l</link>
      <guid>https://dev.to/22191030258yash/trie-data-structure-2m7l</guid>
      <description>&lt;p&gt;Trie is an efficient information reTrieval data structure. Using Trie, search complexities can be brought to optimal limit (key length). If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length and N is number of keys in tree. Using Trie, we can search the key in O(M) time. However the penalty is on Trie storage requirements (Please refer Applications of Trie for more details)&lt;/p&gt;

&lt;p&gt;Every node of Trie consists of multiple branches. Each branch represents a possible character of keys. We need to mark the last node of every key as end of word node. A Trie node field isEndOfWord is used to distinguish the node as end of word node. A simple structure to represent nodes of the English alphabet can be as following,&lt;/p&gt;

&lt;p&gt;// Trie node&lt;br&gt;
struct TrieNode&lt;br&gt;
{&lt;br&gt;
     struct TrieNode *children[ALPHABET_SIZE];&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // isEndOfWord is true if the node
 // represents end of a word
 bool isEndOfWord;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;C program:&lt;/p&gt;

&lt;p&gt;// C implementation of search and insert operations&lt;br&gt;
// on Trie&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
&lt;/h1&gt;

&lt;p&gt;// Alphabet size (# of symbols)&lt;/p&gt;

&lt;h1&gt;
  
  
  define ALPHABET_SIZE (26)
&lt;/h1&gt;

&lt;p&gt;// Converts key current character into index&lt;br&gt;
// use only 'a' through 'z' and lower case&lt;/p&gt;

&lt;h1&gt;
  
  
  define CHAR_TO_INDEX(c) ((int)c - (int)'a')
&lt;/h1&gt;

&lt;p&gt;// trie node&lt;br&gt;
struct TrieNode&lt;br&gt;
{&lt;br&gt;
    struct TrieNode *children[ALPHABET_SIZE];&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// isEndOfWord is true if the node represents
// end of a word
bool isEndOfWord;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;// Returns new trie node (initialized to NULLs)&lt;br&gt;
struct TrieNode *getNode(void)&lt;br&gt;
{&lt;br&gt;
    struct TrieNode *pNode = NULL;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pNode = (struct TrieNode *)malloc(sizeof(struct TrieNode));

if (pNode)
{
    int i;

    pNode-&amp;gt;isEndOfWord = false;

    for (i = 0; i &amp;lt; ALPHABET_SIZE; i++)
        pNode-&amp;gt;children[i] = NULL;
}

return pNode;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// If not present, inserts key into trie&lt;br&gt;
// If the key is prefix of trie node, just marks leaf node&lt;br&gt;
void insert(struct TrieNode *root, const char *key)&lt;br&gt;
{&lt;br&gt;
    int level;&lt;br&gt;
    int length = strlen(key);&lt;br&gt;
    int index;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct TrieNode *pCrawl = root;

for (level = 0; level &amp;lt; length; level++)
{
    index = CHAR_TO_INDEX(key[level]);
    if (!pCrawl-&amp;gt;children[index])
        pCrawl-&amp;gt;children[index] = getNode();

    pCrawl = pCrawl-&amp;gt;children[index];
}

// mark last node as leaf
pCrawl-&amp;gt;isEndOfWord = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// Returns true if key presents in trie, else false&lt;br&gt;
bool search(struct TrieNode *root, const char *key)&lt;br&gt;
{&lt;br&gt;
    int level;&lt;br&gt;
    int length = strlen(key);&lt;br&gt;
    int index;&lt;br&gt;
    struct TrieNode *pCrawl = root;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (level = 0; level &amp;lt; length; level++)
{
    index = CHAR_TO_INDEX(key[level]);

    if (!pCrawl-&amp;gt;children[index])
        return false;

    pCrawl = pCrawl-&amp;gt;children[index];
}

return (pCrawl != NULL &amp;amp;&amp;amp; pCrawl-&amp;gt;isEndOfWord);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// Driver&lt;br&gt;
int main()&lt;br&gt;
{&lt;br&gt;
    // Input keys (use only 'a' through 'z' and lower case)&lt;br&gt;
    char keys[][8] = {"the", "a", "there", "answer", "any",&lt;br&gt;
                     "by", "bye", "their"};&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;char output[][32] = {"Not present in trie", "Present in trie"};


struct TrieNode *root = getNode();

// Construct trie
int i;
for (i = 0; i &amp;lt; ARRAY_SIZE(keys); i++)
    insert(root, keys[i]);

// Search for different keys
printf("%s --- %s\n", "the", output[search(root, "the")] );
printf("%s --- %s\n", "these", output[search(root, "these")] );
printf("%s --- %s\n", "their", output[search(root, "their")] );
printf("%s --- %s\n", "thaw", output[search(root, "thaw")] );

return 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
