-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.cpp
101 lines (88 loc) · 2.17 KB
/
Trie.cpp
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
#include<iostream>
using namespace std;
#include<malloc.h>
struct trienode{
struct trienode *child[26];
int count; // for counting duplicate strings
};
struct trienode mempool[100005];
int memsize=0;
trienode* mymalloc(){
if(memsize<100005){
return &mempool[memsize++];
}
else{
trienode* tempnode = (trienode*)malloc(sizeof(trienode));
return tempnode;
}
}
trienode* getnewnode(){
trienode* temp = mymalloc();
for(int i=0;i<26;i++){
temp->child[i]=NULL;
}
temp->count = 0;
}
struct trienode* root; // defined globally (always try to pass a copy in a function)
void init_trie(){
memsize=0;
root = getnewnode();
}
void trie_insert(const char *key){
// key is a string
trienode* pcrawl = root;
int index,i=0;
while(key[i]!='\0'){
index = key[i]-'a';
if(pcrawl->child[index]==NULL){
pcrawl->child[index]=getnewnode();
}
pcrawl = pcrawl->child[index];
i++;
}
//insertion is complete now increase the count
pcrawl->count++;
}
bool trie_search(const char *key){
trienode* pcrawl = root;
int index,i=0;
while(key[i]!='\0'){
index = key[i];
if(pcrawl->child[index]){// sometimes root doesn't exist so check whether root is null or not;
return false;
}
pcrawl = pcrawl->child[index];
i++;
}
if(pcrawl->count>0){
return true;
}
return false;
}
void trie_delete(const char *key){
trienode* pcrawl = root;
if(pcrawl==NULL){
return;
}
int index,i=0;
while(key[i]!='\0'){
index = key[i]-'a';
if(pcrawl->child[index]==NULL){
return;
}
pcrawl = pcrawl->child[index];
i++;
}
if(pcrawl->count > 0){//sometimes key prefix is given and you dont have to delete it; he from hello string
pcrawl->count--;
}
}
void crawl_trie( trienode * r){
if(r==NULL)
return;
for(int i=0;i<26;i++){
if(r->child[i]){
crawl_trie(r->child[i]);
}
}
}