Skip to content

Commit b68daa3

Browse files
committed
Add prefix tree
1 parent e70ea33 commit b68daa3

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ PROGRAMS = m_based \
4646
sha1_demo\
4747
huffman_demo \
4848
word_seg_demo \
49-
md5_demo
49+
md5_demo \
50+
trie_demo
5051

5152

5253
all: $(PROGRAMS)

include/trie.h

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*******************************************************************************
2+
* DANIEL'S ALGORITHM IMPLEMENTAIONS
3+
*
4+
* /\ | _ _ ._ o _|_ |_ ._ _ _
5+
* /--\ | (_| (_) | | |_ | | | | | _>
6+
* _|
7+
*
8+
* Trie (Prefix tree)
9+
*
10+
* http://en.wikipedia.org/wiki/Trie
11+
******************************************************************************/
12+
13+
#ifndef __TRIE_H__
14+
#define __TRIE_H__
15+
#include <stdio.h>
16+
#include <stdint.h>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
#include <ctype.h>
20+
21+
namespace alg {
22+
const int NUMWORD = 26;
23+
24+
class Trie {
25+
private:
26+
class node {
27+
public:
28+
int words;
29+
int prefixes;
30+
node *edges[NUMWORD];
31+
node():words(0),prefixes(0) {
32+
memset(edges, 0, sizeof(edges));
33+
}
34+
~node() {
35+
for (int i=0;i<NUMWORD;i++) {
36+
if (edges[i] != NULL) {
37+
delete edges[i];
38+
}
39+
}
40+
}
41+
};
42+
node *m_root;
43+
public:
44+
Trie() {
45+
m_root = new node;
46+
}
47+
48+
~Trie() {
49+
delete m_root;
50+
m_root=NULL;
51+
}
52+
53+
void Add(char * str) {
54+
_lowercase(str);
55+
_add(m_root, str);
56+
}
57+
58+
int Count(char *str) {
59+
char * _str = strdup(str);
60+
_lowercase(_str);
61+
62+
int cnt = _count(m_root, _str);
63+
free(_str);
64+
return cnt;
65+
}
66+
67+
int CountPrefix(char *prefix) {
68+
char * _str = strdup(prefix);
69+
_lowercase(_str);
70+
71+
int cnt = _count_prefix(m_root, _str);
72+
free(_str);
73+
return cnt;
74+
}
75+
76+
private:
77+
void _lowercase(char *str)
78+
{
79+
int i;
80+
for (i=0;str[i];i++) {
81+
str[i] = tolower(str[i]);
82+
}
83+
}
84+
85+
void _add(node *n, char * str) {
86+
if (str[0] == '\0') {
87+
n->words++;
88+
} else {
89+
n->prefixes++;
90+
int index=str[0]-'a';
91+
if (n->edges[index]==NULL) {
92+
n->edges[index] = new node;
93+
}
94+
95+
_add(n->edges[index], ++str);
96+
}
97+
}
98+
99+
int _count(node *n, char * str) {
100+
if (str[0] == '\0') {
101+
return n->words;
102+
} else {
103+
int index=str[0]-'a';
104+
if (n->edges[index]==NULL) {
105+
return 0;
106+
}
107+
108+
return _count(n->edges[index], ++str);
109+
}
110+
}
111+
112+
int _count_prefix(node *n, char * str) {
113+
if (str[0] == '\0') {
114+
return n->prefixes;
115+
} else {
116+
int index=str[0]-'a';
117+
if (n->edges[index]==NULL) {
118+
return 0;
119+
}
120+
121+
return _count_prefix(n->edges[index], ++str);
122+
}
123+
}
124+
};
125+
}
126+
127+
#endif //

src/trie_demo.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "trie.h"
2+
3+
int main(void) {
4+
alg::Trie trie;
5+
const char *strs[] = {"sap", "sat", "sad", "rat", "ram", "rag", "rap", "sat", "ram","rag", "nap", "Nat", "lap"};
6+
7+
for (uint32_t i=0;i<sizeof(strs)/sizeof(char*);i++) {
8+
char * str = strdup(strs[i]);
9+
trie.Add(str);
10+
free(str);
11+
}
12+
13+
printf("count of :%s %d\n", "sat", trie.Count("sat"));
14+
printf("count of prefix :%s %d\n", "ra", trie.CountPrefix("ra"));
15+
}

0 commit comments

Comments
 (0)