#BST Find function output is supposed to be true when it finds the element but it false on 4th number

1 messages · Page 1 of 1 (latest)

slender osprey
#
bool BST::find(const T &query) const // test1
{

    Node *tl = root;
    Node *tr = root;

    // if the list is empty
    if (root == nullptr)
    {
        return false;
    }

    // looping to check all left childs
    for (int i = 0; i < size(); i++)
    {
        // if found, return true
        // if found at the root
        if (tl->data == query)
        {
            return true;
        }
        // if found at the left child
        else if (root->leftChild->data == query)
        {
            return true;
        }
        // if found at the right child
        else if (root->rightChild->data == query)
        {
            return true;
        }

        /*
        // if about to fall off tree
        else if ((root->data > query && root->leftChild == nullptr) || (root->data < query && root->rightChild == nullptr))
        {
            return true;
        }
        */

        // looking through left childs
        if (query < tl->data)
        {
            tl = tl->leftChild;

            // if element is greater than the right child
            if (query > tl->rightChild->data)
            {
                // if found, return true
                if (tl->rightChild->data == query)
                {
                    return true;
                }
            }
        }

        // looking through right childs
        if (query > tr->data)
        {
            tr = tr->rightChild;

            // if element is greater than the right child
            if (query < tr->leftChild->data)
            {
                // if found, return true
                if (tr->leftChild->data == query)
                {
                    return true;
                }
            }
        }
    }

    // return false if element isn't found
    return false;
}```
dawn mothBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

safe oar
#

you are doing too much and overcomplicating things in this function

#

it should be 1/2 its current size.

#

there should be only one return true statement

#

and one return false statement as well

slender osprey
#

i see the return false but how would I make it return true only once?

safe oar
#

check only one node at a time

slender osprey
#

i thought i did check one node at a time, which part isnt doing that?

safe oar
#

the fact that you have multiple runners (tr, tl), the fact that you check their children's datas

#

the entire function should be five lines

#

there should be a grand total of one runner

slender osprey
#

I don't see how it can be only 5 lines. how can you implement it while needing to check every node in the bst?

safe oar
#
bool BST::find(const T &query) const {
    Node *n = root;
    while (n != nullptr) {
        if (n->data == query) return true;
        n = (n->data > query) ? n->leftChild : n->rightChild;
    }
    return false;
}
safe oar
#

you need to check only the path to the node that would contain the query value, if it exists

#

there is only one such path

slender osprey
#

!solved