trie-ツリー詳細(後続)
3922 ワード
01
// , , root
02
// isStr false next word ,
03
bool
Trie::deleteWord(
const
char
* word)
04
{
05
TrieNode * current = root;
06
std::stack<TrieNode*> nodes;
// ,
07
while
(*word !=
'\0'
&& current != 0)
08
{
09
nodes.push(current);
//
10
current = current->next[*word -
'a'
];
11
}
12
if
(current && current->isStr)
13
{
14
current->isStr =
false
;
// current word
15
while
(nodes.size() != 0)
16
{
17
char
c = *(--word);
18
current = nodes.top()->next[c -
'a'
];
//
19
bool
allNull =
true
;
20
for
(
int
i=0;i<26;++i)
// next
21
{
22
if
(current->next[i] != 0)
23
{
24
allNull =
false
;
25
}
26
}
27
if
(current->isStr == 0 && allNull == 0)
// isStr false next word ,
28
{
29
delete
current;
30
}
31
else
// , 。
32
{
33
break
;
34
}
35
nodes.top()->next[c -
'a'
] = 0;
// next current 0
36
nodes.pop();
37
}
38
return
true
;
39
}
40
else
41
{
42
return
false
;
43
}
44
}