있는 이해 할 수없는 나는 몇 가지가있다 http://theoryofprogramming.com/2015/01/16/trie-tree-implementation/
할 내가 블로그를 통해 작업, 트라이 데이터 구조의 삽입의 C++ 구현을 구현하기 위해 노력 해왔다
if (traverse->children[*word - CASE] == NULL)
우리는 다음 주요 기능 1로 모든 요소를 초기화대로 : 나는 insertword
에서이 사항이 무엇을 이해 드릴 수 없습니다
#define ALPHABETS 26
#define CASE 'a'
#define MAX_WORD_SIZE 25
using namespace std;
struct Node
{
struct Node * parent;
struct Node * children[ALPHABETS];
vector<int> occurrences;
};
// Inserts a word 'text' into the Trie Tree
// 'trieTree' and marks it's occurence as 'index'.
void InsertWord(struct Node * trieTree, char word[], int index)
{
struct Node * traverse = trieTree;
while (*word != '\0') { // Until there is something to process
if (traverse->children[*word - CASE] == NULL) {
// There is no node in 'trieTree' corresponding to this alphabet
// Allocate using calloc(), so that components are initialised
traverse->children[*word - CASE] = (struct Node *) calloc(1, sizeof(struct Node));
traverse->children[*word - CASE]->parent = traverse; // Assigning parent
}
traverse = traverse->children[*word - CASE];
++word; // The next alphabet
}
traverse->occurrences.push_back(index); // Mark the occurence of the word
}
// Prints the 'trieTree' in a Pre-Order or a DFS manner
// which automatically results in a Lexicographical Order
void LexicographicalPrint(struct Node * trieTree, vector<char> word)
{
int i;
bool noChild = true;
if (trieTree->occurrences.size() != 0) {
// Condition trie_tree->occurrences.size() != 0,
// is a neccessary and sufficient condition to
// tell if a node is associated with a word or not
vector<char>::iterator charItr = word.begin();
while (charItr != word.end()) {
printf("%c", *charItr);
++charItr;
}
printf(" -> @ index -> ");
vector<int>::iterator counter = trieTree->occurrences.begin();
// This is to print the occurences of the word
while (counter != trieTree->occurrences.end()) {
printf("%d, ", *counter);
++counter;
}
printf("\n");
}
for (i = 0; i < ALPHABETS; ++i) {
if (trieTree->children[i] != NULL) {
noChild = false;
word.push_back(CASE + i); // Select a child
// and explore everything associated with the cild
LexicographicalPrint(trieTree->children[i], word);
word.pop_back();
// Remove the alphabet as we dealt
// everything associated with it
}
}
word.pop_back();
}
int main()
{
int n, i;
vector<char> printUtil; // Utility variable to print tree
// Creating the Trie Tree using calloc
// so that the components are initialised
struct Node * trieTree = (struct Node *) calloc(1, sizeof(struct Node));
char word[MAX_WORD_SIZE];
printf("Enter the number of words-\n");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
scanf("%s", word);
InsertWord(trieTree, word, i);
}
printf("\n"); // Just to make the output more readable
LexicographicalPrint(trieTree, printUtil);
return 0;
}
어떻게 우리가 null이 될 수 있습니까?
하지만 '은'은 이러한 구현에있어서,이 경우 – Mark
에서 'ALPHABETS' 상수는 각각의 자식 노드의 개수 인 26로 설정된다 * 워드 경우를 감산하는 이유. 문자열의 각 문자에서''a ''를 뺄 때, 우리는 모든 문자에서 어떤 자식을 찾을지를 효과적으로 찾습니다. 예를 들어, 'a'는 '0', 'b'는 '1', ..,'z '는 25가됩니다. –
주 구조에서 1로 구조를 초기화 했으니까요?() 다음에 우리가 traverse-> children [* word-case]를 NULL과 비교하는 방법은 1과 비교해서는 안된다. 만약 내가 틀렸다면 나를 교정해라. – Mark