0
        travptr = (data > travptr->data) ? travptr->right : travptr->left;

hi, this is a piece of code where I am not able to understand what the command does i am attacking the whole code below for examining the type of variable and data types used in the piece of command above

#include <iostream>
using namespace std;

struct Node
{
    int data;
    Node *left;
    Node *right;
};

Node *newNode(int key)
{
    Node *node = new Node;
    node->data = key;
    node->left = node->right = nullptr;

    return node;
}

void insert(Node **headref, int data)
{

    if (*headref == NULL)
    {
        *headref = newNode(data);
        return;
    }

    Node *travptr = *headref;
    Node *parentNode = NULL;

    while (travptr != NULL)
    {
        parentNode = travptr;
        travptr = (data > travptr->data) ? travptr->right : travptr->left;
    }

    if (data > parentNode->data)
        parentNode->right = newNode(data);

    else
        parentNode->left = newNode(data);
}

bool find(Node **headref, int data)
{
    Node *travptr = *headref;

    if (data == (*headref)->data)
        return true;

    while (travptr != NULL && travptr->data != data)
        travptr = (data > travptr->data) ? travptr->right : travptr->left;

    if (travptr == NULL)
    {
        return false;
    }
    return true;
}

int main()
{

    Node *head = NULL;
    insert(&head, 1);
    insert(&head, 2);
    insert(&head, 3);
    insert(&head, 4);
    cout << find(&head, 3) << "\t" << find(&head, 10);
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

1 Answers1

-2

There is used the conditional operator also sometimes called as ternary operator.

travptr = (data > travptr->data) ? travptr->right : travptr->left;

It is equivalent to the following if-else statement

if ( data > travptr->data )
{
    travptr = travptr->right;
}
else
{
    travptr = travptr->left;
}

Pay attention to that the function find can invoke undefined behavior because there is no check whether the pointer to the root node is a null pointer.

bool find(Node **headref, int data)
{
    Node *travptr = *headref;

    if (data == (*headref)->data)
                ^^^^^^^^^^^^^^^^^ 
        return true;

And this if statement

if (travptr == NULL)
{
    return false;
}
return true;

is better to rewrite like

return  travptr != nullptr;

Also it is unnecessary to pass the pointer to the root node by reference in C meaning because the pointer itself is not being changed. You could declare the function like

bool find( const Node *headref, int data);

Correspondingly the function insert should be declared like

void insert(Node * &headref, int data);

The function newNode can be defined simpler

Node *newNode(int key)
{
    return new Node { key, nullptr, nullptr };
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Didn't you think this is a duplicate? – trincot Feb 02 '22 at 16:47
  • So? You are putting into question the whole idea of closing a question because it is duplicate? Should we tell Stack Overflow to abandon this closure reason? Or should we just close as duplicate (hint)? – trincot Feb 02 '22 at 16:49