0

I'm trying to build a ternary search tree for a school project. As far as I can tell, my code looks okay.
But, the leaf nodes of the original root function are not being initialized when I use malloc. So every string comparison beyond the first one (whatever the root's string is) wind up empty (not null, but ""). I'm not passing any pointers as function arguments, so I can't figure out what the problem is. Please help!
I suspect the problem is somewhere in this part of the code. I really have racked my brain and searched and cannot figure out what my problem is.
More code is available if you want.

I have checked and root->right points to an entirely different location than current->right on the first pass. That's my real problem. I figure it must be an error in declaration or something, but I just can't figure it out.

node_t* buildTree(FILE *file)
{
// check for at least one input
// set this input as the root of the tree
// if there is no input, print error message and return
char start[MAX_TOKEN_LENGTH];
fscanf(file, "%s", start);
printf("Root: %s\n", start);
node_t *root = malloc(sizeof(node_t));
root->counter = 1;
root->depth = 0;
root->right = NULL;
root->middle = NULL;
root->left = NULL;
printf("Allocated.\n");

int n = 0;
while(n < strlen(start))
{
    root->string[n] = start[n];
    n++;
}
printf("Root stored as: %s\n", root->string);

char word[MAX_TOKEN_LENGTH];
while(fscanf(file, "%s", word) != EOF)
{
        printf("Read a word: %s\n", word);

        // Reset depth and sort location
        // Start sorting from the root element
        int depth = 0;
        node_t *current = root;

        // Continue sorting into the tree until a null node is encountered.
        // Increment the depth each pass
        while (current->string != NULL)
        {
            printf("Comparing %s with %s, result is %d\n", word, current->string, strcmp(word, current->string));
            if ( ( word[0] == current->string[0] ) && ( strcmp (word, current->string) != 0 ) )
            {
                printf("Middle node\n");
                if (current->middle == NULL)
                {
                    printf("Middle node is empty; creating new leaf.\n");
                    current->middle = malloc(sizeof(node_t));
                    int n = 0;
                    while (n < strlen(word))
                    {
                        current->middle->string[n] = word[n];
                        n++;
                    }
                    current->middle->counter = 0;
                    current->middle->depth = ++depth;
                    current->middle->left = NULL;
                    current->middle->middle = NULL;
                    current->middle->right = NULL;
                    break;
                }
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Evan
  • 5
  • 2

1 Answers1

1

You have:

while (n > strlen(word))

it should be

while (n < strlen(word))
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • While that is true, it still doesn't solve the problem. root->right points to an entirely location than current->right on the first pass...that's really what I need to fix. Will update the question. – Evan Sep 23 '11 at 04:57
  • Actually, that definitely solved my entire problem. It's the little things...I was beating my brain over this. Major kudos for taking the time out of your day to help me. – Evan Sep 23 '11 at 05:03