Like Tree2Likes
  • 1 Post By anduril462
  • 1 Post By claudiu

tree node allocation

This is a discussion on tree node allocation within the C Programming forums, part of the General Programming Boards category; hi all, i want to create a simple binary tree(not search tree) with three nodes, a,b,c, where b and c ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    44

    tree node allocation

    hi all,
    i want to create a simple binary tree(not search tree) with three nodes, a,b,c, where b and c are children of a. I use two methods, where in method 1 , i do not pass any parameter and return the pointer to allocated node, while in method 2, i pass aNull pointer. however, in method 2, when i pass Null pointer explicitly, it works but otherwise, it gives the error "root1 not initialized". why is it so?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
       typedef struct node{
    		char data;
    		struct node* L;
            struct node* R;
    	    }node;
    	   typedef struct node* tree;  // pointer to structure renamed
           tree newnode(); // function to create each node , method 1
           tree newnode1(tree node1); // create node,method 2
    	   
    	   
    
    int main()
    {
    	   
    	   
    	   tree root,root1,root2 =NULL;
    	   int tree_dpth,n_node;
    	   // create root and left,right child, method 1
    	   root= newnode();  // root
    	   root->L = newnode();  //left child
    	   root->R = newnode();   // right child
            
    	   
    	   //create root and left,right child, method 2
            root1 = newnode1(root1); // this gives run-time error, but newnode1(0), newnode1(NULL) dont.why?
    	  root1->L = newnode1(root1);
    	  root1->R = newnode1(root1);
    
             return 0;
    }
    
      tree newnode()
      {   
    	  char data;
    	  cout<<" enter char value\n";
    	  cin>> data; 
              tree nd= (tree) malloc(sizeof(node));
    	  nd->data =data;
              nd->L= NULL;
    	  nd->R= NULL;
    	  return nd;
      }
        
        tree newnode1(tree nd1)
      {   
    	  char data;
    	  cout<<" enter char value\n";
    	  cin>> data; 
              nd1= (tree) malloc(sizeof(node));
    	  nd1->data =data;
              nd1->L= NULL;
    	  nd1->R= NULL;
    	  return nd1;
      }
    any solutions?
    thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    First, you need to pick whether you're using C or C++. The only thing in here that's particularly C++ is your use of cin and cout. Those are not allowed in C. If you want to use C++, you should post your question in the C++ forum. I'm going to pretend you mean to write C just to answer your question.

    As for the root1 unused, here's the line in question:
    Code:
    root1 = newnode1(root1);
    Before you can assign the result of newnode1() to root1, newnode1 must be called. When it's called, a copy of the current (uninitialized) value of root1 is passed in for use. The compiler is warning you about using the value unitialized. Your declaration line only sets root2 to NULL, the rest have random, garbage values in them. You need to initialize each one to NULL if that's what you want:
    Code:
    tree root = NULL;
    tree root1 = NULL;
    tree root2 = NULL;
    Salem likes this.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That program will never compile in any of the two languages (c or c++) if only because you are not including the correct headers. You should really learn to compile code as you write it rather than posting an utterly meaningless blob of code and expect us to work out the syntax and semantic problems for you.
    Salem likes this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    44
    thanks , i got it. such a dumb mistake i made.
    sid

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    44
    hi, with due respect, i had done the whole code in C++, but thought, i would post it in C forum. so, i copied the entire code, changed the headers for C, but forgot to change the cout,cin. sorry about that.
    sid

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Quote Originally Posted by sed_y View Post
    hi, with due respect, i had done the whole code in C++, but thought, i would post it in C forum. so, i copied the entire code, changed the headers for C, but forgot to change the cout,cin. sorry about that.
    sid
    Don't do that in the future. What you basically did is write some code that didn't work, then change 90% of it, so you potentially covered up your errors, and created other, new errors, then failed to explain this to anybody so we had no idea what we were dealing with. Next time, if you're writing in C++, leave it in C++ and go to the C++ forum. They're two very different languages, and the experts there (there are many) are as good in C++ as the C experts here. Still, glad you got it figured out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Binary Tree Node
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-29-2009, 11:39 PM
  2. Getting the parent of a node in a binary tree
    By budala in forum C Programming
    Replies: 4
    Last Post: 09-18-2009, 12:36 PM
  3. Delete a node in a binary tree
    By alice in forum C Programming
    Replies: 2
    Last Post: 07-05-2004, 05:01 AM
  4. delete node for a binary tree
    By AmazingRando in forum C Programming
    Replies: 4
    Last Post: 10-27-2003, 09:45 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21