Я пытаюсь реализовать структуру данных trie в C
и не знаю, как динамически назвать новые узлы, которые добавлены в словарь. Посмотрите последние несколько строк моего метода add
, где я пытаюсь создать новый узел и попытаюсь указать на него.Создание новых узлов в структуре данных trie в C
bool add(char *word, node tree)
{
// Creates a variable to store the current char in the string
char currChar = word[0];
// Converts the current char to an index #
int currCharIndex = ((int) toupper(currChar)) - 65;
// Checks if we've reached the end of the word
if (currChar == '\0')
{
// Sets current node word to true
tree.word = true;
}
// Checks if next letter in word is not NULL
else if (tree.children[currCharIndex] != NULL)
{
// Follows the pointer
return add(&word[1], *tree.children[currCharIndex],);
}
else
{
//Creates a new node
node; // TODO: name node
// Points the current node to the new node
tree.children[currCharIndex] = &// TODO: new node name
return add(&word[1], *tree.children[currCharIndex]);
}
}
Вот как я определяю node
:
typedef struct node
{
bool word;
struct node *children[26];
}
node;
bool search(char *word, node tree);
bool add(char *word, node tree);
int main(void)
{
node dictionary;
}
Пожалуйста, покажите, как определяется 'node', так что SO может вам помочь. Путь 'node' передан' add' (pass by value) выглядит неправильно. Вам может понадобиться передать указатель на 'node'. Точно так же вы не можете добавить локальную переменную 'node' to 'add', поскольку она выйдет за рамки. Вы должны выделить память с помощью 'malloc', а затем добавить ее. –
user1969104