I made a binary tree but it has some logic problem can some one point

#include<stdio.h>
#include<stdlib.h>
struct node {
	struct node *left, *right;
	int data, color;
} *root;
int check = 0;
typedef struct node tree;

tree *create_node(int);
void add_tree(tree *, tree *);
void travel_tree(tree *);
int main()
{
	int i, j, value;
	tree *nv;
	printf("enter value \n");
	scanf("%d", &value);
	nv = create_node(value);
	add_tree(nv, root);
	travel_tree(root);
}

tree *create_node(int num)
{
	tree *temp;
	if (check == 0) {
		root = (tree *) malloc(sizeof(tree));
		root->data = num;
		root->left = NULL;
		root->right = NULL;
		temp=root;
	}
	if (check != 0) {

		temp = (tree *) malloc(sizeof(tree));
		temp->data = num;
		temp->left = NULL;
		temp->right = NULL;

	}
	check++;
	return temp;
}

void add_tree(tree * something, tree * node)
{
	tree *ki;
	ki=node;
	if (check == 0)
		return;
	if ((something->data < ki->data) && (ki->left)) {
		add_tree(something, node->left);
	} else if ((something->data < ki->data) && (ki->left == NULL)) {
		//node left is to be added at last
		ki->left = something;
		return;
	}
	if ((something->data > node->data) && (ki->right)) {
		add_tree(something, node->right);
	}
	if ((something->data > ki->data) && (ki->right == NULL)) {
		//right node is to be added
		ki->right = something;
		return;
	}

}

void travel_tree(tree * temp)
{
	if (temp->left) {
		travel_tree(temp->left);
		printf(" %d", temp->data);
		return;
	}
	if (temp) {
		travel_tree(temp);
		printf(" %d", temp->data);
		return;
	}
	if (temp->right) {
		travel_tree(temp->right);
		printf(" %d", temp->data);
		return;
	}
}

My logic is I will create a node in function create_node and return a pointer to the node.
Will pass this pointer which will be in nv and root is initial call on add_tree function
when it is called subsequently I am passing left or right sibling depending upon the data in nv is greater or less than parent node to left or right.

What exactly is your problem ?
As the way I see there are many issues with this program. That being said I appreciate the fact that you attempted to work on this problem instead of simply posting "give me the code"
Here check out this link
It is very helpful guide on how to write code for basic binary tree problems

Thanks your link is awesome I am solving binary tree related problems the link you gave surely will help I am trying to implement solutions to the problems on the link you gave.
My logic for above program has some error which I am unable to find hence I posted the code here.

Ok I took a pen and paper to understand the fault in my program
my new program is

#include<stdio.h>
#include<stdlib.h>
struct node {
	struct node *left, *right;
	int data, color;
} *root;
int check = 0;
typedef struct node tree;

tree * create_node(int);
void add_tree(int value, tree *);
void travel_tree(tree *);
int main()
{
	int i, j, value;
	tree *nv;
	printf("enter value \n");
	scanf("%d", &value);
//	printf("\n check is %d",check);
	nv = create_node(value);
	add_tree(value, root);
	travel_tree(root);
}

tree * create_node(int num)
{
	tree *temp;
	printf("\n reached create_node\n");
	if (check == 0) {
		root = (tree *) malloc(sizeof(tree));
		root->data = num;
		root->left = NULL;
		root->right = NULL;
		temp = root;
		check++;
		printf("\nreached check==0 in create_node");
		return temp;
	}
	if (check != 0) {

		temp = (tree *) malloc(sizeof(tree));
		temp->data = num;
		temp->left = NULL;
		temp->right = NULL;
		return temp;

	}
	printf(" coming in \n");
//  check++;
	//return temp;
}

void add_tree(int v1, tree * node)
{
	tree *ki;
	ki = node;
	if (check == 0) {
		root = create_node(v1);
		return;		//check ==0 makes sure if the tree is add root node to it
	}
	if ((v1 < node->data) && (node->left)) {
		add_tree(v1, node->left);
	} else if ((v1 < ki->data) && (node->left == NULL)) {
		//node left is to be added at last
		node->left=create_node(v1);
		return;
	}
	if ((v1 > node->data) && (node->right)) {
		add_tree(v1, node->right);
		return;
	}
	else if ((v1 > node->data) && (node->right == NULL)) {
		//right node is to be added
		node->right = create_node(v1);
		return;
	}

}

void travel_tree(tree * temp)
{
	if (temp->left) {
		travel_tree(temp->left);
		printf(" %d", temp->data);
		return;
	}
	if (temp) {
		travel_tree(temp);
		printf(" %d", temp->data);
		return;
	}
	if (temp->right) {
		travel_tree(temp->right);
		printf(" %d", temp->data);
		return;
	}
}

