Your question, your audience. Choose who sees your identityâ€”and your questionâ€”with question security.
void insert_tree(TREE *tree,int x)
{
NODE *p=(NODE *)malloc(sizeof(NODE));
if(tree->root==NULL)
{
p=tree->root;
p->data=x;
p->left=NULL;
p->right=NULL;
}
else
{
if((x < p->data)&&(p->left==NULL));
{
p->left=insert_node_tree(p->left,x);
}
else
{
// root->right=insert_node_tree(root->right,x);
}
(tree->size)++;
}
p->left=insert_node_tree(p->left,x);
if((x < p->data)&&(p->left==NULL));
void insert_node_tree(TREE *tree,int x)
{
NODE *p=(NODE *)malloc(sizeof(NODE));
if(tree==NULL)
{
// p=tree->root; REMOVE THIS
p->data=x;
p->left=NULL;
p->right=NULL;
tree = p; // ADD THIS
}
}
tree = p; // ADD THIS
with the message :#include<stdio.h>
#include<stdlib.h>
typedef struct tnode
{
int data;
struct tnode *left;
struct tnode *right;
}NODE;
typedef struct
{
NODE * root;
int size;
}TREE;
void initialization_tree(TREE *tree)
{
NODE *root=NULL;
tree->size=0;
}
void insert_node_tree(TREE *tree,int x)
{
NODE *p=(NODE *)malloc(sizeof(NODE));
if(tree->root==NULL)
{
p=tree->root;
p->data=x;
p->left=NULL;
p->right=NULL;
}
else
{
if((x < p->data)&&(p->left==NULL))
{
p->left=insert_node_tree
(p->left,x);
}
else
{
// root->right=insert_node_tree(root->right,x);
}
(tree->size)++;
}
}
int main()
{
TREE tree;
initialization_tree(&tree);
insert_node_tree(&tree,12);
return 0;
}
else
{
if ((x < p->data)&&(p->left==NULL))
{
p->left=insert_node_tree(p->left,x);
}
p->left=insert_node_tree(p->left,x);
insert_node_tree(tree->root,12);
void insert_node_tree(NODE *tree,int x)
{
there should be only one tree structure per tree you are managing.julianH, you don't seem to see that your recommendations and the code you supply are in contradiction. 'root' is not a member of NODE but of TREE. so, your advice will not help but confuse.
tree->root should be passed to the insert function i.e
insert_node_tree(tree->root,12);
void insert_node_tree(NODE *tree,int x)
that your recommendations and the code you supply are in contradiction.No they are not.
void insert_node_tree(NODE *subtree,int x)
{
NODE *p=(NODE *)malloc(sizeof(NODE));
if(subtree==NULL) {
p->data=x;
p->left=NULL;
p->right=NULL;
subtree = p;
}
else {
if(x < p->data) {
insert_node_tree(p->left,x);
}
else {
insert_node_tree(p->right,x);
}
(subtree->size)++;
}
}
void insert_to_tree(TREE * tree, int data)
{
if (tree == NULL)
... // don't accept a NULL tree
if (tree->root == NULL)
{
// create a new root NODE and assign data member
...
}
else
{
insert_to _node(tree->root, data);
...
void insert_to_node(NODE * node, int data)
{
if (node == NULL)
{
// may not happen: error
...
}
if (data < node->data)
{
if (node->left == NULL)
{
// create a new left node and assign data
...
}
// recursive call passing a valid node
insert_to_node(node->left, data);
}
else
{
// handling for right sub node accordingly
...
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
{
p->left=insert_node_tree(p
}
Look at the above code - you are checking if p->left is NULL and if it is you are passing it to insert_node_tree
So tree is coming in with a NULL pointer.
Then here you have
if(tree->root==NULL)
{
p=tree->root;
p->data=x;
p->left=NULL;
p->right=NULL;
}
What if tree is null
Should be
if(treet==NULL)
{
/* p=tree->root; REMOVE THIS */
p->data=x;
p->left=NULL;
p->right=NULL;
tree = p; // ADD THIS
}