-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie_functions.c
88 lines (66 loc) · 1.7 KB
/
Trie_functions.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "Trie.h"
#define array_size(a) sizeof(a) / sizeof(a[0])
// Alphabet size (# of symbols)
#define ALPHABET_SIZE (26)
// Converts key current character into index
// use only 'a' through 'z' and lower case
#define get_index(c) ((int)c - (int)'a')
struct trie_node
{
struct trie_node *arr[ALPHABET_SIZE];
// end_of_word is true if the node represents
// end of a word
bool end_of_word;
};
typedef struct trie_node trie_node;
struct trie_node *create_trie_node()
{
struct trie_node *temp = NULL;
temp = (struct trie_node *)malloc(sizeof(struct trie_node));
if (temp)
{
int i;
temp->end_of_word = false;
for (i = 0; i < ALPHABET_SIZE; i++)
temp->arr[i] = NULL;
}
return temp;
}
// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
void insert(struct trie_node *root, const char *key)
{
int l;
int len = strlen(key);
int index;
struct trie_node *p = root;
for (l = 0; l < len; l++)
{
index = get_index(key[l]);
if (!p->arr[index])
p->arr[index] = create_trie_node();
p = p->arr[index];
}
// mark last node as leaf
p->end_of_word = true;
}
// Returns true if key presents in trie, else false
bool search_trie(struct trie_node *root, const char *key)
{
int l;
int len = strlen(key);
int index;
struct trie_node *p = root;
for (l = 0; l < len; l++)
{
index = get_index(key[l]);
if (!p->arr[index])
return false;
p = p->arr[index];
}
return (p != NULL && p->end_of_word);
}