PhoneBook using Trie Tree Data Structure, Lab Reports of Data Structures and Algorithms

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

2019/2020
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 07/13/2020

unknown user
unknown user 🇮🇳

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DSA PROJECT
Slot B2+TB2
Raghav Sharma 19BCB0051
Rupin Patel 19BCB0030
pf3
pf4
pf5
pf8
pf9
Discount

On special offer

Partial preview of the text

Download PhoneBook using Trie Tree Data Structure and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

DSA PROJECT

Slot B2+TB

Raghav Sharma 19BCB

Rupin Patel 19BCB

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

newly constructed node would be node 'a'. We mark this node

as current node.

4. Now we get the index of the second character of "abc". That

would be 1 and therefore we go to 1st child of current node (from

step#3).

5. Here again, 1st child is null. We create a new TrieNode which

would be node 'b'. We mark this node as current node.

6. Now we get the index of the third character of "abc". That

would be 2 and therefore we go to 2nd child current node (from

step#5).

7. Here again, 2nd child is null. We create a new TrieNode which

would be node 'c'. We mark this node as current node.

8. At this step, we are done reading all the characters of the given

key. Hence, we mark the current node that is node 'c' as leaf

node and store value 1 at this leaf node.

Search-: Searching for a key is similar to insert operation, however we

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".

  1. Go to root node.
  2. Pick the first character of key "ac" which would be 'a'. Find out its index using alphabet system in use.
  1. Index returned would be 0, go to 0th child of root which is node 'a'. Mark this node as current node.
  2. Pick the second character of key "ac" which would be 'c'. Its index would be 2 and therefore we go to 2nd^ child of current node.
  3. Now at this point, we find out that 2 nd^ child of current node is null. If you notice, from insertion algorithm of a given key, no node in the key-path from the root could be null. If it is null then that implies that this key was never inserted in the trie. And therefore, in such cases, we return Not present in phonebook. Example: searching for an existing key "abb" The steps are very similar to previous example. We keep on reading characters of given key and according to indices of characters, travel from root node to node 'b' which is at level-3 (if root is at level-0). At this point we would have read all the characters of the key "abb" and hence we return the value stored at this node.

Errors fixed - :

  1. Else case for showing the output in the case when name was not present in trie trees was fixed with the application of indexing. An output of char type was used to index such that 0 meant not present in phonebook while 1 meant present in phonebook.
  2. When we updated or inserted a word; say booked. It showed that prefixes of the word like book, boo were also present in the trie. This error was removed by adding the length such that it searches the word upto the word own length and not till the prefixes of the word.

References - :

  1. Digital Searching". The Art of Computer Programming Volume 3: Sorting and Searching (2nd ed.). Addison-Wesley.

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);