-
Notifications
You must be signed in to change notification settings - Fork 2
/
_trie_util.h
52 lines (48 loc) · 1016 Bytes
/
_trie_util.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
#pragma once
#include <vector>
#include <string>
#define sIter(c, it)\
for (std::string::iterator it = c.begin(); it != c.end(); it++)
template <typename T>
tnode<T>* recur(tnode<T>* n, int offset = 0) {
if (n == nullptr) {
return nullptr;
}
tnode<T>* it = nullptr;
for (int i = offset; i < 128; i++) {
it = n->getChild(i);
if (it == nullptr) {
if (i == 127) {
return recur(n->getParent(), n->getParentIndex() + 1);
}
continue;
}
if (it->isEnd()) {
return it;
}
return recur(it, 0);
}
}
template <typename T>
tnode<T>* rrecur(tnode<T>* n, int offset) {
if (n == nullptr) {
return nullptr;
}
if (n->isEnd()) {
return n;
}
tnode<T>* it = nullptr;
for (int i = offset; i > -1; i--) {
it = n->getChild(i);
if (it == nullptr) {
if (i == 0) {
return rrecur(n->getParent(), n->getParentIndex() - 1);
}
continue;
}
if (it->isEnd()) {
return it;
}
return recur(it, 0);
}
}