-1

Notice the //, the program stops taking input and that is the main problem.
root->cand = data;
Here is the full code:

#include <bits/stdc++.h>
 using namespace std;
 struct node
{
    int cand;
    node *left;
    node *right;
};
class candies
{
    node *root;

public:
    candies();
    int add(int);
    int check();
};
candies::candies()
{
    root = NULL;
}
int candies::add(int data)
{
    if (root == NULL)
    {
        root->cand = data; //code stops here
        root->left = NULL;
        root->right = NULL;
    }
    else
    {
        node *temp = root;
        while (temp != NULL)
        {
            if (data < temp->cand)
            {
                temp = temp->left;
            }
            else
            {
                temp = temp->right;
            }
        }
        temp = new node;
        temp->cand = data;
        temp->left = temp->right = NULL;
    }
    return 1;
}
int candies::check()
{
    node *temp;
    temp = root;
    int data;
    cin >> data;
    while (temp != NULL)
    {
        if (temp->cand == data)
        {
            cout << "YES\n";
            return 1;
        }
        else if (data < temp->cand)
            temp = temp->left;
        else if (data > temp->cand)
            temp = temp->right;
    }
    cout << "NO\n";
    return 0;
}
int main()
{
    candies c;
    int n;
    cin >> n;
    while (n--)
    {
        int data;
        cin >> data;
        c.add(data);
    }
    c.check();
}
hacxter
  • 11
  • 2

2 Answers2

1

The member function add is invalid and moreover has undefined behavior.

In this if statement

if (root == NULL)
{
    root->cand = data; //code stops here
    root->left = NULL;
    root->right = NULL;
}

there is used a null-pointer to access memory.

In this else statement

else
{
    node *temp = root;
    while (temp != NULL)
    {
        if (data < temp->cand)
        {
            temp = temp->left;
        }
        else
        {
            temp = temp->right;
        }
    }
    temp = new node;
    temp->cand = data;
    temp->left = temp->right = NULL;
}

the created node temp is not added to the tree. So the program has a memory leak.

The function can be written the following way. It is better to make its return type void. Otherwise the returned value 1 as in your function implementation does not make sense.

class candies
{
    node *root;

public:
    candies();
    void add(int);
    int check();
};

//...

void candies::add( int data )
{
    node **current = &root;

    while ( *current != nullptr )
    {
        if ( data < ( *current )->cand )
        {
            current = &( *current )->left;
        }
        else
        {
            current = &( *current )->right;
        }
    }

    *current = new node { data, nullptr, nullptr };
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You are first checking that root is NULL. If it is, you are changing its data. This is not good at all, and will cause a crash.

if (root == NULL)
{
    root->cand = data; //code stops here

If root is NULL, you must create a root node first.

if ( root == nullptr ) {
    root = new node;
    root->cand = data; 
ChrisMM
  • 8,448
  • 13
  • 29
  • 48