Cleaner way to handle double pointer in C++ BST?












3














I have an implementation for my first binary search tree in C++. I was wondering if there was some cleaner way to avoid using the double pointer in the way I have my code setup? Such as on one line I have:



(*node)->left = insert(&((*node)->left),value);


Which seems a bit "messy", but it almost seems necessary for the way I have implemented the BST. Maybe I am possibly missing a way I can change the syntax slightly to achieve the same result? I understand that I can have a double pointer as a parameter for my functions, but I have been told that it is not the standard in C++. I have my code posted below, along with how I am testing it.I am trying to prepare for technical interviews so any feedback is welcome.



#include<stdio.h> 
#include<stdlib.h>
#include<iostream>
struct Node
{
int data;
Node *left, *right;
};

// A utility function to create a new BST node
Node* newNode(int data)
{
Node *temp = new Node();
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST
void inorder(Node **root)
{
if (*root != NULL)
{
inorder(&((*root)->left));
printf("%d n", (*root)->data);
inorder(&((*root)->right));
}
}

/* A utility function to insert a new node with given key in BST */
Node* insert(Node** node, int value)
{
if(*node==NULL){
return newNode(value);
}
if((*node)->data > value){
(*node)->left = insert(&((*node)->left),value);
}
else if((*node)->data < value){
(*node)->right = insert(&((*node)->right),value);
}
return *node;
}

// Driver Program to test above functions
int main()
{
/* Let us create following BST
50
/
30 70
/ /
20 40 60 80 */
Node *root = NULL;
root = insert(&root, 50);
insert(&root, 30);
insert(&root, 20);
insert(&root, 40);
insert(&root, 70);
insert(&root, 60);
insert(&root, 80);

// print inoder traversal of the BST
inorder(&root);

return 0;
}


EDIT:
By changing " ** " in the parameters of the function to "*&" was able to make code much easier to read, with the same functionality.



#include<stdio.h> 
#include<stdlib.h>
#include<iostream>
struct Node
{
int data;
Node *left, *right;
};

// A utility function to create a new BST node
Node* newNode(int data)
{
Node *temp = new Node();
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST
void inorder(Node *&root)
{
if (root != NULL)
{
inorder(((root)->left));
printf("%d n", (root)->data);
inorder(((root)->right));
}
}

/* A utility function to insert a new node with given key in BST */
Node* insert(Node*& node, int value)
{
if(node==NULL){
return newNode(value);
}
if((node)->data > value){
node->left = insert(((node)->left),value);
}
else if((node)->data < value){
(node)->right = insert(((node)->right),value);
}
return node;
}

// Driver Program to test above functions
int main()
{
/* following BST
50
/
30 70
/ /
20 40 60 80 */
Node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

// print inoder traversal of the BST
inorder(root);

return 0;
}









share|improve this question









New contributor




Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    3














    I have an implementation for my first binary search tree in C++. I was wondering if there was some cleaner way to avoid using the double pointer in the way I have my code setup? Such as on one line I have:



    (*node)->left = insert(&((*node)->left),value);


    Which seems a bit "messy", but it almost seems necessary for the way I have implemented the BST. Maybe I am possibly missing a way I can change the syntax slightly to achieve the same result? I understand that I can have a double pointer as a parameter for my functions, but I have been told that it is not the standard in C++. I have my code posted below, along with how I am testing it.I am trying to prepare for technical interviews so any feedback is welcome.



    #include<stdio.h> 
    #include<stdlib.h>
    #include<iostream>
    struct Node
    {
    int data;
    Node *left, *right;
    };

    // A utility function to create a new BST node
    Node* newNode(int data)
    {
    Node *temp = new Node();
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
    }

    // A utility function to do inorder traversal of BST
    void inorder(Node **root)
    {
    if (*root != NULL)
    {
    inorder(&((*root)->left));
    printf("%d n", (*root)->data);
    inorder(&((*root)->right));
    }
    }

    /* A utility function to insert a new node with given key in BST */
    Node* insert(Node** node, int value)
    {
    if(*node==NULL){
    return newNode(value);
    }
    if((*node)->data > value){
    (*node)->left = insert(&((*node)->left),value);
    }
    else if((*node)->data < value){
    (*node)->right = insert(&((*node)->right),value);
    }
    return *node;
    }

    // Driver Program to test above functions
    int main()
    {
    /* Let us create following BST
    50
    /
    30 70
    / /
    20 40 60 80 */
    Node *root = NULL;
    root = insert(&root, 50);
    insert(&root, 30);
    insert(&root, 20);
    insert(&root, 40);
    insert(&root, 70);
    insert(&root, 60);
    insert(&root, 80);

    // print inoder traversal of the BST
    inorder(&root);

    return 0;
    }


    EDIT:
    By changing " ** " in the parameters of the function to "*&" was able to make code much easier to read, with the same functionality.



    #include<stdio.h> 
    #include<stdlib.h>
    #include<iostream>
    struct Node
    {
    int data;
    Node *left, *right;
    };

    // A utility function to create a new BST node
    Node* newNode(int data)
    {
    Node *temp = new Node();
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
    }

    // A utility function to do inorder traversal of BST
    void inorder(Node *&root)
    {
    if (root != NULL)
    {
    inorder(((root)->left));
    printf("%d n", (root)->data);
    inorder(((root)->right));
    }
    }

    /* A utility function to insert a new node with given key in BST */
    Node* insert(Node*& node, int value)
    {
    if(node==NULL){
    return newNode(value);
    }
    if((node)->data > value){
    node->left = insert(((node)->left),value);
    }
    else if((node)->data < value){
    (node)->right = insert(((node)->right),value);
    }
    return node;
    }

    // Driver Program to test above functions
    int main()
    {
    /* following BST
    50
    /
    30 70
    / /
    20 40 60 80 */
    Node *root = NULL;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);

    // print inoder traversal of the BST
    inorder(root);

    return 0;
    }









    share|improve this question









    New contributor




    Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      3












      3








      3







      I have an implementation for my first binary search tree in C++. I was wondering if there was some cleaner way to avoid using the double pointer in the way I have my code setup? Such as on one line I have:



      (*node)->left = insert(&((*node)->left),value);


      Which seems a bit "messy", but it almost seems necessary for the way I have implemented the BST. Maybe I am possibly missing a way I can change the syntax slightly to achieve the same result? I understand that I can have a double pointer as a parameter for my functions, but I have been told that it is not the standard in C++. I have my code posted below, along with how I am testing it.I am trying to prepare for technical interviews so any feedback is welcome.



      #include<stdio.h> 
      #include<stdlib.h>
      #include<iostream>
      struct Node
      {
      int data;
      Node *left, *right;
      };

      // A utility function to create a new BST node
      Node* newNode(int data)
      {
      Node *temp = new Node();
      temp->data = data;
      temp->left = NULL;
      temp->right = NULL;
      return temp;
      }

      // A utility function to do inorder traversal of BST
      void inorder(Node **root)
      {
      if (*root != NULL)
      {
      inorder(&((*root)->left));
      printf("%d n", (*root)->data);
      inorder(&((*root)->right));
      }
      }

      /* A utility function to insert a new node with given key in BST */
      Node* insert(Node** node, int value)
      {
      if(*node==NULL){
      return newNode(value);
      }
      if((*node)->data > value){
      (*node)->left = insert(&((*node)->left),value);
      }
      else if((*node)->data < value){
      (*node)->right = insert(&((*node)->right),value);
      }
      return *node;
      }

      // Driver Program to test above functions
      int main()
      {
      /* Let us create following BST
      50
      /
      30 70
      / /
      20 40 60 80 */
      Node *root = NULL;
      root = insert(&root, 50);
      insert(&root, 30);
      insert(&root, 20);
      insert(&root, 40);
      insert(&root, 70);
      insert(&root, 60);
      insert(&root, 80);

      // print inoder traversal of the BST
      inorder(&root);

      return 0;
      }


      EDIT:
      By changing " ** " in the parameters of the function to "*&" was able to make code much easier to read, with the same functionality.



      #include<stdio.h> 
      #include<stdlib.h>
      #include<iostream>
      struct Node
      {
      int data;
      Node *left, *right;
      };

      // A utility function to create a new BST node
      Node* newNode(int data)
      {
      Node *temp = new Node();
      temp->data = data;
      temp->left = NULL;
      temp->right = NULL;
      return temp;
      }

      // A utility function to do inorder traversal of BST
      void inorder(Node *&root)
      {
      if (root != NULL)
      {
      inorder(((root)->left));
      printf("%d n", (root)->data);
      inorder(((root)->right));
      }
      }

      /* A utility function to insert a new node with given key in BST */
      Node* insert(Node*& node, int value)
      {
      if(node==NULL){
      return newNode(value);
      }
      if((node)->data > value){
      node->left = insert(((node)->left),value);
      }
      else if((node)->data < value){
      (node)->right = insert(((node)->right),value);
      }
      return node;
      }

      // Driver Program to test above functions
      int main()
      {
      /* following BST
      50
      /
      30 70
      / /
      20 40 60 80 */
      Node *root = NULL;
      root = insert(root, 50);
      insert(root, 30);
      insert(root, 20);
      insert(root, 40);
      insert(root, 70);
      insert(root, 60);
      insert(root, 80);

      // print inoder traversal of the BST
      inorder(root);

      return 0;
      }









      share|improve this question









      New contributor




      Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have an implementation for my first binary search tree in C++. I was wondering if there was some cleaner way to avoid using the double pointer in the way I have my code setup? Such as on one line I have:



      (*node)->left = insert(&((*node)->left),value);


      Which seems a bit "messy", but it almost seems necessary for the way I have implemented the BST. Maybe I am possibly missing a way I can change the syntax slightly to achieve the same result? I understand that I can have a double pointer as a parameter for my functions, but I have been told that it is not the standard in C++. I have my code posted below, along with how I am testing it.I am trying to prepare for technical interviews so any feedback is welcome.



      #include<stdio.h> 
      #include<stdlib.h>
      #include<iostream>
      struct Node
      {
      int data;
      Node *left, *right;
      };

      // A utility function to create a new BST node
      Node* newNode(int data)
      {
      Node *temp = new Node();
      temp->data = data;
      temp->left = NULL;
      temp->right = NULL;
      return temp;
      }

      // A utility function to do inorder traversal of BST
      void inorder(Node **root)
      {
      if (*root != NULL)
      {
      inorder(&((*root)->left));
      printf("%d n", (*root)->data);
      inorder(&((*root)->right));
      }
      }

      /* A utility function to insert a new node with given key in BST */
      Node* insert(Node** node, int value)
      {
      if(*node==NULL){
      return newNode(value);
      }
      if((*node)->data > value){
      (*node)->left = insert(&((*node)->left),value);
      }
      else if((*node)->data < value){
      (*node)->right = insert(&((*node)->right),value);
      }
      return *node;
      }

      // Driver Program to test above functions
      int main()
      {
      /* Let us create following BST
      50
      /
      30 70
      / /
      20 40 60 80 */
      Node *root = NULL;
      root = insert(&root, 50);
      insert(&root, 30);
      insert(&root, 20);
      insert(&root, 40);
      insert(&root, 70);
      insert(&root, 60);
      insert(&root, 80);

      // print inoder traversal of the BST
      inorder(&root);

      return 0;
      }


      EDIT:
      By changing " ** " in the parameters of the function to "*&" was able to make code much easier to read, with the same functionality.



      #include<stdio.h> 
      #include<stdlib.h>
      #include<iostream>
      struct Node
      {
      int data;
      Node *left, *right;
      };

      // A utility function to create a new BST node
      Node* newNode(int data)
      {
      Node *temp = new Node();
      temp->data = data;
      temp->left = NULL;
      temp->right = NULL;
      return temp;
      }

      // A utility function to do inorder traversal of BST
      void inorder(Node *&root)
      {
      if (root != NULL)
      {
      inorder(((root)->left));
      printf("%d n", (root)->data);
      inorder(((root)->right));
      }
      }

      /* A utility function to insert a new node with given key in BST */
      Node* insert(Node*& node, int value)
      {
      if(node==NULL){
      return newNode(value);
      }
      if((node)->data > value){
      node->left = insert(((node)->left),value);
      }
      else if((node)->data < value){
      (node)->right = insert(((node)->right),value);
      }
      return node;
      }

      // Driver Program to test above functions
      int main()
      {
      /* following BST
      50
      /
      30 70
      / /
      20 40 60 80 */
      Node *root = NULL;
      root = insert(root, 50);
      insert(root, 30);
      insert(root, 20);
      insert(root, 40);
      insert(root, 70);
      insert(root, 60);
      insert(root, 80);

      // print inoder traversal of the BST
      inorder(root);

      return 0;
      }






      c++ algorithm binary-search






      share|improve this question









      New contributor




      Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 3 hours ago







      Pulse













      New contributor




      Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      PulsePulse

      1184




      1184




      New contributor




      Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Pulse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          4














          If you're trying to learn C++, you should get comfortable with constructors and destructors — they're what C++ is all about!



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

          // A utility function to create a new BST node
          Node* newNode(int data)
          {
          Node *temp = new Node();
          temp->data = data;
          temp->left = NULL;
          temp->right = NULL;
          return temp;
          }


          That's C style. C++ style would be:



          struct Node { 
          int data_;
          Node *left_ = nullptr;
          Node *right_ = nullptr;

          explicit Node(int data) : data_(data) {}
          };


          Then when you want a new heap-allocated node, you don't call newNode(42) — you call new Node(42)! Or, a good habit you should get into: call std::make_unique<Node>(42) to get back a smart pointer.



          Notice that I added sigils to your data members (data_ etc) to distinguish them from non-member variables; and I declared no more than one variable per line to reduce reader confusion.





          void inorder(Node *&root) 
          {
          if (root != NULL)
          {
          inorder(((root)->left));
          printf("%d n", (root)->data);
          inorder(((root)->right));
          }
          }


          Several things weird here. First, you have a bunch of unnecessary parentheses. (root) is the same thing as root. Second, you're passing root by non-const reference, even though you don't intend to modify it. Third, very minor nit, you're using C-style NULL instead of nullptr. Fourth, why do you print a space before the newline? Fixed up:



          void inorder(const Node *root)
          {
          if (root != nullptr) {
          inorder(root->left);
          printf("%dn", root->data);
          inorder(root->right);
          }
          }


          Remember to remove the redundant parentheses in places like insert(((node)->right),value). It's much easier to read as insert(node->right, value).






          share|improve this answer





















          • Yeah the unnecessary parentheses came from when I was editing from the previous solution. Also, surprisingly was not aware of putting a constructor / destructor into the struct, thanks for pointing that out to me. Much needed feedback.
            – Pulse
            51 mins ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Pulse is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211072%2fcleaner-way-to-handle-double-pointer-in-c-bst%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          If you're trying to learn C++, you should get comfortable with constructors and destructors — they're what C++ is all about!



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

          // A utility function to create a new BST node
          Node* newNode(int data)
          {
          Node *temp = new Node();
          temp->data = data;
          temp->left = NULL;
          temp->right = NULL;
          return temp;
          }


          That's C style. C++ style would be:



          struct Node { 
          int data_;
          Node *left_ = nullptr;
          Node *right_ = nullptr;

          explicit Node(int data) : data_(data) {}
          };


          Then when you want a new heap-allocated node, you don't call newNode(42) — you call new Node(42)! Or, a good habit you should get into: call std::make_unique<Node>(42) to get back a smart pointer.



          Notice that I added sigils to your data members (data_ etc) to distinguish them from non-member variables; and I declared no more than one variable per line to reduce reader confusion.





          void inorder(Node *&root) 
          {
          if (root != NULL)
          {
          inorder(((root)->left));
          printf("%d n", (root)->data);
          inorder(((root)->right));
          }
          }


          Several things weird here. First, you have a bunch of unnecessary parentheses. (root) is the same thing as root. Second, you're passing root by non-const reference, even though you don't intend to modify it. Third, very minor nit, you're using C-style NULL instead of nullptr. Fourth, why do you print a space before the newline? Fixed up:



          void inorder(const Node *root)
          {
          if (root != nullptr) {
          inorder(root->left);
          printf("%dn", root->data);
          inorder(root->right);
          }
          }


          Remember to remove the redundant parentheses in places like insert(((node)->right),value). It's much easier to read as insert(node->right, value).






          share|improve this answer





















          • Yeah the unnecessary parentheses came from when I was editing from the previous solution. Also, surprisingly was not aware of putting a constructor / destructor into the struct, thanks for pointing that out to me. Much needed feedback.
            – Pulse
            51 mins ago
















          4














          If you're trying to learn C++, you should get comfortable with constructors and destructors — they're what C++ is all about!



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

          // A utility function to create a new BST node
          Node* newNode(int data)
          {
          Node *temp = new Node();
          temp->data = data;
          temp->left = NULL;
          temp->right = NULL;
          return temp;
          }


          That's C style. C++ style would be:



          struct Node { 
          int data_;
          Node *left_ = nullptr;
          Node *right_ = nullptr;

          explicit Node(int data) : data_(data) {}
          };


          Then when you want a new heap-allocated node, you don't call newNode(42) — you call new Node(42)! Or, a good habit you should get into: call std::make_unique<Node>(42) to get back a smart pointer.



          Notice that I added sigils to your data members (data_ etc) to distinguish them from non-member variables; and I declared no more than one variable per line to reduce reader confusion.





          void inorder(Node *&root) 
          {
          if (root != NULL)
          {
          inorder(((root)->left));
          printf("%d n", (root)->data);
          inorder(((root)->right));
          }
          }


          Several things weird here. First, you have a bunch of unnecessary parentheses. (root) is the same thing as root. Second, you're passing root by non-const reference, even though you don't intend to modify it. Third, very minor nit, you're using C-style NULL instead of nullptr. Fourth, why do you print a space before the newline? Fixed up:



          void inorder(const Node *root)
          {
          if (root != nullptr) {
          inorder(root->left);
          printf("%dn", root->data);
          inorder(root->right);
          }
          }


          Remember to remove the redundant parentheses in places like insert(((node)->right),value). It's much easier to read as insert(node->right, value).






          share|improve this answer





















          • Yeah the unnecessary parentheses came from when I was editing from the previous solution. Also, surprisingly was not aware of putting a constructor / destructor into the struct, thanks for pointing that out to me. Much needed feedback.
            – Pulse
            51 mins ago














          4












          4








          4






          If you're trying to learn C++, you should get comfortable with constructors and destructors — they're what C++ is all about!



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

          // A utility function to create a new BST node
          Node* newNode(int data)
          {
          Node *temp = new Node();
          temp->data = data;
          temp->left = NULL;
          temp->right = NULL;
          return temp;
          }


          That's C style. C++ style would be:



          struct Node { 
          int data_;
          Node *left_ = nullptr;
          Node *right_ = nullptr;

          explicit Node(int data) : data_(data) {}
          };


          Then when you want a new heap-allocated node, you don't call newNode(42) — you call new Node(42)! Or, a good habit you should get into: call std::make_unique<Node>(42) to get back a smart pointer.



          Notice that I added sigils to your data members (data_ etc) to distinguish them from non-member variables; and I declared no more than one variable per line to reduce reader confusion.





          void inorder(Node *&root) 
          {
          if (root != NULL)
          {
          inorder(((root)->left));
          printf("%d n", (root)->data);
          inorder(((root)->right));
          }
          }


          Several things weird here. First, you have a bunch of unnecessary parentheses. (root) is the same thing as root. Second, you're passing root by non-const reference, even though you don't intend to modify it. Third, very minor nit, you're using C-style NULL instead of nullptr. Fourth, why do you print a space before the newline? Fixed up:



          void inorder(const Node *root)
          {
          if (root != nullptr) {
          inorder(root->left);
          printf("%dn", root->data);
          inorder(root->right);
          }
          }


          Remember to remove the redundant parentheses in places like insert(((node)->right),value). It's much easier to read as insert(node->right, value).






          share|improve this answer












          If you're trying to learn C++, you should get comfortable with constructors and destructors — they're what C++ is all about!



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

          // A utility function to create a new BST node
          Node* newNode(int data)
          {
          Node *temp = new Node();
          temp->data = data;
          temp->left = NULL;
          temp->right = NULL;
          return temp;
          }


          That's C style. C++ style would be:



          struct Node { 
          int data_;
          Node *left_ = nullptr;
          Node *right_ = nullptr;

          explicit Node(int data) : data_(data) {}
          };


          Then when you want a new heap-allocated node, you don't call newNode(42) — you call new Node(42)! Or, a good habit you should get into: call std::make_unique<Node>(42) to get back a smart pointer.



          Notice that I added sigils to your data members (data_ etc) to distinguish them from non-member variables; and I declared no more than one variable per line to reduce reader confusion.





          void inorder(Node *&root) 
          {
          if (root != NULL)
          {
          inorder(((root)->left));
          printf("%d n", (root)->data);
          inorder(((root)->right));
          }
          }


          Several things weird here. First, you have a bunch of unnecessary parentheses. (root) is the same thing as root. Second, you're passing root by non-const reference, even though you don't intend to modify it. Third, very minor nit, you're using C-style NULL instead of nullptr. Fourth, why do you print a space before the newline? Fixed up:



          void inorder(const Node *root)
          {
          if (root != nullptr) {
          inorder(root->left);
          printf("%dn", root->data);
          inorder(root->right);
          }
          }


          Remember to remove the redundant parentheses in places like insert(((node)->right),value). It's much easier to read as insert(node->right, value).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          QuuxplusoneQuuxplusone

          11.5k11959




          11.5k11959












          • Yeah the unnecessary parentheses came from when I was editing from the previous solution. Also, surprisingly was not aware of putting a constructor / destructor into the struct, thanks for pointing that out to me. Much needed feedback.
            – Pulse
            51 mins ago


















          • Yeah the unnecessary parentheses came from when I was editing from the previous solution. Also, surprisingly was not aware of putting a constructor / destructor into the struct, thanks for pointing that out to me. Much needed feedback.
            – Pulse
            51 mins ago
















          Yeah the unnecessary parentheses came from when I was editing from the previous solution. Also, surprisingly was not aware of putting a constructor / destructor into the struct, thanks for pointing that out to me. Much needed feedback.
          – Pulse
          51 mins ago




          Yeah the unnecessary parentheses came from when I was editing from the previous solution. Also, surprisingly was not aware of putting a constructor / destructor into the struct, thanks for pointing that out to me. Much needed feedback.
          – Pulse
          51 mins ago










          Pulse is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Pulse is a new contributor. Be nice, and check out our Code of Conduct.













          Pulse is a new contributor. Be nice, and check out our Code of Conduct.












          Pulse is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211072%2fcleaner-way-to-handle-double-pointer-in-c-bst%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo