Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:57:57+00:00 2026-05-25T19:57:57+00:00

I’m trying to build a ternary search tree for a school project. As far

  • 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;
                }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:57:58+00:00Added an answer on May 25, 2026 at 7:57 pm

    You have:

    while (n > strlen(word))
    

    it should be

    while (n < strlen(word))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.