-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
139 lines (115 loc) · 3.16 KB
/
trie.cpp
File metadata and controls
139 lines (115 loc) · 3.16 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<iostream>
#include<unordered_map>
using namespace std;
struct Trie{
bool is_leaf;
unordered_map<char, Trie*> map;
};
Trie* get_node(void){
Trie *p=new Trie;
p->is_leaf=false;
return p;
}
void insert(Trie* &root, char *key){
if(root==NULL){
root=get_node();
}
Trie *curr=root;
while(*key){
if(curr->map.find(*key)==curr->map.end()){
curr->map[*key]=get_node();
}
curr=curr->map[*key];
key++;
}
curr->is_leaf=true;
}
bool have_children(Trie *node){
for(auto it: node->map){
if(it.second!=NULL){
return true;
}
}
return false;
}
bool deletion(Trie* &root, char *key){
//return if trie is empty
if(root==NULL){
return false;
}
if(*key){
//recurse for the node curresponding to next character in
//string and if it return true, delete current node if it is not leaf
if(root!=NULL && root->map.find(*key)!=root->map.end() &&
deletion(root->map[*key], key+1) && root->is_leaf==false){
if(!have_children(root)){
delete root;
root=NULL;
return true;
}
else{
return false;
}
}
}
// if we have reached the end of the string
if(*key=='\0' && root->is_leaf){
//if current node is leaf and don't have any children
if(!have_children(root)){
delete root; //delete current node
root=NULL;
return true; //delete non leaf parent node
}
//if current node is leaf and have children
else{
//mark current node as non-leaf (don't delete it)
root->is_leaf=false;
return false; //don't delete its parent node
}
}
return false;
}
bool search(Trie *root, char *key){
if(root==NULL){
return false; // return false if trie is empty
}
Trie *curr=root;
while(*key){
curr=curr->map[*key];
if(curr==NULL){
return false;
}
key++;
}
return curr->is_leaf;
}
int main(){
Trie *root=NULL;
insert(root, "hello");
search(root, "hello") ? cout<<"hello:yes"<<endl : cout<<"hello:no"<<endl;
insert(root, "helloworld");
search(root, "helloworld") ? cout<<"helloworld:yes"<<endl : cout<<"helloworld:no"<<endl;
search(root, "hellowrld") ? cout<<"hellowrld:yes"<<endl : cout<<"hellowrld:no"<<endl;
insert(root, "hell");
search(root, "hell") ? cout<<"hell:yes"<<endl : cout<<"hell:no"<<endl;
insert(root, "h");
search(root, "h") ? cout<<"h:yes"<<endl : cout<<"h:no"<<endl;
deletion(root,"hello");
search(root, "hello") ? cout<<"hello:yes"<<endl : cout<<"hello:no"<<endl;
search(root, "helloworld") ? cout<<"helloworld:yes"<<endl : cout<<"helloworld:no"<<endl;
search(root, "hell") ? cout<<"hell:yes"<<endl : cout<<"hell:no"<<endl;
deletion(root,"h");
search(root, "h") ? cout<<"h:yes"<<endl : cout<<"h:no"<<endl;
search(root, "helloworld") ? cout<<"helloworld:yes"<<endl : cout<<"helloworld:no"<<endl;
search(root, "hell") ? cout<<"hell:yes"<<endl : cout<<"hell:no"<<endl;
deletion(root,"helloworld");
search(root, "helloworld") ? cout<<"helloworld:yes"<<endl : cout<<"helloworld:no"<<endl;
search(root, "hell") ? cout<<"hell:yes"<<endl : cout<<"hell:no"<<endl;
deletion(root,"hell");
search(root, "hell") ? cout<<"hell:yes"<<endl : cout<<"hell:no"<<endl;
if(root==NULL){
cout<<"trie is empty"<<endl;
}
search(root, "hello") ? cout<<"hello:yes"<<endl : cout<<"hello:no"<<endl;
return 0;
}