-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
class trie{ | ||
|
||
public: | ||
char val; | ||
bool end=false; | ||
trie* children[26]={NULL}; | ||
}; | ||
|
||
void insert(trie* root, string str) | ||
{ | ||
trie* temp=root; | ||
for(int i=0; i<str.length(); i++) | ||
{ | ||
if(!temp->children[str[i]-'a']) | ||
{ | ||
temp->children[str[i]-'a']= new trie(); | ||
} | ||
temp=temp->children[str[i]-'a']; | ||
} | ||
temp->end=true; | ||
cout<<"value successfully Inserted"<<endl; | ||
} | ||
|
||
void search(trie* root, string str) | ||
{ | ||
trie* temp=root; | ||
for(int i=0; i<str.length(); i++) | ||
{ | ||
if(temp->children[str[i]-'a']==NULL) | ||
{ | ||
cout<<" Given value Not Found"<<endl; | ||
|
||
} | ||
temp=temp->children[str[i]-'a']; | ||
} | ||
if(temp->end==true) | ||
{ | ||
cout<<" Given value successfully Found"<<endl; | ||
} | ||
else | ||
{ | ||
cout<<" Given value Not Found"<<endl; | ||
} | ||
} | ||
|
||
void del(trie* root, string str) | ||
{ | ||
trie* temp=root; | ||
for(int i=0; i<str.length(); i++) | ||
{ | ||
if(temp->children[str[i]-'a']==NULL) | ||
{ | ||
cout<<"Given value is not present"<<endl; | ||
|
||
} | ||
temp=temp->children[str[i]-'a']; | ||
} | ||
|
||
if(temp->end==true) | ||
{ | ||
temp->end=0; | ||
cout<<"value successfully deleted"<<endl; | ||
} | ||
else | ||
{ | ||
cout<<"Given value is not present"<<endl; | ||
} | ||
|
||
} | ||
int main() | ||
{ | ||
trie* root=new trie(); | ||
cout<<"please enter string"<<endl; | ||
string str; | ||
cin>>str; | ||
insert(root,str); | ||
search(root,str); | ||
del(root,str); | ||
return 0; | ||
} |