#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#define MAX_LINE 100
#define maxChar 8
typedef struct Node {
char * data[25];
//struct Node *similar;
struct Node * children[8];
struct Node * parent;
bool isWord;
}
Node;
typedef struct Trie {
Node * root;
}
Trie;
//will convert the letter to a key for my tree, we only use
//the numbers 2-9 for t9 word so I will convert each char to it's
//t9 value and subtract 2 so that I do not leave the first two
//spots of my array empty.
int charToInt(char s) {
switch (s) {
case 'a':
case 'b':
case 'c':
return 0;
case 'd':
case 'e':
case 'f':
return 1;
case 'g':
case 'h':
case 'i':
return 2;
case 'j':
case 'k':
case 'l':
return 3;
case 'm':
case 'n':
case 'o':
return 4;
case 'p':
case 'q':
case 'r':
case 's':
return 5;
case 't':
case 'u':
case 'v':
return 6;
case 'w':
case 'x':
case 'y':
case 'z':
return 7;
default:
return -1;
}
}
//allocates the space for and creates new node
Node * newNode(Node * parent) {
Node
* n
= (Node
* ) malloc(1 * sizeof(struct Node
)); n -> isWord = false;
for (int i = 0; i < 25; i++) {
n -> data[i] = NULL;
}
for (int i = 0; i < maxChar; i++) {
n -> children[i] = NULL;
}
n -> parent = parent;
return n;
}
Node * setRoot() {
Node
* n
= (Node
* ) malloc(1 * sizeof(struct Node
)); //n->similar = NULL;
n -> isWord = false;
for (int i = 0; i < 25; i++) {
n -> data[i] = NULL;
}
for (int i = 0; i < maxChar; i++) {
n -> children[i] = NULL;
}
n -> parent = NULL;
return n;
}
//checks if it is a word
bool isItAWord(Node * n) {
return n -> isWord;
}
//insert function. Starts at root and will traverse down tree creating
//additional nodes as needed until reaches the end of the word
void insert(Node * root, char * key) {
Node * trieExplorer = root;
//Node *trieExplorer = malloc(1* sizeof(Node));
int depth;
int index;
char word[25] = {
0
};
for (depth = 0; depth < length; depth++) {
index = charToInt(key[depth]);
word[depth] = key[depth];
if (!trieExplorer -> children[index]) {
trieExplorer -> children[index] = newNode(trieExplorer);
}
trieExplorer = trieExplorer -> children[index];
}
trieExplorer -> isWord = true;
for (int i = 0; i < 25; i++) {
trieExplorer -> data[i] = & word[i];
}
printf(" %d ", trieExplorer
-> isWord
); }
//starting at the root of the trie this will crawl down the tree using the key
//until it either reaches a dead end or the end of the input
bool search(Node * root, char * key) {
Node * searchNode = root;
int depth;
int index;
char word[25] = {
0
};
for (depth = 0; depth < length; depth++) {
index = charToInt(key[depth]);
word[depth] = key[depth];
if (searchNode -> children[index] != NULL) {
searchNode = searchNode -> children[index];
}
}
return isItAWord(searchNode);
}
void freeNode(Node * n) {
if (n
-> data
!= NULL
) free((n
-> data
)); for (int i = 0; i < 8; i++) {
if (n -> children[i] != NULL) {
}
}
}
void freeTrie(Node * n) {
if (n != NULL) {
for (int i = 0; i < 8; i++) {
if (n -> children[i] != NULL) {
freeTrie(n -> children[i]);
}
}
freeNode(n);
}
}
int main(int argc, char ** argv) {
char word[MAX_LINE];
//Node *start = malloc(1* sizeof(Node));
Node * start = setRoot();
while (fgets(word
, MAX_LINE
, stdin
)) { insert(start, word);
}
printf("%d\n", search
(start
, "rocks")); printf("%d\n", search
(start
, "up")); printf("%d\n", search
(start
, "tuple")); printf("%d\n", search
(start
, "things")); printf("%d\n", search
(start
, "zebra")); //fclose(file);
//freeTrie(start);
//free(start);
}