/
11Trie.c
92 lines (82 loc) · 2.06 KB
/
11Trie.c
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
/*************************************************************************
> File Name: 10Trie.c
> Author: snowflake
> Mail: ︿( ̄︶ ̄)︿
> Created Time: 2018年09月22日 星期六 18时40分36秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BASE 26
#define BASE_LETTER 'a'
typedef struct Node {
int flag;
struct Node *next[BASE];
}Node, *Trie;
Node *get_new_node() {
Node *p = (Node *)calloc(sizeof(Node), 1);
return p;
}
void clear(Trie root) {
if (root == NULL) return ;
for (int i = 0; i < BASE; i++) {
if (root->next[i] == NULL) continue;
clear(root->next[i]);
}
free(root);
return ;
}
Node *insert(Trie root, const char *str) {
if (root == NULL) root = get_new_node();
Node *p = root;
for (int i = 0; str[i]; i++) {
int ind = str[i] - BASE_LETTER;
if (p->next[ind] == NULL) p->next[ind] = get_new_node();
p = p->next[ind];
}
p->flag = 1;
return root;
}
int search(Trie root, const char *str) {
Node *p = root;
int i = 0;
while (p && str[i]) {
int ind = str[i++] - BASE_LETTER;
p = p->next[ind];
}
return (p && p->flag);
}
char *get_random_string(char *str) {
int len = rand() % 10 + 1;
for (int i = 0; i < len; i++) {
str[i] = rand() % BASE + BASE_LETTER;
}
str[len] = 0;
return str;
}
void output(Trie root, char *str, int level) {
if (root == NULL) return ;
str[level] = 0;
if (root->flag) {
printf("find word : %s\n", str);
}
for (int i = 0; i < BASE; i++) {
if (root->next[i] == NULL) continue;
str[level] = i + BASE_LETTER;
output(root->next[i], str, level + 1);
}
return ;
}
int main() {
srand(time(0));
Trie root = NULL;
char str[50] = {0};
for (int i = 0; i < 20; i++) {
get_random_string(str);
root = insert(root, str);
printf("insert: %s\n", str);
}
output(root, str, 0);
return 0;
}