-
Notifications
You must be signed in to change notification settings - Fork 30
/
trie.h
96 lines (85 loc) · 1.95 KB
/
trie.h
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
/**
* @file trie.h
*
* @author hutusi ([email protected])
*
* @brief Trie Tree.
*
* @date 2019-08-11
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#ifndef RETHINK_C_TRIE_H
#define RETHINK_C_TRIE_H
#include "hash_table.h"
#include <stdbool.h>
/**
* @brief Definition of a @ref TrieNode.
*
*/
typedef struct _TrieNode {
/** Value of the node. */
char data;
bool ending;
HashTable *children;
} TrieNode;
/**
* @brief Definition of a @ref Trie.
*
*/
typedef struct _Trie {
TrieNode *root;
} Trie;
/**
* @brief Allcate a new Trie.
*
* @return Trie* The new Trie if success, otherwise NULL.
*/
Trie *trie_new();
/**
* @brief Delete a Trie and free back memory.
*
* @param trie The Trie to delete.
*/
void trie_free(Trie *trie);
/**
* @brief Insert a string into a Trie.
*
* @param trie The Trie.
* @param str The string.
* @param len The length of the string.
* @return int 0 if success.
*/
int trie_insert(Trie *trie, const char *str, unsigned int len);
/**
* @brief Delete a string into a Trie.
*
* Just mark the ending as 'false'.
*
* @param trie The Trie.
* @param str The string.
* @param len The length of the string.
* @return int 0 if success.
*/
int trie_delete(Trie *trie, const char *str, unsigned int len);
/**
* @brief Check if a Trie include a string (full match).
*
* @param trie The Trie.
* @param str The string.
* @param len The length of the string.
* @return true Include a full match.
* @return false Not include a full match.
*/
bool trie_include(const Trie *trie, const char *str, unsigned int len);
/**
* @brief Get the last node of a Trie by a string.
*
* @param trie The Trie.
* @param str The string.
* @param len The length of the string.
* @return TrieNode* The last match node.
*/
TrieNode *trie_last_node(const Trie *trie, const char *str, unsigned int len);
#endif /* #ifndef RETHINK_C_TRIE_H */