-2

The problem is that I cannot return a false value in the binary tree.

#include <iostream>
#include <string>

using namespace std;

struct Node
{
    int Identification;
    string full_name;
    Node *left;
    Node *right;
};

Node *newNode(int id, string name)
{
    Node *temp = new Node;
    temp->Identification = id;
    temp->full_name = name;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

Node* insert(Node* node, int id, string name)
{
    if ( node == NULL)
    {
        return newNode(id,name);
    }
    
    if(id < node->Identification)
    {
        node->left = insert(node->left, id, name);
    }
    else if (id > node->Identification)
    {
        node->right = insert(node->right, id, name);
    }
    
    return node;
}

bool search(Node* root, int id)
{
    if (root == NULL || root->Identification == id)
    {
        cout << root->full_name << endl;
        return true;
    }
    else if (root != NULL && root->Identification != id)
    {
        cout << "Record not found";
        return false;
    }
    
    
    if (root->Identification < id)
    {
        return search(root->right, id);
    }
    return search(root->left, id);
    
}


int main()
{
    int searching;
    Node *root = NULL;

    root = insert(root, 1021, "John Williams");
    root = insert(root, 1057, "Bill Witherspoon");
    root = insert(root, 2487, "Jennifer Twain");
    root = insert(root, 3769, "Sophia Lancaster");
    root = insert(root, 1017, "Debbie Reece");
    root = insert(root, 1275, "George McMullen");
    root = insert(root, 1899, "Ashley Smith");
    root = insert(root, 4218, "Josh Plemmons");
  
  cout << "Enter ID to find employe name ";
  cin >> searching;
  search(root, searching);

    return 0;
}

It works when I types like 444 and returns false, No Id found. And when I type 1021, it return true, John Williams. But when I use other IDs like for example 1899, which correspond with Ashley smith, it returns false. So I do not know what is the problem.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 2
    DANGER: In `if (root == NULL || root->Identification == id)` if `root` is null, `cout << root->full_name << endl;` has an ill doom. You can't get the `full_name` of a null pointer. – user4581301 Apr 10 '23 at 04:04
  • 1
    We can't help you debug this unless you edit your question to include the source code for the `insert` function. Also, by the way, your `search` function contains undefined behavior: if `root == NULL` then it dereferences `root->full_name`. – rob mayoff Apr 10 '23 at 04:05
  • `search` only ever looks at the root node. The recursive call to `search` is unreachable, the function always returns before that. – Igor Tandetnik Apr 10 '23 at 04:18
  • *"I cannot return the false statement in the binary tree."* -- Right, you would return a false **value**, not a statement. (The statement does the returning; it is not the thing returned.) I could fix that much for you, but I'm stuck on the second part. Returning in a data structure (e.g. a binary tree) does not make sense; one returns from a function. Which function is not returning what you intend? – JaMiT Apr 10 '23 at 06:45
  • *"It works when I types [...]"* -- A better [mre] would not require user input. Instead of asking for an ID and getting the ID from the user, hardcode the two searches. For example: `std::cout << "Search for 444: " << search(root, 444) << '\n';` `std::cout << "Search for 1021: " << search(root, 1021) << '\n';` – JaMiT Apr 10 '23 at 06:49
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Apr 10 '23 at 23:33

1 Answers1

2

In the function search, the code

if (root == NULL || root->Identification == id)
{
    cout << root->full_name << endl;
    return true;
}

does not make sense, because if the content of the if block is executed due to root == NULL, then you will dereference this NULL pointer by using the expression root->full_name, which will invoke undefined behavior (i.e. likely cause a segmentation fault).

Also, in that function, the following code is unreachable:

if (root->Identification < id)
{
    return search(root->right, id);
}
return search(root->left, id);

It is unreachable because the conditions of both preceding if statements would have to evaluate false for this code to be reached, but that is not possible. It is not possible for the conditions

if (root == NULL || root->Identification == id)

and

if (root != NULL && root->Identification != id)

to both evaluate to false. The second one will always be true if the first one is false.

The search function should look like this:

bool search(Node* root, int id)
{
    if ( root == NULL )
    {
        return false;
    }

    if ( id < root->Identification )
    {
        return search( root->left, id );
    }

    if ( id > root->Identification )
    {
        return search( root->right, id );
    }

    return true;
}

Now that function will return true also when you enter 1899.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39