2

I tried to implement a binary search tree for the purpose of (re-)learning C. The problem is that the this current = new; does not work as desired because the tree.root is still a null pointer after adding two nodes. What's wrong with that?

#include <stdlib.h>
#include <stdio.h>


typedef struct BinaryNode {
    int key;
    double value;
    struct BinaryNode *left;
    struct BinaryNode *right;
    } BinaryNode;

typedef struct BinaryTree {
    struct BinaryNode *root;
    } BinaryTree;


static void binary_tree_insert_recursive(BinaryNode *current, BinaryNode *new) {
    if (current == NULL || current->key == new->key) {
        current = new;
    } else if (current->key > new->key) {
        binary_tree_insert_recursive(current->left, new);
    } else if (current->key < new->key) {
        binary_tree_insert_recursive(current->right, new);
    }
}

void binary_tree_insert(BinaryTree *tree, int key, double value) {
    BinaryNode *new = (BinaryNode *) malloc(sizeof(BinaryNode));
    new->key = key;
    new->value = value;
    binary_tree_insert_recursive(tree->root, new);
}

int main(void) {
    BinaryTree tree;
    binary_tree_insert(&tree, 5, 123);
    binary_tree_insert(&tree, 10, 123);
    printf("%p\n", tree.root);
    return 0;
}

Thank you!

4 Answers 4

1

I believe the problem with current = new; is that you are changing your local copy of current. After the function is done, this modification is not visible.

I suspect you want something like:

static void binary_tree_insert_recursive(BinaryNode **current, BinaryNode **new)
{
    if (*current == NULL || (*current)->key == (*new)->key) {
    *current = *new;
    /* ... */

Well explained in the C FAQ.

Sign up to request clarification or add additional context in comments.

3 Comments

@ahojnnes: You might need to consider also initializing the "left" and "right" pointers to NULL for every node you create. Also, don't name your variable "new", it's confusing.
@Andrei: is it not initialized with a null pointer by default?
@ahojnnes: They are not requited to be initialized to anything. Some compilers will initialize them to some "easy to see" values such as 0xcccccccc or 0xdeadbeef.
1

current is a pointer to a node. When you pass it to binary_tree_insert_recursive from binary_tree_insert the value of the pointer is passed. So although it is changed inside the called function, the change is not reflected in the calling function. You need to modify the function to take the address of the pointer you wish to change:

 static void binary_tree_insert_recursive(BinaryNode **current, BinaryNode *new)
 {
         if (*current == NULL || (*current)->key == new->key)  {
             *current = new; 

Comments

0

all that current = new does is make it so that the variable current points at the thing that new is pointing at. No copying takes place, and the function has no effect on that codepath.

Comments

0

new is a keyword. Choose a different variable name.

2 Comments

In C proper it's not. However, who knows what is the OP compiling it like. Didn't even specify the compiler, or the error message.
I thought this is C++? Nevertheless it does not solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.