I have been trying to implement the basic functions of inserting characters into a trie data structure in C. I have been trying to figure out what I am doing wrong, but for the last day or so I’ve been stumped/stuck.
Heres some code I’ve written up:
TR head = NULL;
void initDict () {
head = NULL;
}
TR newNode (char item) {
TR temp;
temp = malloc (sizeof(*temp));
temp->thisChar = item;
temp->child = NULL;
temp->sibling = NULL;
return temp;
}
TR insertInOrder (char item, TR trie) {
if (trie == NULL) {
trie = newNode(item);
} else if (trie->thisChar < item) {
insertInOrder(item, trie->sibling);
} else if (trie->thisChar > item) {
char temp = trie->thisChar;
trie->thisChar = item;
insertInOrder(temp, trie->sibling);
}
return trie;
}
void insert (char *word) {
char letter = *word;
TR temp = NULL;
while (*word != '\0') {
letter = *word;
if (head == NULL) {
head = newNode(letter);
temp = head->child;
word++;
} else {
temp = insertInOrder(letter, temp);
temp->child = head->child;
head->child = temp;
word++;
}
}
}
I can’t figure this out…
P.S checkLetter, is a boolean function that checks if the letter is already inside the trie (through traversing through the trie structure, i.e. trie = trie->sibling)
Any help would be appreciated =]
Cheers!
EDIT: changed my code, so that insertInOrder returns a value, but since insert is a void function and has to stay a void function, I don’t know of a way to insert nodes further down into the head of the trie (i.e. head->child, head->child->child etc)
You could re-think your insertion algorithm 🙂
I am not very good teacher, so I’ll just give you the solution without any good motivations. This is not compiled and verified though, think of this as pseudo-code to give you an idea of what I think is a better algorithm that handles some corner cases you seem to have missed, plus uses the ‘head’ pointer differently to yield a more consistent algorithm: