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?
any solutions?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; }
thanks



2Likes
LinkBack URL
About LinkBacks



