# Tries as data structure

Tries is a data structure which is usually neglected by programmers while designing solutions. Tries are very useful in cases where strings are involved with a good amount of duplication of their prefixes. What are tries? Trie is a data structure which stores information, referred to as **key**, in such a manner that the common information is stored only once. Usually **keys are strings**. No node in trie stores the whole key, but the position of the node gives us information that which key it is part of. All the descendant node in trie have the **same prefix**, hence trie is also know as **prefix trees**.Word trie is taken from re**trie**val word.

Another property of trie is that every node of trie has multiple children. In case of string (assuming lower case), there will be 26 children of each node. Take an example: We have the following strings: “House”, “Home”, “Tree” I need to store this information, Trie for the above words would look like:

## Insertion in trie

Before insertion, we need to understand that how can we represent a node of a trie. Since intermediate nodes of a key do not store any information at them, we need to distinguish between the leaf node and the intermediate node. For that we can store a field called as the value inside each node, If the value is zero, it’s an intermediate node, if non-zero, it’s a leaf node.

In our case, we are assuming MAX_SIZE of 26.

Basic initialization

#define MAX_SIZE 26 #define GET_CHAR_INDEX(ch)\ ((int) ch - (int)'a' ) #define LEAF_NODE 1 #define true 1 #define false 0 typedef struct trie_node_t { int value; struct trie_node_t * children[MAX_SIZE]; }Node; typedef struct trie{ int count ; Node *root; }trie; void initialize(trie *t){ t->root = (Node *)malloc(sizeof(Node)); t->count =0; }

Now we can go and see insertion part.

Scan through the string character one by one. For each character, check if it has child nodes. The character acts as an index in children array. If the node at the char index has a child node, move to the child node and take the next character of the string. Repeat step 2 and 3 If the node at the char index doesn’t have a child node, create a child node and add it to trie. Move the character in the string and move down to a newly created child. Go to step 2.

void insert(trie *t, char key[]){ int len = strlen(key); int i; if(t->root == NULL){ printf("\n Trie is not initialized\n"); } Node * current = t->root; for(i=0; i<len; i++){ /* Get the index of char in children array */ int index = GET_CHAR_INDEX(key[i]); /* If the children array does not contain the pointer at index pointed by char, allocate new node */ if(current->children[index] == NULL){ current->children[index] = (Node *)malloc(sizeof(Node)); } /* Else traverse down the trie. */ current = current->children[GET_CHAR_INDEX(key[i])]; } /* To mark it as leaf */ current->value = LEAF_NODE; }

Search in trie

The best part of the trie is its search. It is done in O(M) where M is the length of the input string.

Start from the root. and take the first character of the string. If the array index pointed by the character is NULL, return false. Else move to the child node and next character. Repeat step 2 and 3 till the end to the input string. If we reach till the end of the string and the current node is a leaf node (identified using value), then return true.

int search(trie *t, char key[]){ int len = strlen(key); int i; if(t->root == NULL){ printf("\n Trie is not initialized\n"); } Node * current = t->root; for(i=0; i<len; i++){ int index = GET_CHAR_INDEX(key[i]); /* If characters are left in key and we have reached at NULL, there is no key present */ if(current->children[index] == NULL) return false; /* Else traverse down the trie */ current = current->children[index]; } /* If we have reached the lead=f node, key is present */ if(current && current->value == LEAF_NODE ) return true; return false; }

The only drawback with tries is that it takes a lot more space **(K * MAX_SIZE * N)** as compared to the binary search tree. It is evident from the fact that every node in trie has to have a place for all the possible characters. In time complexity terms, insertion and search both are **O(M)** where **M is length** of string being entered. In this post we saw ** insertion and search operation in tries**. We will use these operations in the future post and solve some real problems.