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

  • SEARCH
  • Home
  • 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 5838239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:26:48+00:00 2026-05-22T11:26:48+00:00

I have been trying to implement the basic functions of inserting characters into a

  • 0

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)

  • 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-22T11:26:49+00:00Added an answer on May 22, 2026 at 11:26 am

    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:

    // 'head' is assumed to be a valid pointer, its 'child' field either NULL or a valid 
    // pointer
    TR currentNode = head;
    while ( *word )
    {
        assert(currentNode != NULL);
    
        if ( currentNode->child == NULL || currentNode->child->thisChar < *word )
        {
            // We need to insert a new node first in the child list
            TR newNode = malloc(sizeof *currentNode);
            newNode->thisChar = *word;
            newNode->sibling = currentNode->child;
            newNode->child = NULL;
            currentNode->child = newNode;
            currentNode = newNode;
        }
        else
        {
            // Find the place to insert next node
            currentNode = currentNode->child;
            while ( currentNode->sibling && currentNode->thisChar < *word )
                currentNode = currentNode->sibling;
    
            // If the current node already represents current character, we're done
            // Otherwise, insert a new node between the current node and its sibling
            if ( currentNode->thisChar != *word )
            {
                TR newNode = malloc(sizeof *currentNode);
                newNode->thisChar = *word;
                newNode->child = NULL;
                newNode->sibling = currentNode->sibling;
                currentNode->sibling = newNode;
                currentNode = newNode;
            }
        }
        word++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure out how to use JAX-RS for quite some
I have been trying to implement pinch zoom/in-out for PhotoView (a UIImageView instance) using
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I've been trying to implement unit testing and currently have some code that does
I've been trying to figure this out for hours now, and I'm at my
I have been trying to implement Nhibernate.Linq 1.0.0.4000 together with Nhibernate 2.1.2.4000. After what
I am new to open gl, and have been trying to do some basic
I have been trying to implement my own linked list class for didactic purposes.
i have been trying to implement a bottombar for my site, however the vision
I have been trying to implement this code , where i capture a image

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.