f



t = (t->left != NULL) ? t->left : t->right;

hi i am learning C++ and when trying to implement a binary search tree
for the first time I came across:

t = (t->left != NULL) ? t->left : t->right;

in a remove function, I have never seen this syntax and was wondering
if anyonr could explain to me what it does.

thanks in advance
-joe


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
mailsurferjoe
5/9/2005 10:26:57 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

10 Replies
1310 Views

Similar Articles

[PageSpeed] 11

it is the same as
if ( t->left != NULL )
       t = t->left;
else
       t = t->right;


Best regards, Alexey.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
alexey
5/9/2005 10:17:56 PM
Hi there, the syntax:

a = b ? c : d;

is short hand for:

if(b)
    a = c;
else
    a = d;

Adam. 



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Adam
5/9/2005 10:23:35 PM
mailsurferjoe@gmail.com wrote:
> hi i am learning C++ and when trying to implement a binary search tree
> for the first time I came across:
>
> t = (t->left != NULL) ? t->left : t->right;
>
> in a remove function, I have never seen this syntax and was wondering
> if anyonr could explain to me what it does.

Semantically it's the same as

     if (t->left != NULL)
         t = t->left;
     else
         t = t->right;

The ternary operator (x?a:b) evaluates to 'a' if 'x' evaluates to 'true'
otherwise it evaluates to 'b'.

V

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Victor
5/10/2005 11:58:30 AM
<mailsurferjoe@gmail.com> wrote in message
news:1115615274.256579.107200@z14g2000cwz.googlegroups.com...
> hi i am learning C++ and when trying to implement a binary search tree
> for the first time I came across:
>
> t = (t->left != NULL) ? t->left : t->right;
>
> in a remove function, I have never seen this syntax and was wondering
> if anyonr could explain to me what it does.

The question-colon pair (?:) are collectively known as the "ternary
operator."  It is a sort of "shorthand" for an if-else operation.  The
syntax is:

(boolean expression ? value if true : value if false)

So, in your example above, the statement could be understood as:

if (t->left != NULL)
{
     t = t->left;
}
else
{
     t = t->right;
}

- Dennis



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Dennis
5/10/2005 12:06:59 PM
mailsurferjoe@gmail.com wrote:
> hi i am learning C++ and when trying to implement a binary search tree
> for the first time I came across:
>
> t = (t->left != NULL) ? t->left : t->right;
>
> in a remove function, I have never seen this syntax and was wondering
> if anyonr could explain to me what it does.

See
http://msdn.microsoft.com/library/en-us/vccelng/htm/express_54.asp&e=10167
    for a description of the ?: operator.  In short,

a ? b : c

is equivalent to

if( a ) b; else c;

but ?: evaluates to the value of b or c.

    -- MJF

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
M
5/10/2005 12:07:53 PM
> Joe wrote:
>
> t = (t->left != NULL) ? t->left : t->right;

It's not entirely clear since we can't see the whole program, but I'll
make some assumptions and continue. BTW, this code looks more like C
than C++ because of the use of "NULL," which some C++ gurus discourage
in favor of 0 (or even a simple class).

Anyway, there are three things going on:

1. Use of the ? : ternary operator, which makes that line shorthand for
this code:

if( t->left != NULL) {
   t = t->left;
} else {
   t = t->right;
}

2. The use of a pointer to a struct or class with the -> operator. The
struct might look something like this:

template <class T>
struct Elem {
   T*    data;
   Elem* left;
   Elem* right;
};

The -> operator works like the . operator (member access operator) but
on pointers to structs/classes (there's a bit more to it when it comes
to operator overloading, but let's keep it simple). Hence the following
two assignments do the same thing (viz., assign 0 to the "left" members
of two distinct Elem's):

Elem<int> elem;
Elem<int>* pElem = new Elem;
elem.left = 0;
pElem->left = 0;

3. The logic inherent to the design of your binary tree. The meaning of
the code is essentially this: 'The pointer "t" is pointing to some node
in our tree. Traverse down the left branch from the current node if
possible, or take the right branch if there isn't a left node.' This is
useful, for instance, when searching for an element in the tree.

Cheers!

M


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
mlimber
5/10/2005 12:09:32 PM
mailsurferjoe@gmail.com writes:

> hi i am learning C++ and when trying to implement a binary search
> tree for the first time I came across:
>
> t = (t->left != NULL) ? t->left : t->right;
>
> in a remove function, I have never seen this syntax and was
> wondering if anyonr could explain to me what it does.

Every textbook could.


The ?: operator has the following semantics:

a ? b : c

evaluates to the value of b if a evaluates to a non-zero value, and to
the value of c if a evaluates to a zero value (such as false or 0). If
the types of b and c are different, rather complicated rules are
applied to find out if the expression is valid and to determine its
type; if b and c are of the same type, a ? b : c is also of that type.


In your code, t is thus assigned t->left if that value is != NULL, and
t->right otherwise. In words, after the line t points to its leftmost
t->child if it has a child, and to 0 otherwise.


The line is equivalent to

if (t->left==NULL)
   t = t->right;
else
   t = t->left;

but has the advantage of being more concise.

More, a ?: expression can be used to initialize a const object or to
initialize a data member in the member-initialization list.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Thomas
5/10/2005 12:09:57 PM
In article <1115615274.256579.107200@z14g2000cwz.googlegroups.com>,
<mailsurferjoe@gmail.com> wrote:

> hi i am learning C++ and when trying to implement a binary search tree
> for the first time I came across:
>
> t = (t->left != NULL) ? t->left : t->right;
>
> in a remove function, I have never seen this syntax and was wondering
> if anyonr could explain to me what it does.
>
   its a conditional expression  equivalent to;
   if(t->left != NULL)
    t = t->left;
  else
    t = t->right;

   generally  a?b:c   's value is if a evaluates to true then the value
of the expression is b, otherwise the value is b.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Carl
5/10/2005 7:41:04 PM
In article <427f6084$0$39055$ed2e19e4@ptn-nntp-reader04.plus.net>, Adam 
Miller <adam@tkdkid.freeserve.co.uk> writes
>Hi there, the syntax:
>
>a = b ? c : d;
>
>is short hand for:
>
>if(b)
>    a = c;
>else
>    a = d;

Almost except that a = b ? c : d; is an expression statement and so has 
a value which could be used. The side effects of those two pieces of 
code are the same but the ways they can be used are different



-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Francis
5/10/2005 9:21:52 PM
One more thing, it works because the priority of the ternary operator ?:
is greater than =, so you should read
t =  ( (t->left != NULL) ? t->left : t->right);
and not
(t = (t->left != NULL)) ? t->left : t->right;
which obviously means nothing.

Ithier

mailsurferjoe@gmail.com wrote:
> hi i am learning C++ and when trying to implement a binary search tree
> for the first time I came across:
>
> t = (t->left != NULL) ? t->left : t->right;
>
> in a remove function, I have never seen this syntax and was wondering
> if anyonr could explain to me what it does.
>

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Ithier
5/11/2005 7:46:37 AM
Reply: