-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
50 lines (45 loc) · 670 Bytes
/
Trie.cpp
File metadata and controls
50 lines (45 loc) · 670 Bytes
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
#include<stdio.h>
#include<string.h>
#define SZ 15
char s[SZ];
bool flag;
struct node{
int val;
node *next[10];
node(){
val=0;
memset(next,NULL,sizeof(next));
}
};
node *root;
void insert(char *str){
node *curr = root;
int i, ch, len=strlen(str);
for(i=0; i<len ;i++){
ch = str[i] - '0';
if(curr->next[ch] == NULL){
curr->next[ch] = new node();
}
curr = curr->next[ch];
}
curr->val++;
}
void traverse(node *p){
int i;
for(i=0;i<10;i++){
if(p->next[i]!=NULL){
if( p->val ){
flag =false;
}
traverse(p->next[i]);
delete p->next[i];
}
}
}
int main(){
root = new node();
gets(s);
insert(s);
traverse(root);
return 0;
}