0% found this document useful (0 votes)
317 views6 pages

Dictionary Using Tries in C

The document describes using a trie data structure to implement a dictionary that allows storing words and their meanings. It stores strings in a trie where each node represents a character and edges connect parent nodes to children. Words can be inserted, searched, and modified in O(M) time where M is the word length. Memory usage is O(ALPHABETSIZE * length * N) where N is the number of words. Functions like insert(), search(), modify(), and delete() are implemented to add, find, update, and remove words from the trie. The code provided implements these functions and a main driver to interact with the dictionary trie.

Uploaded by

Shuvam Mitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
317 views6 pages

Dictionary Using Tries in C

The document describes using a trie data structure to implement a dictionary that allows storing words and their meanings. It stores strings in a trie where each node represents a character and edges connect parent nodes to children. Words can be inserted, searched, and modified in O(M) time where M is the word length. Memory usage is O(ALPHABETSIZE * length * N) where N is the number of words. Functions like insert(), search(), modify(), and delete() are implemented to add, find, update, and remove words from the trie. The code provided implements these functions and a main driver to interact with the dictionary trie.

Uploaded by

Shuvam Mitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DICTIONARY IMPLEMENTATION USING TRIE

By Shailesh Kumar Sharma (1RV18CS149)

Shuvam Mitra (1RV18CS165)

Problem Statement:
Through this project, we intend to help the user to store words and their meanings. It also
allows for users to search for a specific word and fetch its meaning from the collection of
previously stored data. Along with searching, this program also allows for the user to modify
the meaning of a previously stored word, by searching for the word and storing the new
meaning and also to delete a word and its meaning.

Data structure used:

For the implementation of the dictionary, we used Trie. Trie is a special data structure that is
used to store strings that can be visualized like a graph. It consists of nodes and edges. Each
node consists at max 26 children and edges connect each parent node to its children.
Dictionary can be implemented using other concepts like hash tables, but using a Trie allows
for data to be searched faster. It also avoids the collisions that can occur if hash tables are
used. Using Trie allows to skip the hassle of choosing an appropriate hash function. Trie has
predetermined alphabetical ordering which makes it easier to use to implement a dictionary
and find and store words with common prefix.

Time complexity: We can search a key in O(M) time, where M is length of the string

Space complexity: Memory requirements of Trie is O(ALPHABETSIZE * Key Length * N),


where N is the number of keys in Trie.

Algorithm:
getNode():

Description: This function returns a new Trie node

Method: Allocates space to temporary variable pNode and sets all its alphabet nodes to
NULL and

returns pNode.

insert(root, key, value):

Description: This function inserts a word in Trie

Method: Every character of the input key is inserted as an individual Trie node. Children is
an array of pointers to the next level trie nodes. The key character acts as an index into the
array children. If the input key is new or an extension of the existing key, then non-existing
nodes of the tree are constructed and used to mark end of the word for the last node. If the
input key is a prefix of an existing key in Trie, the last node of the key is marked as the end
of word.

search(root, key):

Description: This function searches for a key(word) in Trie

Method: Searching for a word is similar to insert operation, except here, we only compare the
characters and move down. The search can terminate due to end of the string or lack of key in
the trie. This function returns true if the word exists and false if it doesn’t exist.

searchAndReturn(root, key):

Description: This function returns the node with the desired word to the function that evokes
this function.

Method: This function works in the same way as search function does to locate the word and
then returns the node to the function for which the required operation of either modifying the
meaning or deleting the word along with the meaning is performed.

modify(root, key):

Description: This function is used to modify the meaning of an existing key in the Trie

Method: This function evokes searchAndReturn function to see if the word entered by the
user whose meaning has to be modified exists or not. If the word exists, then the new
meaning is asked for by the user, which replaces the old meaning of the word. If the word
doesn’t exist, then an appropriate message is displayed.

deleteNode(root, key):

Description: This function is used to delete a word and it’s meaning from the Trie

Method: This function also evokes searchAndReturn function to see if the word entered by
the user which has to be deleted exists or not. If the word exists, then the word and its
meaning are deleted from the Trie. If the word doesn’t exist, then an appropriate message is
displayed.

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

// Alphabet size (# of symbols)


#define ALPHABET_SIZE (26)
// Converts key current character into index
// use only 'a' through 'z' and lower case
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')

// trie node
struct TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];
char value[30]; // The meaning of the word
// isEndOfWord is true if the node represents
// end of a word
bool isEndOfWord;
};

// Returns new trie node (initialized to NULLs)


