i got it success but error and i cant find it

my code here:

bool BinarySearchTree::search(int d)
{
    tree_node *t = head;
    if(root==NULL)

        cout<<" The Tree is empty. Please insert a value "<<endl;


    while (t != NULL)
    {

        if (t->data == d)
            break;

        else if (d > t->data)
            t = t->right;
        else                  
        //(d < t->data)
            t = t->left;
    }

    if (t == NULL)
        return false;

    if (t->data == d)
    cout << "found" <<endl;
        return true;

    return false;
}

Is there an error message? Does it compile? Does it crash? Does it not do what you think it does?

commented: no error but cant search for it. it crash +0

no error but cant search for it. it crash

Run it under a debugger. Then you will know which line it crashes on, and you can check the values of the variables to see what the problem is.

commented: how to run it. i was run it with "start without debugging" +0

What are you using to build and run your code? Are you using an IDE?

It may help to show your list structures as well. Also, this is very clumsy code and prone to logic errors. In addition, your lack of appropriate scoping (bracing) of your logic means that the return values are not what you think. Example, this code:

if (t->data == d)
    cout << "found" <<endl;
return true;

Will ALWAYS return true, even if it isn't. This is how it should be scoped:

if (t->data == d)
{
    cout << "found" <<endl;
    return true;
}

To me, line 25 until the end could be replaced with return true; because if and only if t->data is equal to d would get to that point (or the value would be null).

I believe the problem with the OP could be from inserting a node to the tree. The search looks fine to me...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.