-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
136 lines (117 loc) · 2.42 KB
/
trie.cpp
File metadata and controls
136 lines (117 loc) · 2.42 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <cstring>
using namespace std;
struct Node
{
char* date;
Node* children[26];
Node():date{nullptr}
{
memset(children, 0, 26* sizeof(Node*));
}
};
class Ar
{
private:
Node root;
void set_Data(Node& n, const char* ss)
{
auto len = strlen(ss);
auto ns = new char[len +1];
memcpy(ns, ss, len+1);
n.date=ns;
// std::cout << len << '\n';
// auto ns =
}
void add(Node& n, const char* a, const char* ss)
{
if (*a == 0)
{
set_Data(n,ss);
// std::cout << "ss" << '\n';
return;
}
auto index = *a-'a';
auto& e = n.children[index];
if (e==nullptr)
{
e = new Node;
}
add(*e,a+1,ss);
// std::cout << *a <<*ss<< '\n';
}
const char* find(const Node& n, const char *ss) const
{
if (*ss == 0)
{
return n.date;
}
auto index = *ss-'a';
auto e = n.children[index];
if (e==nullptr)
{
return nullptr;
}
return find(*e,ss+1);
}
public:
void print(const Node& n, char* ss)const
{
if (n.date == nullptr)
{
std::cout << "traduccion" <<n.date<< '\n';
}
for (auto i = 0; i < 26; i++)
{
auto e = n.children[i];
if (e !=nullptr)
{
char index =(char)(i+'a');
char* letter = new char[2];
letter[0]=index;
letter[1]='\0';
strcat(ss, (const char*)letter);
std::cout << ss << '\n';
delete []letter;
print(*e,ss);
}
}
return;
}
Ar()
{
}
Ar& add(const char* k, const char* ss)
{
add(root, k,ss);
return *this;
}
void print()const
{
char* ss;
print(root, ss);
}
const char* find(const char* ss)const
{
return find(root, ss);
}
~Ar()
{
delete[] root.date;
}
};
int main()
{
Ar x;
x.add("one","uno");
x.add("two","dos");
x.add("thre","tres");
x.add("four","cuatro");
x.add("five","cinco");
x.print();
auto i= x.find("uno");
if( i != nullptr)
{
cout<<"result->"<<i<<endl;
}
}