1

I am reading the C++ code of an AVL tree, and I found the following code from Geeks for Geeks (https://www.geeksforgeeks.org/avl-tree-set-2-deletion/):

Node *temp = root->left ? root->left : root->right;

I know that a > b ? c : d means if a > b, then c. Otherwise, d but I'm not sure what the above means. Is it treated as Boolean such as if root->left true, then root->left. Otherwise, root->right?

gsk
  • 107
  • 11
  • Yes, temp will be root->left if root->left is not null – Mark PM Mar 31 '20 at 21:11
  • 1
    Correct, or in this case it's essentially checking for the existence of the node. You could think of it like your Boolean being `root->left != nullptr`. – Rogue Mar 31 '20 at 21:11
  • Thank you all for the detailed comments. It helps me a lot to learn. – gsk Mar 31 '20 at 21:20
  • almost everything can be a condition in C++, if it can have the value of an integer, it can act as a condition. If the value is 0 or less, it is false, if it's 1 or greater, it is true. I believe the code `root->left` will assume the value 0 if it is `NULL` or `nullptr` and other positive value (that would be the memory address) otherwise. – Daniel Mar 31 '20 at 21:22

0 Answers0