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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:58:50+00:00 2026-05-14T18:58:50+00:00

stack. I’ve got a binary tree of type TYPE (TYPE is a typedef of

  • 0

stack. I’ve got a binary tree of type TYPE (TYPE is a typedef of data*) that can add and remove elements. However for some reason certain values added will overwrite previous elements. Here’s my code with examples of it inserting without overwriting elements and it not overwriting elements.

the data I’m storing:

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

typedef struct data data;

# ifndef TYPE
# define TYPE      data*
# define TYPE_SIZE sizeof(data*)
# endif

The tree struct:

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

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

The comparator for the data.

int compare(TYPE left, TYPE right) {
    int left_len; int right_len; int shortest_string;

    /* find longest string */
    left_len = strlen(left->name);
    right_len = strlen(right->name);
    if(right_len < left_len) { shortest_string = right_len; } else { shortest_string = left_len; }

    /* compare strings */
    if(strncmp(left->name, right->name, shortest_string) > 1) {
        return 1;
    }
    else if(strncmp(left->name, right->name, shortest_string) < 1) {
        return -1;
    }
    else {
        /* strings are equal */

        if(left->number > right->number) {
            return 1;
        }
        else if(left->number < right->number) {
            return -1;
        }
        else {
            return 0;
        }
    }
}

And the add method

struct Node* _addNode(struct Node* cur, TYPE val) {
    if(cur == NULL) {
        /* no root has been made */
        cur = _createNode(val);

        return cur;
    }
    else {
        int cmp;

        cmp = compare(cur->val, val);
        if(cmp == -1) {
            /* go left */

            if(cur->left == NULL) {
                printf("adding on left node val %d\n", cur->val->number);
                cur->left = _createNode(val);
            }
            else {
                return _addNode(cur->left, val);
            }
        }
        else if(cmp >= 0) {
            /* go right */

            if(cur->rght == NULL) {
                printf("adding on right node val %d\n", cur->val->number);
                cur->rght = _createNode(val);
            }
            else {
                return _addNode(cur->rght, val);
            }
        }

        return cur;
    }
}

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

The method to create a new node:

struct Node* _createNode(TYPE val) {
struct Node* new_node;
new_node = (struct Node*)malloc(sizeof(struct Node*));
new_node->val = val;
new_node->left = NULL;
new_node->rght = NULL;

return new_node;
}

The function to print the tree:

void printTree(struct Node *cur) {
    if (cur == 0) {
        printf("\n");
    }
    else {
        printf("(");
        printTree(cur->left);
        printf(" %s, %d ", cur->val->name, cur->val->number);
        printTree(cur->rght);
        printf(")\n");
    }
}

Here’s an example of some data that will overwrite previous elements:

struct BSTree myTree;
struct data myData1, myData2, myData3;

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

initBSTree(&myTree);
addBSTree(&myTree, &myData1);
addBSTree(&myTree, &myData2);
addBSTree(&myTree, &myData3);
    printTree(myTree.root);

Which will print:

((
 righty, 10 
)
 lefty, 1 
)

Finally here’s some test data that will go in the exact same spot as the previous data, but this time no data is overwritten:

struct BSTree myTree;
struct data myData1, myData2, myData3;

myData1.number = 5;
myData1.name = "i";
myData2.number = 5;
myData2.name = "h";
myData3.number = 5;
myData3.name = "j";

initBSTree(&myTree);
addBSTree(&myTree, &myData1);
addBSTree(&myTree, &myData2);
addBSTree(&myTree, &myData3);
printTree(myTree.root);

Which prints:

((
 j, 5 
)
 i, 5 (
 h, 5 
)
)

Does anyone know what might be going wrong? Sorry if this post was kind of long.

  • 1 1 Answer
  • 2 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-14T18:58:51+00:00Added an answer on May 14, 2026 at 6:58 pm

    it looks like there is an error in your _addNode procedure. It looks like you arent properly storing the new node references in the tree.

    struct Node* _addNode(struct Node* cur, TYPE val) {
    if(cur == NULL) {
        /* no root has been made */
        cur = _createNode(val);
        return cur;
    }
    else {
        int cmp;
    
        cmp = compare(cur->val, val);
        if(cmp == -1) {
            /* go left */
            cur->left = _addNode(cur->left, val);
        }
        else if(cmp >= 0) {
            /* go right */
            cur->left = _addNode(cur->right, val);
        }
    
        return cur;
    }
    

    the _addnode function was kind of confusing because you were using the return value inconsistently. i believe this version should avoid loosing any nodes.

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

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Please post your connection string or have a look at… May 15, 2026 at 6:24 am
  • Editorial Team
    Editorial Team added an answer I would say what you have is very simple, you… May 15, 2026 at 6:24 am
  • Editorial Team
    Editorial Team added an answer According to the getstatusoutput documentation, commands.getstatusoutput(cmd) is executed as {… May 15, 2026 at 6:24 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.