0

I am trying to implement insert function in binary search tree. I was able to do it without using recursion(the commented part). But while doing it using recursion my root is not geting updated Following is my code :

 #include<bits/stdc++.h>
using namespace std;

class Node{
public:
    int data;
    Node *left;
    Node *right;

    Node(int x){
        data = x;
        left = NULL;
        right = NULL;
    }
};

void  insert(Node *root,int  data){
    // Node *temp  = root;
    // while (temp){

    //  if (data == temp->data )
    //      return; 

    //  if (data > temp->data){
    //      cout<<temp->data<<endl;
    //      if(temp->right == NULL){

    //          Node *node = new Node(data);
    //          temp->right = node;
    //          return ;
    //      }
    //      temp = temp->right;     
    //  }

    //  else {
    //      if (!temp->left){
    //          Node *node = new Node(data);
    //          temp->left = node;
    //          return ;
    //      }
    //      temp = temp->left;
    //  }   
    // }
    if(root == NULL){

        root = new Node(data);

        cout<<root->data<<endl;
        return;
    }
    // Node *temp = root;
    if(data >= root->data ){
        insert(root->right,data);

    }

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

int main(){
    Node *root = NULL;

    insert(root,45);
    if(root == NULL)
        cout<<"hello"<<endl;
    // cout<<root->data;
    // insert(root,8);

    inorder(root);
    // cout<<root->data;

}

On running root is remaining NULL, which suppposed to get updated. Any help will be appreciated.

Deepak
  • 49
  • 1
  • 6
  • 1
    Your are working with the local variable `Node* root` in your function (arguments are local variables). Either return this variable or use a reference to pointer to modify the pointer. – 273K Oct 06 '19 at 18:00
  • return Node* from insert and read it into root, the local pointer is going out of scope after insert( ) exits – Abdul kareem Oct 06 '19 at 18:04
  • Unrelated: Don't do `#include`. It's a non-standard header file. – Ted Lyngmo Oct 06 '19 at 18:05
  • #include seems to pretty useful in linux. any particular reason why we shoudln't use it?? – Deepak Oct 06 '19 at 18:08
  • 1
    @TOMWOOD [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Ted Lyngmo Oct 06 '19 at 18:10
  • 1. It's an internal file for only some compilers, so you have no guarantees of its behavior or even of its existence. You code might not work with other compilers or even newer versions of the same compiler. 2. Especially when used with `using namespace std`, it pollutes the global namespace with identifiers, many of which can easily cause conflicts. 3. You don't learn how to include standard library headers the right way. – eesiraed Oct 06 '19 at 18:52

0 Answers0