Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Program to Implement the Trie data Structure */
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #define MAX 21
- struct node
- {
- int val;
- struct node *children[26];
- };
- struct trie_t{
- struct node *root;
- int count;
- };
- struct node *getnode();
- struct trie_t* initialise();
- void insert(struct trie_t *,char []);
- void display(struct trie_t*);
- void displayrec(struct node *,char [],int);
- void printpath(char [],int);
- int search(struct trie_t *,char []);
- void del(struct trie_t *,char []);
- int delhelp(struct node *,char [],int,int);
- main()
- {
- int i;
- struct trie_t *trie;
- char key[MAX];
- trie=initialise();
- if(!trie)
- {
- printf("Error");
- exit(0);
- }
- while(1)
- {
- printf("Select an Option :\n");
- printf("1. Insert\n2. Search\n3. Display All Keys\n4. Delete Key\n5. Exit\n");
- scanf("%d",&i);
- putchar('\n');
- switch(i)
- {
- case 1:
- printf("Enter the word to be added : ");
- scanf("%s",key);
- insert(trie,key);
- printf("\n%s successfully inserted",key);
- break;
- case 2:
- printf("Enter the word to be searched : ");
- scanf("%s",key);
- if(search(trie,key))
- printf("\n%s found in the Trie",key);
- else
- printf("\n%s not found in the Trie",key);
- break;
- case 3:
- if(trie)
- display(trie);
- break;
- case 4:
- printf("Enter the entry to be deleted from the Trie : ");
- scanf("%s",key);
- del(trie,key);
- break;
- case 5:
- exit(0);
- }
- getch();
- system("cls");
- }
- }
- struct node *getnode()
- {
- struct node *tnode;
- int i;
- tnode=(struct node *)malloc(sizeof(struct node));
- tnode->val=0;
- for(i=0;i<26;i++)
- tnode->children[i]=NULL;
- return tnode;
- }
- struct trie_t *initialise()
- {
- struct trie_t* t;
- t=(struct trie_t *)malloc(sizeof(struct trie_t));
- t->root=getnode();
- t->count=0;
- return t;
- }
- void insert(struct trie_t *trie,char key[])
- {
- int len=strlen(key),i,index;
- struct node *crawl=trie->root;
- if(!trie)
- return;
- trie->count++;
- for(i=0;i<len;i++)
- {
- index=key[i]-'a';
- if(crawl->children[index]==NULL)
- crawl->children[index]=getnode();
- crawl=crawl->children[index];
- }
- crawl->val=trie->count;
- }
- void display(struct trie_t *trie)
- {
- char path[MAX];
- int i;
- if(trie->count==0)
- printf("The Trie is Empty");
- else
- {
- printf("The Trie contains %d elements :\n",trie->count);
- displayrec(trie->root,path,0);
- }
- }
- void displayrec(struct node *root,char path[],int len)
- {
- int i;
- if(root==NULL)
- return;
- if(root->val)
- printpath(path,len);
- for(i=0;i<26;i++)
- if(root->children[i])
- {
- path[len]='a'+i;
- displayrec(root->children[i],path,len+1);
- }
- }
- void printpath(char path[],int len)
- {
- int i;
- for(i=0;i<len;i++)
- putchar(path[i]);
- putchar('\n');
- }
- int search(struct trie_t *trie,char key[])
- {
- int i,len,index;
- struct node *crawl=trie->root;
- len=strlen(key);
- for(i=0;i<len;i++)
- {
- index=key[i]-'a';
- if(crawl->children[index]==NULL)
- return 0;
- crawl=crawl->children[index];
- }
- return crawl&&crawl->val;
- }
- void del(struct trie_t *trie,char key[])
- {
- int len,ret;
- len=strlen(key);
- if(len)
- ret=delhelp(trie->root,key,0,len);
- if(ret==0 || ret==1)
- {
- trie->count--;
- printf("%s successfully deleted from Trie",key);
- }
- }
- int delhelp(struct node *root,char key[],int lev,int len)
- {
- int index,ret,temp=0,check,i;
- if(root==NULL)
- {
- printf("%s not found in the Trie",key);
- return 2; // This means that the nodes above are not to be deleted and key not found
- }
- else if(lev==len)
- {
- if(root->val)
- {
- root->val=0;
- for(i=0,check=1;i<26;i++) // check==1 means we can delete check==0 means we can not delete
- if(root->children[i])
- {
- check=0;
- break;
- }
- if(check)
- {
- free(root);
- return 0;
- }
- else
- return 1;
- }
- else
- return 2;
- }
- else
- {
- index=key[lev]-'a';
- ret=delhelp(root->children[index],key,lev+1,len);
- if(ret==0)
- root->children[index]=NULL; // Node below it deleted in the recursive call
- for(i=0,check=1;i<26;i++) // check==1 means we can delete check==0 means we can not delete
- if(root->children[i])
- {
- check=0;
- break;
- }
- if((!(temp=root->val || ret)) && check)
- {
- free(root);
- return 0;
- }
- else
- return (ret?ret:1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement