Repository navigation
Expand file tree
/
Copy pathTrie.cpp
More file actions
91 lines (73 loc) · 2.14 KB
/
Copy pathTrie.cpp
File metadata and controls
91 lines (73 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
const int ALPHABET_SIZE=26;
class node{
public:
node* children[ALPHABET_SIZE];
bool end_of_world;
};
node* getNode(){
node* temp=new node();
for(int i=0;i<ALPHABET_SIZE;i++){temp->children[i]=NULL;}
temp->end_of_world=false;
return temp;
}
void insert(node* pcrawl,string str){
for(int i=0;i<str.length();i++){
int index=str[i]-'a';
if(!pcrawl->children[index]){pcrawl->children[index]=getNode();}
pcrawl=pcrawl->children[index];
}
pcrawl->end_of_world=true;
return;
}
bool search(node* pcrawl,string str){
for(int i=0;i<str.length();i++){
int index=str[i]-'a';
if(!pcrawl->children[index]){return false;}
pcrawl=pcrawl->children[index];
}
return(pcrawl!=NULL && pcrawl->end_of_world);
}
bool isEmpty(node* pcrawl){
for(int i=0;i<ALPHABET_SIZE;i++){
if(pcrawl->children[i]!=NULL){return false;}
}
return true;
}
node* remove(node* pcrawl,string str,int depth){
if(pcrawl==NULL){return NULL;}
if(depth==str.size()){
if(pcrawl->end_of_world){pcrawl->end_of_world=false;}
if(isEmpty(pcrawl)){
delete(pcrawl);
pcrawl=NULL;
}
return pcrawl;
}
int index=str[depth]-'a';
pcrawl->children[index]=remove(pcrawl->children[index],str,depth+1);
if(isEmpty(pcrawl) && pcrawl->end_of_world==false){
delete(pcrawl);
pcrawl=NULL;
}
return pcrawl;
}
int main(){
string keys[] = {"the", "a", "there", "answer", "any", "by", "bye", "their" };
int n = sizeof(keys)/sizeof(keys[0]);
node* root=getNode();
for(int i=0;i<n;i++){insert(root,keys[i]);}
string toFind="answer";
if(search(root,toFind)){cout<<toFind<<" is present\n";}
else{cout<<toFind<<" is not present\n";}
toFind="abcd";
if(search(root,toFind)){cout<<toFind<<" is present\n";}
else{cout<<toFind<<" is not present\n";}
toFind="answer";
remove(root,toFind,0);
if(search(root,toFind)){cout<<toFind<<" is present\n";}
else{cout<<toFind<<" is not present\n";}
return 0;
}