Thread: BST pointer problem root pointer

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    6

    BST pointer problem root pointer

    Hi everyone

    I'm new to c++ programming, I have a problem in the following Binary search tree code .

    when I initialize (root = NULL) , the code compiles ,but when I run it stopped working.
    if I remove the initialization it works , but I can't proceed with the binary tree as I can't initialize any pointer to NULL.

    please HELP ..


    Code:
      #include <iostream>
    
        using namespace std;
    
        struct node
        {
        int key;
        node *left;
        node *right;
        };
    
        void insert_new(node *&root, int key)
        {
    
            root->key = key;
            root->left = NULL;
            root->right = NULL;
    
        }
    
    
        int main()
        {
        node *root = new node;
        root = NULL;
        
        insert_new(root,50);
        return 0;
        }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Don't set root to the location of an allocated memory address and then back to 0 and then try to use it in a function.

    You aren't "initializing" root when you set it to NULL, you're just reassigning it to 0 or NULL or in C++, nullptr which is preferred.

    To initialize the root, use a constructor.

    Also, why are you passing your pointer by reference? Do you need a copy of the address or are you really trying to modify the value of your root pointer?

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    I'm not concerned about passing the pointer reference.

    what I want to do is to begin a binary search tree . in Binary search tree .
    I need to be able to check if the pointer is set to NULL or not .
    so that I can insert new items, but whenever I set root == NULL; the program crashes.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course it does! Have you forgotten pointer and pointee?
    First you create a new node somewhere in the ocean and tell its address to root.
    Then you tell root to forget the address of the node somewhere out in the ocean and tell it to contains the address of neverland.
    Then you tell the program to go searching in neverland - but there is nothing there! Neverland is not an address of a node!
    Even worse is that the node out in the ocean is now lost forever because you forgot its address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    yeah, you are right. Elysia. It finally worked.



    Code:
    #include <iostream>
    
    using namespace std;
    
    struct node
    {
        int key;
        node *left;
        node *right;
    };
    
    void insert_new(node *root, int key)
    {
        if (root != NULL)
        {
          if(key > root->key)
          {
              insert_new(root->right,key);
          }
          else
        {
            if(key < root->key)
          {
              insert_new(root->left,key);
          }
        }
        }
        else
        {
            node *temp = new node;
            temp->key = key;
    
            root = temp;
            temp->left = NULL;
            temp->right = NULL;
    }
    }
    
    
    int main()
    {
        node *root = new node;
        root->right = NULL;
        root->left = NULL;
    
        insert_new(root,50);
        insert_new(root,100);
        
        return 0;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't forget to delete what you new!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    I always forget to delete new
    finally I did it thanks to you, Elysia . Thank you very much.

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct node
    {
        int key;
        node *left;
        node *right;
    };
    
    void create_first_node(node *&root,int key)
    {
        node *temp = new node;
        temp->key = key;
        temp->left = NULL;
        temp->right = NULL;
        root = temp;
    }
    
    void insert_new(node *&root, int key)
    {
    
        if (root != NULL)
        {
          if(key > root->key)
          {
              insert_new(root->right,key);
          }
          else
        {
            if(key < root->key)
          {
              insert_new(root->left,key);
          }
        }
        }
        else
        {
            node *temp = new node;
            temp->key = key;
    
            root = temp;
            temp->left = NULL;
            temp->right = NULL;
    }
    }
    
    
    int main()
    {
        node *root = new node;
        root->right = NULL;
        root->left = NULL;
    
        create_first_node(root,50);
        insert_new(root,100);
        insert_new(root,20);
        insert_new(root,70);
    
         cout << root->key ;
    
    
        return 0;
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You still haven't freed the memory (i.e. delete what you new).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Aboulkomsan View Post
    I always forget to delete new
    finally I did it thanks to you, Elysia . Thank you very much.
    Quote Originally Posted by Elysia View Post
    You still haven't freed the memory (i.e. delete what you new).
    Don't you know? He got promoted to kernel!

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    Quote Originally Posted by MutantJohn View Post
    Don't you know? He got promoted to kernel!
    what does "promoted to kernel" mean?

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Aboulkomsan View Post
    what does "promoted to kernel" mean?
    The kernel is the part of the operating system that handles memory requests made by programs. Should you fail to make a formal request for a deallocation of a block, the kernel will do it for you when the program terminates.

    It's a joke about memory leaks because, I think, kernels weren't always that way.

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    Quote Originally Posted by MutantJohn View Post
    The kernel is the part of the operating system that handles memory requests made by programs. Should you fail to make a formal request for a deallocation of a block, the kernel will do it for you when the program terminates.

    It's a joke about memory leaks because, I think, kernels weren't always that way.
    I have a lot of things to learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointer to a pointer variable
    By Rodri in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 10:50 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. pointer to pointer how do i solve following problem?
    By kobra_swe in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 04:49 PM

Tags for this Thread