Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
There is no compiler error but the program does not run

What I have tried:

C++
#include <iostream>

using namespace std;
struct node
{
    int data;
    node*p_left;
    node*p_right;

};
node*insert(node*p_tree,int data)
{
    if(p_tree=NULL)
    {
        node*p_new_tree=new node;
        p_new_tree->p_left=NULL;
        p_new_tree->p_right=NULL;
        return p_new_tree;

    }

    if(data<p_tree->data)
    {
        insert(p_tree->p_left,data);
    }
    else if(data>p_tree->data)
    {
        insert(p_tree->p_right,data);
    }
}
int main()
{ node*p_temp;
    insert(p_temp,4);
    insert(p_temp,5);
    insert(p_temp,6);
    insert(p_temp,3);
    insert(p_temp,2);
    insert(p_temp,1);
    insert(p_temp,8);
    insert(p_temp,9);

}
Posted
Updated 11-Jun-16 23:11pm
v3
Comments
nv3 12-Jun-16 4:46am    
This is your fifth question and you still have not learnt to format your question properly. It's not that difficult. Just enclose your code with a <pre> ... </pre> tags, which you can also do by hitting the "code" button above the edit window.
Patrice T 12-Jun-16 4:46am    
Define "not run"
Philippe Mori 12-Jun-16 19:08pm    
Also, the problem in that case is not much different than the one in your previous question : What is wrong in my C++ code for linked list

Thus you are repeating the same errors again and again and again.

"does not run" is probably the single most useless error report you can give: it means nothing! It could mean "my program immediately ends", "my program sits there and does nothing", or even "my computer catches fire and burns down the building". We don't know.
So you have a problem, explain exactly what happens that you didn't expect, or didn't happen that you did! Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work from!

And basically, at best it's the first option: "my program immediately ends".
That's because that is exactly what you told it to do. You have told it to load a tree and then exit. It doesn't have to do anything after it loads the tree, because you haven't told it to. The whole of your main method is eight calls to your insert function and then end the program. So even if your insert function works - and I have no idea if it does - there is no simple way for you to tell.

Start by writing a "show tree" function which traverses the tree and prints it out. That way, you'll get an idea if your tree is being built correctly. If it is, all well and good. If it isn't - then you need to use the debugger to follow your code through and find out why it isn't correct! Getting it to compile is only the start, the easy bit. Now comes the fun part - getting it to do exactly what you want, rather than exactly what you told it to! And that's a skill - so just like all other skills, you only develop it by using it.
Give it a try: see what you can find out.
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Where are you storing data in the new node ?
How do you link a new node in previous node in the tree ?
 
Share this answer
 
In your insert function the data member is not assigned when the p_tree parameter is NULL:
if(p_tree=NULL)
{
    node*p_new_tree=new node;
    p_new_tree->p_left=NULL;
    p_new_tree->p_right=NULL;
    // This is missing:
    p_new_tree->data=data;
    return p_new_tree;
}

In the other cases no value is returned by the insert function. This should raise a compiler warning.

In your first call you are passing an unitialised parameter which should raise a compiler warning too:
node*p_temp;
insert(p_temp,4);

When p_temp is not NULL here, the program crashes (access violation). When it is NULL, a new node is allocated but nowhere stored to be later used (return value not used).

Overall you should rethink your implementation. It would not work this way.
 
Share this answer
 

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