Skip to content

Commit 3eae549

Browse files
authored
Trie Datastructure
1 parent c405c45 commit 3eae549

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

Trie/TrieDs.cpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include<stdio.h>
2+
#include<iostream>
3+
#include<string>
4+
#include<algorithm>
5+
6+
#define CHAR_TO_INT(c) c-'a'
7+
#define ALPHABET_SIZE 26
8+
9+
using namespace std;
10+
struct Trie {
11+
Trie* childern[ALPHABET_SIZE];
12+
bool isleaf;
13+
};
14+
15+
Trie* getNode() {
16+
Trie* pNode = new Trie;
17+
18+
std::fill (std::begin(pNode->childern), std::end(pNode->childern), nullptr);
19+
20+
pNode->isleaf = false;
21+
return pNode;
22+
}
23+
void insert(Trie* pRoot, string str) {
24+
if(pRoot == nullptr) {
25+
return;
26+
}
27+
Trie* pCrawl = pRoot;
28+
for(auto c:str) {
29+
cout<<c <<"" <<CHAR_TO_INT(c) <<" " ;
30+
if(pCrawl->childern[CHAR_TO_INT(c)] == nullptr)
31+
pCrawl->childern[CHAR_TO_INT(c)] = getNode();
32+
pCrawl = pCrawl->childern[CHAR_TO_INT(c)];
33+
}
34+
pCrawl->isleaf = true;
35+
}
36+
37+
bool search(Trie* pRoot, string str) {
38+
if(pRoot == nullptr) {
39+
return false;
40+
}
41+
Trie* pCrawl = pRoot;
42+
for(auto c:str) {
43+
cout<<c <<"" <<CHAR_TO_INT(c) <<" " ;
44+
if(pCrawl->childern[CHAR_TO_INT(c)] == nullptr) {
45+
return false;
46+
}
47+
pCrawl = pCrawl->childern[CHAR_TO_INT(c)];
48+
}
49+
if(pCrawl->isleaf == false) return false;
50+
return true;
51+
}
52+
53+
bool hasChildern(Trie* pNode) {
54+
if(pNode == nullptr) return false;
55+
for(auto i =0;i<ALPHABET_SIZE;i++) {
56+
if(pNode->childern[i] != nullptr) return true;
57+
}
58+
return false;
59+
}
60+
61+
int deleteString(Trie* pRoot, char* c) {
62+
if(pRoot == nullptr ) return 0;
63+
if(*c){
64+
if(pRoot->childern[CHAR_TO_INT(*c)]!=nullptr // current character is present in the trie
65+
&& deleteString(&pRoot[CHAR_TO_INT(*c)],c+1)!=0 // Recurse to next character
66+
&& pRoot->isleaf != true) { // is not leaf node
67+
if(!hasChildern(pRoot)) { // this situation is very less likly to happn i.e is not leaf and has no childern
68+
delete pRoot; // the above can happen whn leaf node is deleted , and left with intermediate nodes in recursive calls
69+
pRoot = nullptr;
70+
return 1;
71+
} else {
72+
return 0;
73+
}
74+
}
75+
}
76+
// here I reached till end of string .
77+
if(*c == '\0' && pRoot->isleaf) {
78+
if(!hasChildern(pRoot)) {
79+
delete pRoot;
80+
pRoot= nullptr;
81+
return 1;
82+
} else {
83+
pRoot->isleaf = false;
84+
}
85+
}
86+
return 0;
87+
}
88+
89+
// driver program
90+
int main() {
91+
Trie* root = getNode();
92+
cout<<endl;
93+
insert(root,"omshreeganeshayanamah");
94+
insert(root,"om");
95+
insert(root,"omshree");
96+
insert(root,"omshreeganesh");
97+
cout<<endl;
98+
printf("\n Searching\n");
99+
100+
printf("\nomshreeganeshayanama Is Found = %d\n",search(root,"omshreeganeshayanamah"));
101+
printf("\nom Is Found = %d\n",search(root,"om"));
102+
printf("\nomshree Is Found = %d\n",search(root,"omshree"));
103+
printf("\nomshreegane Is Found = %d\n",search(root,"omshreegane"));
104+
105+
return 0;
106+
}

0 commit comments

Comments
 (0)