1
我無法打印出trie
在C的話,我已經實現了trie
這樣的:特里印刷用C
struct trie {
struct trie *children[26];
char letter;
int wordEnd;
};
void printSubtree(struct trie *subtree) {
int i;
if (subtree == NULL){
return;
}
else {
for (i = 0; i<26;i++) {
if (subtree->children[i]!= NULL) {
printf("%c", subtree->children[i]->letter);
printSubtree(subtree->children[i]);
}
}
}
}
void printResult(){
struct trie *temp;
temp = master;
int i ;
if (temp){
for (i = 0; i<26;i++) {
if (temp->children[i]!= NULL) {
printf("%c", temp->children[i]->letter);
printSubtree(temp->children[i]);
printf("\n");
printf("\n");
}
}
}
}
我知道這是不對的,但我不確定如何使用遞歸來打印出單詞。如果trie
的和"abe"
存儲爲不同的單詞,最終打印出來的只是字符串"abce"
,將和"abe"
都插入爲不同的單詞。 後來,我不確定如何使用DFS打印出來,因爲不會DFS出行一路,印刷說了出來,再回去的"b"
水平,看到"b"
有一個孩子有沒有被訪問過,然後打印出來,導致字符串"abce"
無論如何?
你可能需要不斷表示,到目前爲止字的整串併爲您下降線索到你追加字母。當你找到一個單詞時,打印該字符串。你不能隨便打印字母,因爲它們只能打印一次,但可能屬於幾個字。 –