Skip to content

Commit

Permalink
Merge pull request #135 from sandy2004/master
Browse files Browse the repository at this point in the history
Create trie.cpp
  • Loading branch information
Ayushsinhahaha authored Oct 30, 2022
2 parents 7929b4e + 6e70071 commit a245b70
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions trie.cpp
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;
}

0 comments on commit a245b70

Please sign in to comment.