struct TrieNode *getNode(void)
{
struct TrieNode *pNode = NULL;

pNode = (struct TrieNode *)malloc(sizeof(struct TrieNode));

if (pNode)
{
int i;

pNode->isEndOfWord = false;

for (i = 0; i < ALPHABET_SIZE; i++)


pNode->children[i] = NULL;
}
return pNode;
}

// If not present, inserts key into trie


// If the key is prefix of trie node, just marks leaf node
void insert(struct TrieNode *root, const char *key, char value[30])
{
int level;
int length = strlen(key);
int index;

struct TrieNode *pCrawl = root;

for (level = 0; level < length; level++)


{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();

pCrawl = pCrawl->children[index];
}
// mark last node as leaf
strcpy (pCrawl->value, value);
pCrawl->isEndOfWord = true;
}

// Returns true if key presents in trie, else false


bool search(struct TrieNode *root, const char *key)
{
int level;
int length = strlen(key);
int index;
struct TrieNode *pCrawl = root;

for (level = 0; level < length; level++)


{
index = CHAR_TO_INDEX(key[level]);

if (!pCrawl->children[index])
return false;

pCrawl = pCrawl->children[index];
}
if (pCrawl != NULL && pCrawl->isEndOfWord) {
printf ("Word Found in the Dictionary. Meaning : %s\n!", pCrawl->value);
}
return (pCrawl != NULL && pCrawl->isEndOfWord);
}

struct TrieNode *searchAndReturn(struct TrieNode *root, const char *key) {


int level;
int length = strlen(key);
int index;
struct TrieNode *pCrawl = root;

for (level = 0; level < length; level++)


{
index = CHAR_TO_INDEX(key[level]);

if (!pCrawl->children[index])
return false;

pCrawl = pCrawl->children[index];
}
if (pCrawl != NULL && pCrawl->isEndOfWord) {
// printf ("Key Found in the Dictionary %s ---- %s\n!", key, pCrawl->value);
return pCrawl;
}
return NULL;
}

void modify(struct TrieNode *root, const char *key) {

struct TrieNode *pCrawl = searchAndReturn(root, key);


char ca;
if (pCrawl) {
// printf("Element Found\n");
printf ("Word Found in the Dictionary %s ---- %s!\n", key, pCrawl->value);
char value[30];
printf("Enter the new meaning: \n");
scanf("%c",&ca);
fflush(stdin);
scanf ("%[^\n]", value);
for (int i = 0; i<strlen(pCrawl->value);i++) {
pCrawl->value[i] = '\0';
}
strcpy (pCrawl->value, value);
printf("Word (%s) succesfully updated with the meaning: %s\n", key, pCrawl->value);
}
else {
printf("Word not found\n");
}
}
void deleteNode (struct TrieNode *root, const char *key) {
struct TrieNode *pCrawl = searchAndReturn(root, key);
// printf ("Key Found in the Dictionary %s ---- %s\n!", key, pCrawl->value);
if (pCrawl) {
printf ("Word Found in the Dictionary and it will be deleted: %s ---- %s!\n", key, pCrawl->value);
pCrawl->isEndOfWord = false;
for (int i = 0; i < ALPHABET_SIZE; i++)
pCrawl->children[i] = NULL;
free (pCrawl);
printf("Node deleted succesfully\n");
}
}

// Driver
int main()
{
struct TrieNode *root = getNode();
// Construct trie
int i; char ca, key[20], value[30];
// for (i = 0; i < ARRAY_SIZE(keys); i++)
// insert(root, keys[i]);
int ch;
while (1) {
printf("1. Insert word and meaning\n2. Search meaning\n3. Modify meaning\n4. Delete word(node)\n5. Exit\nEnter
choice: \n");
scanf("%d", &ch);

if (ch == 1) {

printf ("Enter the word : ");


scanf ("%s", key);
printf ("Enter the meaning: ");
scanf("%c",&ca);
fflush(stdin);
scanf("%[^\n]",value);
insert (root, key, value);
printf ("Insert Successful\n");
}

if (ch == 2) {
char key[20];
printf ("Enter the word to search: ");
scanf ("%s", key);
bool isPresent = search (root, key);
if (isPresent) {
// printf ("%s is present!\n", key);
}
else {
printf ("Oops, cant find %s, Try again!\n", key);
}
}

if (ch == 3) {
char key[20];
printf ("Enter the key to modify: ");
scanf ("%s", key);
modify(root, key);
}

if (ch == 4) {
char key[20];
printf ("Enter the key to delete: ");
scanf ("%s", key);
deleteNode(root, key);
}

if (ch == 5) {
break;
}
}
return 0;
}

You might also like