The above program is giving me segmentation fault.

When I do a

./a.out following happens

enter value 
46
 reached create_node

Segmentation fault

Quick question why are create_node and add_tree separate functions ?
All you are doing in create_node is allotting memory for a new node and then checking to see if this node should be the parent node in the tree or not ?

Maybe you want to combine both of these functions. Here is how I would do it .

void add_Node(struct node** parent, int value)
{
    // Allocate new memory. Check for error
    // Check if *parent is NULL. If it is then this is the first node in the tree. Else this is a child
    // If this is the first node in the tree then you are done
    // Else start a while loop for to figure out where exactly in the tree should this new node be placed
}

Edited 6 Years Ago by abhimanipal: n/a

Ok I found the previous program were having problem in add_node function since after adding a node when it returns to the previous instance of add_node which called the current
it goes down and checked if(v1>node->right) and if it exist it will create a node irrespective of the fact it has already traveled the tree once to add.


You are using a while loop.
I want to do it recursively.
Here is the new program

#include<stdio.h>
#include<stdlib.h>
struct node {
	struct node *left, *right;
	int data, color;
} *root;
int check = 0;
typedef struct node tree;
tree *create_node(int);
void add_tree(int value, tree *);
void travel_tree(tree *);
int main()
{
	int i, j, value;
	tree *nv;
	root = NULL;
	value = 0;
	while (1==1) {
		printf("enter value \n");
		scanf("%d", &value);
		if(value==1)
		break;
		if (root == NULL)
			root = create_node(value);
		else if (root != NULL) {
			//      nv = create_node(value);
			add_tree(value, root);
		}
	}
	travel_tree(root);
}

tree *create_node(int num)
{
	tree *temp;

	temp = (tree *) malloc(sizeof(tree));
	temp->data = num;
	temp->left = NULL;
	temp->right = NULL;
	return temp;

}

void add_tree(int v1, tree * node)
{

	if (v1 <= node->data) {
		if (node->left != NULL) {
			add_tree(v1, node->left);
		} else {
			node->left = create_node(v1);
		}
	return;	
	}
	if (v1>node->data)  {
		if (node->right != NULL) {
			add_tree(v1, node->right);
		} else {
			node->right = create_node(v1);
		}
	return;	
	}
	
}

void travel_tree(tree * temp)
{
//	printf("\n inside travel_tree");

	if (temp->left!=NULL) 
	travel_tree(temp->left);
	printf(" \n %d ", temp->data);
	if (temp->right!=NULL) 
	travel_tree(temp->right);
	return;
}

This new program which I just posted is having some problem.Not sure what.

Edited 6 Years Ago by onus: n/a

It would be more helpful if you gave me some more information about the "PROBLEM" you are having ?
How about using some debugger/ printfs and trying to pin point your problem

Just saying program does not work wont cut it.

If I would have understood what to do why would have I posted here.

//This program is to create a binary tree 

#include<stdio.h>
#include<stdlib.h>
struct node {
	struct node *left, *right;
	int data, color;
} *root;
int check = 0;
typedef struct node tree;
tree *create_node(int);
void add_tree(int value, tree *);
void travel_tree(tree *);
int main()
{
	int i, j, value;
	tree *nv;
	root = NULL;
	value = 0;
	while (1==1) {
		printf("enter value \n");
		scanf("%d", &value);
		if(value==1)
		break;
		if (root == NULL)
			root = create_node(value);
		else if (root != NULL) {
			//      nv = create_node(value);
			add_tree(value, root);
		}
	}
	travel_tree(root);
}

tree *create_node(int num)
{
	tree *temp;

	temp = (tree *) malloc(sizeof(tree));
	temp->data = num;
	temp->left = NULL;
	temp->right = NULL;
	return temp;

}

void add_tree(int v1, tree * node)
{

	if (v1 <= node->data) {
		if (node->left != NULL) {
			add_tree(v1, node->left);
		} else {
			node->left = create_node(v1);
		}
	return;	
	}
	if (v1>node->data)  {
		if (node->right != NULL) {
			add_tree(v1, node->right);
		} else {
			node->right = create_node(v1);
		}
	return;	
	}
	
}

void travel_tree(tree * temp)
{
//	printf("\n inside travel_tree");

	if (temp->left!=NULL) 
	travel_tree(temp->left);
	printf(" \n %d ", temp->data);
	if (temp->right!=NULL) 
	travel_tree(temp->right);
	return;
}

Try to run this new program I believe this works perfectly.
I wrote it this week but have tested only with one input.
5 9 7 3 8 1.
and output is
3 5 7 8 9
last 1 is test condition so I have not included it in tree.

This article has been dead for over six months. Start a new discussion instead.