C Program to Implement Trie

This is a C Program to implement Trie. A Trie is a very useful (but often ignored) data structure, which can be used to solve a large number of Strings related problems quickly.

Here is source code of the C Program to Implement Trie. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define ALPHABET_SIZE 26
  6.  
  7. struct node {
  8.     int data;
  9.     struct node* link[ALPHABET_SIZE];
  10. };
  11.  
  12. struct node* root = NULL;
  13.  
  14. struct node* create_node() {
  15.     struct node *q = (struct node*) malloc(sizeof(struct node));
  16.     int x;
  17.     for (x = 0; x < ALPHABET_SIZE; x++)
  18.         q->link[x] = NULL;
  19.     q->data = -1;
  20.     return q;
  21. }
  22.  
  23. void insert_node(char key[]) {
  24.     int length = strlen(key);
  25.     int index;
  26.     int level = 0;
  27.     if (root == NULL)
  28.         root = create_node();
  29.     struct node *q = root; // For insertion of each String key, we will start from the root
  30.  
  31.     for (; level < length; level++) {
  32.         index = key[level] - 'a';
  33.  
  34.         if (q->link[index] == NULL) {
  35.             q->link[index] = create_node(); // which is : struct node *p = create_node(); q->link[index] = p;
  36.         }
  37.  
  38.         q = q->link[index];
  39.     }
  40.     q->data = level; // Assuming the value of this particular String key is 11
  41. }
  42.  
  43. int search(char key[]) {
  44.     struct node *q = root;
  45.     int length = strlen(key);
  46.     int level = 0;
  47.     for (; level < length; level++) {
  48.         int index = key[level] - 'a';
  49.         if (q->link[index] != NULL)
  50.             q = q->link[index];
  51.         else
  52.             break;
  53.     }
  54.     if (key[level] == '\0' && q->data != -1)
  55.         return q->data;
  56.     return -1;
  57. }
  58.  
  59. int main(int argc, char **argv) {
  60.     insert_node("by");
  61.     insert_node("program");
  62.     insert_node("programming");
  63.     insert_node("data structure");
  64.     insert_node("coding");
  65.     insert_node("code");
  66.     printf("Searched value: %d\n", search("code"));
  67.     printf("Searched value: %d\n", search("geeks"));
  68.     printf("Searched value: %d\n", search("coding"));
  69.     printf("Searched value: %d\n", search("programming"));
  70.     return 0;
  71. }

Output:

$ gcc Trie.c
$ ./a.out
 
Searched value: 4
Searched value: -1
Searched value: 6
Searched value: 11

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.