





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A trie is a tree-like data structure whose nodes store the letters of an alphabet. By structuring the nodes in a particular way, words and strings can be retrieved from the structure by traversing down a branch path of the tree.
Typology: Lab Reports
Limited-time offer
Uploaded on 07/13/2020
1 / 9
This page cannot be seen from the preview
Don't miss anything!






On special offer
Slot B2+TB
Problem Statement Implement a phonebook using trie trees as a data structure in C language. Data structure used Tries were first described by René de la Briandais in 1959.The term trie was coined two years later by Edward Fredkin. Data structure used to implement phonebook in C language is trie trees. In computer science, a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is a kind of search tree—an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. A Trie is a special data structure used to store strings that can be visualized like a graph. It consists of nodes and edges. Each node consists of at max 26 children and edges connect each parent node to its children. These 26 pointers are nothing but pointers for each of the 26 letters of the English alphabet A separate edge is maintained for every edge. Justification for the choice of trie trees A trie has a number of advantages over binary search trees and other data structure. A trie can also be used to replace a hash table. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are not necessarily associated with every node. Rather, values tend only to be associated with leaves, and with some inner nodes that correspond to keys of interest. A trie tree provide an alphabetical ordering of each character of string by key. Moreover, there are no collisions of different keys in a trie tree. There is no need to provide a hash function or to change hash functions as more keys are added to a trie. Time complexity Trie is an efficient information re trie val data structure. Using Trie, search complexities can be brought to optimal limit (key length). If we store keys
only compare the characters and move down. The search can terminate due to end of string or lack of key in trie. In the former case, if the is_end_of_word field of last node is one, then the key exists in trie. In the second case, the search terminates without examining all the characters of key, since the key is not present in trie. Search algorithm steps Example: Searching for non-existing key "ac".
for (i = 0; i < alphasize; i++) { node->children[i] = NULL; } } return node; } //Inserting a key into trie // If not present, inserts key into trie // If the key is prefix of trie node, just mark it the leaf node void insert(struct TrieNode *root, const char *key) { int level; int l = strlen(key); int ind; struct TrieNode *newnode = root; for (level = 0; level < l; level++) { ind = chartoind(key[level]); if (!newnode->children[ind]) newnode->children[ind] = getNode(); newnode = newnode->children[ind]; } // mark the last node as leaf newnode->is_end_of_word = 1; printf("Enter the number:"); scanf("%llu", &newnode->number); } int search(struct TrieNode *root, const char *key) { int level; int length = strlen(key); int index; struct TrieNode newnode = root; for (level = 0; level < length; level++) { index =chartoind(key[level]); if (!newnode->children[index]) return 0; newnode=newnode->children[index]; } return (newnode->is_end_of_word); } /int duplicate(struct TrieNode *root,const char *key) { int j; int len=strlen(key);
int ind; struct TrieNode newnode = root; for (level = 0; level < l; level++) { } }/ ind = chartoind(key[level]); if (!newnode->children[ind]) return 0; newnode = newnode->children[ind]; // Main program to apply insert and search in a menu driven approach int main() { int n1,i1,ch,hi; char name[100]; char keys[100]; char output[][32] = {"Not present in Phonebook", "Present in Phonebook"}; struct TrieNode *root; root = getNode(); do { printf("1.Create a New Contact\n2.Search For a Contact\n"); printf("Please Enter Your Choice: "); scanf("%d",&ch); switch(ch) { PhoneBook:"); case 1: printf("Enter the number of contacts to be updated in scanf("%d",&n1); for(i1=0;i1<n1;i1++) { } break; case 2: printf("Enter the name of the Person: "); scanf("%s",keys); //duplicate(root,keys); insert(root, keys); printf("Enter the number of persons to be searched in Phonebook: "); scanf("%d",&hi); for(i1=0;i1<hi;i1++) { printf("Enter the name to be Searched: "); scanf("%s",name); printf("%s\n",output[search(root,name)]); } break; default: printf("Invalid Choice\n"); } }while(ch<=2);