Click here to Skip to main content
15,392,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there. For some reason, when I try and insert a node onto an empty BST tree in C, the pointer wont update and keeps pointing to NULL. Is there anyway I can fix this. Much appreciated.

What I have tried:

#include<stdio.h>
    #include<stdlib.h>
    #include<assert.h>
    
    typedef struct _Node{
        int data;
        struct _Node *left, *right;
    }Node;
    
    Node *insertNode(Node *root, int value){
        if(root == NULL){
            Node *temp = (Node*)malloc(sizeof(Node));
            temp->data = value;
            temp->left = temp->right = NULL;
            return temp;
        }
        if(root->data < value){
            root->right = insertNode(root->right, value);
        }
        else if(root->data > value){
            root->left = insertNode(root->left, value);
        }
        else{
            
        }
        return root;
    }  
    
    void printSubtree(Node *N){
        if(N == NULL){
            return;
        }
        else if(N != NULL){
            printSubtree(N->left);
            printf("%d \n", N->data);
            printSubtree(N->right);
        }
    }
    
    int main(){
        Node *a;
    
        a = insertNode(NULL, 30);
        printf("%d", *a);
        a = insertNode(a, 50);
        printf("%d", *a);
    }
Posted
Updated 15-Oct-21 9:19am

How do you know that? If you are relying on the printf statement to tell you need to think again. If I were you I would make a display function to do this :
C++
void printNode( Node * pnode )
{
   if( pnode )
   {
       printf( "node at %p : left=%p, right=%p, data=%d\n",
           pnode, pnode->left, pnode->right, pnode->data );
       if( pnode->left )
           printNode( pnode->left );
       if( pnode->right )
           printNode( pnode->right );
   }
   else
       printf( "node is null\n" );
and change your code to look like this :
C++
int main()
{
    Node * pn = NULL;
    printNode( pn );
    pn = insertNode( pn, 1 );
    printNode( pn );
    pn = InsertNode( pn, 2 );
    printNode( pn );
}
If your compiler does not like the %p specifier then use %08X instead.
   
v3
Comments
[no name] 15-Oct-21 14:22pm
   
When utilising this function, with the input of:
a = insertNode(NULL, 30)
printNode(a)
a = insertNode(a, 50)
printNode(a)
The output is:
node is at 0x55eb43ce12a0 - data is 30
node is at 0x55eb43ce12a0 - data is 30

Running this surely if the first run of the main call says that the data is 30, then the root pointer is now pointing to 30, therefore when the insertNode function is called again it should be parsed as:
insertNode(pointer to 30, 50) and therefore when running the insertNode, root != NULL and therefore run the main code and return the tree with the newly inserted node 50 with a pointing to this value
Rick York 15-Oct-21 15:40pm
   
OK, change it to print the entire tree then. I will revise the solution to have a modified printNode function. This should help you see what's going on a little better but the debugger is usually the best way to do this.
[no name] 15-Oct-21 15:52pm
   
When I print the entire tree it prints the values 30 and 50, but I have recently changed it to have two pointers:
Node *a, *b;
a = insertNode(NULL, 1)
b = insertNode(a, 2)

However the b pointer just points to the root node - I believe this is an issue in the insertNode function in what I return. I want to return the newly added leaf node not the original root node, but I am unsure of how to do this. Once I return the right thing, the entire program should work
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
   
Comments
[no name] 15-Oct-21 14:28pm
   
I would assume that when the insertNode function is called in main, it should see that the root is NULL, and therefore allocate some memory to a pointer and assign the value of this pointer to the value that is wanting to be inserted and then return this pointer. Therefore the next time the insertNode(a, ..) function is called, it should assume that the pointer is pointing to the newly added node
and then from that go to the next statements and see if the new value to be added is larger or smaller than the root
OriginalGriff 15-Oct-21 14:59pm
   
Don't assume; use the debugger and check exactly what *is* happening.

Assumptions about faulty code don't help: facts and information do!
[no name] 15-Oct-21 15:00pm
   
When using the debugger, this is the result:

Starting program: /home/a.out
node is at 0x5555555592a0 - data is 30
node is at 0x5555555592a0 - data is 30
[Inferior 1 (process 1965) exited normally]
(gdb)

I am new to coding so dont really know what this means
OriginalGriff 15-Oct-21 15:04pm
   
That's not "using the debugger" - that's printing some bits out.
Google for your compiler system (or IDE) and "Debugger" and it will explain how to use it.
It lets you look at your code and data while it is running and follow exactly what is happening and work out why.
[no name] 15-Oct-21 15:04pm
   
I am using an online compiler at the moment and this is the only feature it comes with
OriginalGriff 15-Oct-21 15:08pm
   
Which one? even GDB includes a debugger: https://www.onlinegdb.com/
[no name] 15-Oct-21 15:08pm
   
Im using that one - when I go into debug and press start thats all that is returned
OriginalGriff 15-Oct-21 15:13pm
   
Then start here: http://www.gdbtutorial.com/tutorial/how-use-gdb
Quote:
Why does pointer not update on empty BST tree in C when inserting node?

The to learn the debugger.
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900