508 views
0 votes
0 votes

Why not this code not printing 10?

(It is just a binary tree with one node i.e. root. And value of root is 10)

#include <stdio.h>
#include<stdlib.h>
struct BinaryTree *root=NULL; 
struct BinaryTree{
	int data;
	struct BinaryTree *left;
	struct BinaryTree *right;
};
 
 struct BinaryTree *node(int data)
 {
 	struct BinaryTree *node;
 	node=(struct BinaryTree*)malloc(sizeof(struct BinaryTree));
 	node->data=data;
 	node->left=node->right=NULL;
 	return node;
 }
struct BinaryTree *insert(int *root,int data1){
	if(root==NULL){
		root=node(data1);
		return root;
	}
}
int main() {
	insert(root,10);
	printf("%d",*root);
	return 0;
}

 

1 Answer

0 votes
0 votes

Either you pass the root node by reference like:

insert(&root,10);
struct BinaryTree *insert(int **root,int data1)

 

Or you assign the returned pointer to node to the root node like:

root = insert(root,10);

Related questions

1 votes
1 votes
1 answer
2
iarnav asked Jan 7, 2018
1,449 views
Consider a binary tree T that has 100 leaf nodes. Then the number of INTERNAL nodes in T that have exactly two children are ______.
0 votes
0 votes
1 answer
3
4 votes
4 votes
3 answers
4
Don't you worry asked Jul 15, 2016
2,830 views
What is the difference betweenBinary Tree and Almost complete Binary tree and complete Binary Tree and full Binary Tree and Binary search Tree and Balanaced Binary Search...