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 7662609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:50:38+00:00 2026-05-31T13:50:38+00:00

Doing a project with BSTs, I have a logic fault somewhere in my insertion

  • 0

Doing a project with BSTs, I have a logic fault somewhere in my insertion function, just can’t seem to find it.

Insertion function:

int bst_insert (bst_t *tree, bst_key_t key)
{
  bst_node_t *node, *temp_node;
  if( tree == NULL ) {
    printf("Invalid tree pointer.\n");
    return;
  }
  if( tree->root == NULL ) {
    node = (bst_node_t *)malloc(sizeof(bst_node_t));
    node->left = node->right = NULL;
    node->key = key;
    tree->root = node;
    return 1;
  }
  temp_node = tree->root;
  while(1) {
    if( temp_node == NULL ) {
      node = (bst_node_t *)malloc(sizeof(bst_node_t));
      node->left = node->right = NULL;
      node->key = key;
      temp_node = node;
      return 1;
    }
    if( temp_node->key == key ) {
      temp_node->data_ptr = data;
      return 0;
    } else if( key < temp_node->key ) {
      temp_node = temp_node->left;
    } else if( temp_node->key < key ) {
      temp_node = temp_node->right;
    }
  }  
}

In using this function, inserting one node works just fine (probably because the tree->root is null, so it exits inside that if statement), but when I try and insert a second and leave this function, the tree only has the first node in it.

Let me know if I forgot to give any pertinent information.

  • 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-31T13:50:40+00:00Added an answer on May 31, 2026 at 1:50 pm

    The problem is here:

    temp_node = tree->root;
    ...
    temp_node = node;
    

    You’re assuming that when you perform the second assignment, it actually affects the node temp_node was pointing to. But in fact, it simply replaces the contents of temp_node without changing anything else (it’s a local variable).

    One solution is to have a pointer to a “node pointer”. Then, when you change the value that this pointer is pointing to, you’re actually “changing the world” instead of changing a local variable. Something like:

    bst_node_t **temp_node;
    ...
    temp_node = &tree->root;
    
    /* To change the value that temp_node is pointing to */
    *temp_node = node;
    
    /* To change temp_node itself */
    ...
    } else if( key < temp_node->key ) {
      temp_node = &temp_node->left;
    } else if( temp_node->key < key ) {
      temp_node = &temp_node->right;
    }
    

    It is possible to fix this without using a pointer-to-pointer, but then you have to keep remembering the previous (parent) pointer, and whether you need to change left or right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing project euler question 33 and have divised a refactor to solve
I'm doing a project in seam that requires restful URLs. I have a view
while doing flex project iam unable to start my Jboss server. can any one
I am doing project in ASP.Net, and i have used QueryString for dynamic profiles
I'm doing a project about cooking recipes with PHP(with Codeigniter) and MYSQL. I have
I am doing my project in struts2 and i have created the login pages
I am doing project euler question 63 where I must find the amount of
As the titles says I can't seem to build the project with OpenGL and
Am doing a project in C#.net.I have to use the same forms to all
im doing a project in which will use google api can anyone post a

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.