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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:32:20+00:00 2026-06-15T12:32:20+00:00

Disclaimer: This is for an assignment. I am not asking for explicit code answers,

  • 0

Disclaimer: This is for an assignment. I am not asking for explicit code answers, only help understanding why my code isn’t working.

I am trying to implement a basic Binary Search Tree, but I am having problems with my _addNode(...) function.

Here’s the problem. When I walk through my code with the debugger, I notice that leaf nodes are created infinitely on both sides (left and right) so aside from the creation of the root, there is never any point when a leaf node is NULL. The problem is that I am asking my program to create a new node whenever it finds a NULL value where a leaf would be. Therefore, if there are never any NULL values, there will never be any new leaves created, right?

The other issue I’m running into is with my compare(...) function. Stepping through it in the debugger shows it to iterate through the function several times, never actually returning a value. When it returns to the calling function, it drops back into the compare(...) function and loops infinitely. Again, I don’t know why this is happening considering I have valid return statements in each if statement.

Here is all the code you’ll probably need. If I left something out, let me know and I’ll post it.

struct Node {
    TYPE         val;
    struct Node *left;
    struct Node *right;
};

struct BSTree {
    struct Node *root;
    int          cnt;
};

struct data {
    int number;
    char *name;
};

int compare(TYPE left, TYPE right)
{
    assert(left != 0);
    assert(right != 0);

    struct data *leftData = (struct data *) left;
    struct data *rightData = (struct data *) right;

    if (leftData->number < rightData->number) {
        return -1;
    }
    if (leftData->number > rightData->number) {
        return 1;
    } else return 0;
}

void addBSTree(struct BSTree *tree, TYPE val)
{
    tree->root = _addNode(tree->root, val);
    tree->cnt++;
}

struct Node *_addNode(struct Node *cur, TYPE val)
{
    assert(val != 0);

    if(cur == NULL) {
        struct Node * newNode = malloc(sizeof(struct Node));
        newNode->val = val;
        return newNode;
    }
    if (compare(val, cur->val) == -1) {
        //(val < cur->val)
        cur->left = _addNode(cur->left, val);
    } else cur->right = _addNode(cur->right, val);

    return cur;
}

Edit: Adding the below function(s)

int main(int argc, char *argv[])
{
    struct BSTree *tree = newBSTree();

    /*Create value of the type of data that you want to store*/
    struct data myData1;
    struct data myData2;
    struct data myData3;
    struct data myData4;

    myData1.number = 5;
    myData1.name = "rooty";
    myData2.number = 1;
    myData2.name = "lefty";
    myData3.number = 10;
    myData3.name = "righty";
    myData4.number = 3;
    myData4.name = "righty";

    /*add the values to BST*/
    addBSTree(tree, &myData1);
    addBSTree(tree, &myData2);
    addBSTree(tree, &myData3);
    addBSTree(tree, &myData4);

    /*Print the entire tree*/
    printTree(tree);
    /*(( 1 ( 3 ) ) 5 ( 10 ))*/
    return 1;
}
  • 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-06-15T12:32:22+00:00Added an answer on June 15, 2026 at 12:32 pm

    Maybe you could try setting right and left to NULL right after malloc:

    struct Node * newNode = malloc(sizeof(struct Node));
    newNode->left = NULL;
    newNode->right = NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone
( DISCLAIMER : This is NOT a question about understanding the difference between abstract
Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This is for a programming class, but it is not the answer or
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
Disclaimers: I am not interested in doing this in any real production code. I
Disclaimer: This question is not about fixing visual studio So, I've used VSS for
Disclaimer This is not strictly a programming question, but most programmers soon or later

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.