[Algorithm] Trie

Trie

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
#include <stdio.h>
#include <memory.h>

#define Alpha_N_Nums 62
#define INPUTS 20
#define LETTERS 20

struct Trie{
Trie *next[Alpha_N_Nums];
bool is_Finish;
Trie() : is_Finish(false){
memset(next,0,sizeof(next));
}
~Trie(){
for(int i=0;i < Alpha_N_Nums; i++)
if(next[i]!=0)
delete next[i];
}
void insert(const char *key){
if(is_Alpha(*key)!=0){
if(next[get_Index(*key)]==0)
next[get_Index(*key)] = new Trie();
next[get_Index(*key)]->insert(key+1);
}
else
is_Finish = true;
}
bool is_Exsist(const char *key){
if(is_Alpha(*key)){
if(next[get_Index(*key)]==0)
return false;
return next[get_Index(*key)]->is_Exsist(key+1);
}
return is_Finish;
}
bool is_Prefix(const char *key){
if(is_Alpha(*key)){
if(next[get_Index(*key)]==0)
return false;
return next[get_Index(*key)]->is_Prefix(key+1);
}
return true;
}

int get_Index(char c){
if('A'<=c&&c<='Z')
return c-'A';
else if('a'<=c&&c<='z')
return 26 + c - 'a';
else
return 52 + c - '0';
}

bool is_Alpha(char c){
return ('a'<=c&&c<='z')||('A'<=c&&c<='Z')||('0'<=c&&c<='9');
}
};

int main(int argc, char** argv){
char input[INPUTS][26] = {"eevee", "vaporeon", "jolteon", "flareon", "espeon", "umbreon", "leafeon", "glaceon", "sylveon", "pikachu",
"Charizard", "Butterfree", "Dragonite", "Mewtwo", "Mew", "Persian", "001Bulbasaur", "002bulbasaur", "Venusaur003", "5011"};

char letter[LETTERS][26] = {"pika", "leaf", "monster", "eve", "espn", "umbrella", "flame", "grace", "sylveon", "poket",
"Butter", "001B", "Venusaur003", "50", "Mew", "p", "003", "Vinus", "Hayoung", "KOREA"};
Trie *root = new Trie();
for(int i=0;i<INPUTS;i++){
printf("INPUT %s\n",input[i]);
root->insert(input[i]);
}
printf("\n\n\n");
for(int i=0;i<LETTERS;i++){
root->is_Prefix(letter[i]) ?
printf("Is Prefix %s ? : YES\n",letter[i]) : printf("IS Prefix %s ? : NO\n",letter[i]);
}
printf("\n\n\n");
for(int i=0;i<LETTERS;i++){
root->is_Exsist(letter[i]) ?
printf("Is Exsist %s ? : YES\n",letter[i]) : printf("IS Exsist %s ? : NO\n",letter[i]);
}
delete root;
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/21/Algorithm/trie/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.