Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am starting to move into Binary trees in my C class. I understand the concept of a binary tree, but now I'm trying to gain a deeper understanding of how it works. I tried to set up a simple binary tree that changes size based on what the user enters. Whenever I run the program it crashes after the first input. Could someone help me understand why?
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int value;
struct node *left;
struct node *right;
}node;
void traverse(node *root);
void addnode (node *root, int nUser);
int checknode (node *root, int nUser);
int main (void)
{
int nUser;
node *root;
root=(node *)malloc(sizeof(node));
root->value=10;
do
{
printf("Please enter any integer, enter 0 when done\n\n\n"); /*Program crashes when I enter the first value, unsure why*/
scanf("%d",&nUser);
if(!(checknode(root,nUser)))/*check node runs through the binary tree to find the data if it exists, returns 1 if exists, 0 if it doesn't*/
addnode(root,nUser); /*add node runs through the binary tree, when it finds a place where the number will fit, that is a null pointer where the number can be placed it will create a new node with the value and null pointers on right and left*/
}while(nUser);
traverse(root);/*should traverse the tree and print out values*/
return(0);
}
void traverse(node *root)
{
printf("%d/n",root->value);
if(root->left)
{
traverse(root->left);
}
if(root->right)
{
traverse(root->right);
}
}
void addnode (node *root, int nUser)
{
if(root->value<nUser)
if(root->right)
{
addnode(root->right,nUser);
}
else
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->value=nUser;
root->right=temp;
}
if(root->value>nUser)
if(root->left)
{
addnode(root->left,nUser);
}
else
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->value=nUser;
root->left=temp;
}
}
int checknode (node *root, int nUser)
{
if(!(root->value==nUser))
{
if(root->value<nUser)
if(root->right)
{
checknode(root->right,nUser);
}
else
{
return(0);
}
if(root->value>nUser)
if(root->left)
{
checknode(root->left,nUser);
}
else
{
return(0);
}
}
else
{
return (1);
}
}
I think that the problem is that the left and right pointers of root are uninitialized when you pass root to addnode. A buffer returned by malloc is not guaranteed to be NULL.
Try initializing root->right and root->left to NULL before the beginning of the loop.
root=(node *)malloc(sizeof(node));
Change to:
root=(node *)calloc(1, sizeof(node));
You didn't clear root->left and root->right to NULL, so you are using uninitialized pointers. You should change all your malloc to calloc, or you need to clear your left and right pointers to NULL after each malloc.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need to create a c program for BST but I can't create left child or right child for the root node. Creation of root node is ok. I can also search the element in the root node. But Can't add more than one element in the tree
Please help me solve this problem, kindly explain about the mistake that I made
#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
struct tree *left;
int data;
struct tree *right;
}node;
node *ROOT;
//////////////////////////
void *bud()
{
return (node *)malloc(sizeof(node));
}
/////////////////////
void createNode(int data, node *ptr)
{
if(ptr==NULL)
{
ptr=bud();
ptr->data=data;
ptr->left=NULL;
ptr->right=NULL;
ROOT=ptr;
ptr=NULL;
printf("DONE");
return;
}
else if(data==ptr->data)
{
printf("Duplication not possible");
return;
}
else if (data<ptr->data)
{
//ptr=ptr->left;
createNode(data,ptr->left);
}
else if (data>ptr->data)
{
//ptr=ptr->right;
createNode(data,ptr->right);
}
else
{
printf("INVALID");
}
}
//////////////////////////////////////
void search(int data,node *ptr)
{
if(ptr==NULL)
{
printf("NOT FOUND");
return;
}
else if (ptr->data==data)
{
printf("Item Found");
return;
}
else if(data<ptr->data)
{
//ptr=ptr->left;
search(data,ptr->left);
}
else if (data>ptr->data)
{
//ptr=ptr->right;
search(data,ptr->right);
}
else
{
printf("INVALID");
}
}
/////////////////////////////////
void main()
{
int ch;
ch=0;
while (ch!=6)
{
printf("\nMENU\n1. Create Tree\n2. Search\n3. Inorder\n4.
Preorder\n5. Postorder\n6. Exit\n EnterYour Choice: ");
scanf("%d",&ch);
if(ch==1)
{
int dat;
printf("\nEnter the number to be inserted: ");
scanf("%d",&dat);
createNode(dat,ROOT);
}
else if(ch==2)
{
int dat;
printf("Enter number to be searched: ");
scanf("%d",&dat);
search(dat,ROOT);
}
else if(ch==6)
{
return;
}
else
{
printf("\nEnter a valid choice\n");
}
}
}
The problem is logical error. I can't create the left or right child of the tree. Whatever I insert to the tree gets inserted to the root node.
I can't create the left or right child of the tree. Whatever I insert to the tree gets inserted to the root node.
This is happening because of this statement in your createNode():
ROOT=ptr;
Whenever you are inserting an element in the tree, you are traversing tree recursively and when you find the appropriate location to insert, you are passing it to createNode() which is NULL (either ptr->left or ptr->right) and ROOT is reassigned to ptr.
If I just make changes in your program to make work it properly, the createNode() will look like this:
void createNode(int data, node **ptr)
{
if(*ptr==NULL)
{
*ptr=bud();
(*ptr)->data=data;
(*ptr)->left=NULL;
(*ptr)->right=NULL;
printf("DONE");
return;
}
else if(data==(*ptr)->data)
{
printf("Duplication not possible");
return;
}
else if (data<(*ptr)->data)
{
createNode(data,&((*ptr)->left));
}
else if (data>(*ptr)->data)
{
createNode(data,&((*ptr)->right));
}
else
{
printf("INVALID");
}
}
And in main(), you need to do:
createNode(dat,&ROOT);
However, there is a scope of improvement in your program, for e.g. separate the node creation and insertion operation in your program. I am leaving it up to you to find the improvements and better ways of theirs implementation.
The error is in your createNode() function: you pass node *ptr and assign like this ptr=bud();. But this only modifies local variable, not pointer in parent node. Try passing node **ptr and do dereferencing inside of the function *ptr=bud();. Use & operator to derive pointer to pointer. As #H.S. mentioned ROOT=ptr; statement makes your program lose node. Besides you allocate memory and don't free it (you can't even do that, because you lose pointer to ROOT node).
Repeated Question:
Recently I'm reading Data Structure(Binary Search Trees), I understand recursion very well and can trace it as well.
I used an approach which always worked for me i.e write a program with a loop, then eliminate loop and write a recursive function, the base condition will be same as loop exit condition.
But when it comes to writing one without my loop method, am getting failed.
I wasn't able to write a recursive function to insert a node in Binary Search Tree.(Though I understood it properly by referring the solution).
Kindly guide me, How to improve it?
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;//To store the address of the left child
struct node *right;//To store the address of the Right child
};
struct node * root;
struct node *createnewnode(int x)
{
struct node *n=(struct node *)malloc(sizeof(struct node));
n->data=x;
n->left=NULL;
n->right=NULL;
return n;
}
void Insert(int x)
{
struct node *a,*b;
struct node *temp=root;
if(root==NULL)
root=createnewnode(x);
else
{
while(1)
{
if(x<=temp->data)
{
if(temp->left!=NULL)
temp=temp->left;
else
{
a=createnewnode(x);
temp->left=a;
break;
}
}
else
{
if(temp->right!=NULL)
temp=temp->right;
else
{
a=createnewnode(x);
temp->right=a;
break;
}
}
}
}
}
int main()
{
root==NULL;//Empty Tree
Insert(15);
Insert(10);
Insert(20);
Insert(25);
return 0;
}
Edit: Sorry for not posting the code previously.
This is the code I have written for inserting a node, now how do I convert this into a recursive method?
The recursive Insert always asks the following question: can I insert a node in the current root? If not because root is not null then I have to check whether I have to recurse on the left or right subtree and call Insert recursively.
Something like the following should be enough to give you an idea on how to do it
Node* Insert(Node* root, int x) {
if (root == NULL)
return createnewnode(x);
else
if (x <= root->data)
root->left=Insert(root->left);
else
root->right=Insert(root->right);
}
My code is not printing the elements of binary search tree:
//x is the element to be inserted
//structure of program
typedef struct BST
{
int info;
struct BST *left;
//pointer to left node
struct BST *right;
//pointer to right node
}
bst;
//global root variable
bst *root;
void insert(int x)
{
bst *ptr,*sptr=root;
ptr=(bst*)malloc(sizeof(bst));
ptr->info=x;
if(root==NULL)
{
ptr->left=ptr->right=NULL;
root=ptr;
}
while(sptr!=NULL)
{
if(x<sptr->info)
{
sptr=sptr->left;
}
else
sptr=sptr->right;
}
sptr=ptr;
}
edit:
//following is the show function
void show()
{
bst *ptr=root;
while(ptr!=NULL)
{
//it will print untill the ptr is null
printf("%d",ptr->info);
ptr=ptr->left;
ptr=ptr->right;
}
}
Where is the value of root coming from? You're not passing in the value anywhere? Also, it is tough to help when we don't know the design of type bst.
It appears that you have the right idea. Create a node, and give it some data. If the root is null, then the new value is the root of the BST. After that you go ahead and find the first null node either in the left or right subtree of the root using the standard BST behavior. Finally, when you reach the end you go ahead and insert the last node in the proper place.
void insert(int x)
{
bst *ptr, *sptr=root; //<-- the value right here?
ptr = malloc(sizeof(bst));
ptr->info = x;
if(root == NULL)
{
ptr->left=ptr->right=NULL;
root=ptr;
}
while(sptr!=NULL)
{
if(x<sptr->info)
{
sptr=sptr->left;
}
else
sptr=sptr->right;
}
sptr=ptr; // <-- What is this line trying to do?
}
However, where did your updated tree go?
Since in C everything is passed by value, you're running into the problem where you're not seeing your updated tree after you leave this function. You need to go ahead and change the function to return a bst* type, and also maintain the root node during the entire function. Now the first line of code (*sptr = root) makes more sense! Finally, you were not setting the left and right fields of ptr to NULL. This means you were jumping over your if statements.
bst* insert(int x, bst *root)
{
bst *ptr, *sptr=root;
ptr = malloc(sizeof(bst));
ptr->left = NULL;
ptr->right = NULL;
ptr->info = x;
if(root == NULL)
{
ptr->left=ptr->right=NULL;
root=ptr;
return root;
}
while(sptr!=NULL)
{
if(x<sptr->info)
{
sptr=sptr->left;
}
else
sptr=sptr->right;
}
sptr=ptr;
return root;
}
What about the next function?
I just started looking at this one too. I am not used to the global variables in c, so I will go ahead and make two modifications. Let's make this recursive, and pass in the value of the root rather than using the global.
void show(bst *root)
{
if(root == NULL){
return;
}
printf("%d",root->info);
show(root->left);
show(root->right);
}
This will take in some value, and solve the tree recursively, and print as it reaches each node. Thus, it will print the root node (if it exists), and then print the left entire left subtree before it prints the right subtree.
Finally, looking at your main
I added the local variable root and thus you will have to remove the global variable named root outside of your main function. I also set the value of it to null so your first insert will fire correctly.
int main()
{
int i,n,x;
bst *root = NULL; //<-- I added this line of code to remove the global
puts("Enter number of elements");
scanf("%d",&x);
for(i=0;i<x;i++)
{
puts("Enter elements");
scanf("%d",&n);
root = insert(n, root);
}
show(root);
return 0;
}
I hope this helps!
i have written a simple code to create and insert elements into a binary search tree, when i write the code in the following manner, the chaining doesn't seem to happen, can anybody help me understand what exactly is happening in the insert function ? I know the code to insert elements into a binary search tree, just curious to know why this one doesn't work.
#include <stdio.h>
#include<stdlib.h>
struct node {
struct node *left;
struct node* right;
int element;
};
void insert(struct node *node,int x) {
if(node==NULL) {
node=(struct node *)malloc(sizeof(struct node));
node->element=x;
node->left=NULL;
node->right=NULL;
} else {
if(x<node->element) {
insert(node->left,x);}
else {
insert(node->right,x);
}
}
}
void inorder(struct node *base) {
if(base!=NULL) {
inorder(base->left);
printf("%d ",base->element);
inorder(base->right);
}
}
int main(int argc, char *argv[]) {
struct node *base;
base=(struct node *)malloc(sizeof(struct node));
base->element=1;
base->left=NULL;
base->right=NULL;
insert(base,25);
insert(base,30);
inorder(base);
return 0;
}
if the insert function is written this way, it works but still doesn't work for creation of the first node of the binary search tree, confused :/
void insert2(struct node *node,int x) {
if(node==NULL) {
node=(struct node *)malloc(sizeof(struct node));
node->element=x;
node->left=NULL;
node->right=NULL;
} else {
if(x<node->element) {
if(node->left==NULL) {
node->left=(struct node *)malloc(sizeof(struct node));
node->left->element=x;
node->left->left=NULL;
node->left->right=NULL;
} else {
insert2(node->left,x);
}
} else {
if(node->right==NULL) {
node->right=(struct node *)malloc(sizeof(struct node));
node->right->element=x;
node->right->left=NULL;
node->right->right=NULL;
} else {
insert2(node->right,x);
}
}
}
}
When you pass a value to a function in C, you're using pass-by-value. What that means is the value gets copied into another variable which becomes local to the function.
void change(struct node *n) {
n = 42;
}
int main(void) {
change(NULL); // change can't change the value of NULL, can it?
}
Now take a look at your insert function... Suppose I call insert(NULL,42);, do you think insert is trying to change NULL? Or is it trying to change some local variable, which your main function can't see?
You know what needs to be done already... Either make your insert function use an extra level of pointer indirection as you've suggested in your comments, or return the root node from insert so that you can propagate the changes made by insert into the data structure stored in main (using the assignment = operator).
C is pass-by-value. When you pass something into a function's parameter, the function makes a copy of it. So when you pass a struct node pointer into the insert function, C makes a copy of that pointer.
What you did in main is first you made a struct node* base:
base --> struct node.
When you call insert(base, 25), C makes a copy of base. So now in your memory you have something like this:
basecpy ---> [struct node] <--- base
So in your insert, you are essentially doing
if(basecpy==NULL)
{basecpy=(struct node *)malloc(sizeof(struct node));
basecpy->element=x;
basecpy->left=NULL;
basecpy->right=NULL;}
Which does not change base in anyway at all - it just changes basecpy.
This can be solved by using double pointers:
void insert(struct node **node, int x) {
if (*node == NULL) {
*node = malloc(sizeof(*node));
(*node)->element=x;
(*node)->left=NULL;
(*node)->right=NULL;
} else {
if (x < (*node)->element) {
insert(&((*node)->left), x);
} else {
insert(&((*node)->right), x);
}
}
}
And then to use it, pass in the address of base.
insert(&base, 25);
Pointers can be tricky :)
ideone
In the below code, I'am creating a binary tree using insert function and trying to display the inserted elements using inorder function which follows the logic of In-order traversal.When I run it, numbers are getting inserted but when I try the inorder function( input 3), the program continues for next input without displaying anything. I guess there might be a logical error.Please help me clear it.
Thanks in advance...
#include<stdio.h>
#include<stdlib.h>
int i;
typedef struct ll
{
int data;
struct ll *left;
struct ll *right;
} node;
node *root1=NULL; // the root node
void insert(node *root,int n)
{
if(root==NULL) //for the first(root) node
{
root=(node *)malloc(sizeof(node));
root->data=n;
root->right=NULL;
root->left=NULL;
}
else
{
if(n<(root->data))
{
root->left=(node *)malloc(sizeof(node));
insert(root->left,n);
}
else if(n>(root->data))
{
root->right=(node *)malloc(sizeof(node));
insert(root->right,n);
}
else
{
root->data=n;
}
}
}
void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
}
main()
{
int n,choice=1;
while(choice!=0)
{
printf("Enter choice--- 1 for insert, 3 for inorder and 0 for exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter number to be inserted\n");
scanf("%d",&n);
insert(root1,n);
break;
case 3:
inorder(root1);
break;
default:
break;
}
}
}
Your insert accepts a pointer to a node, which is local to the scope of the function. After insert returns, the root it was called upon remains unchanged. And you get a leak.
If you want the changes insert does to be visible outside of it, start by changing its signature
void insert(node **root,int n)
And calling it like so:
void insert(&root1, n);
That way, it will modify root1 and not a copy of it.
As a rule of thumb, if a function needs to modify something, it should be passed a pointer to it. You function needs to modify a node*, so it should be passed a node**.
EDIT
As unwind pointed out, you could also return the root as a means of updating it
root1 = insert(root1, n);
This of course works only if you need to modify one object.
Your allocation code is off, root1 will always stay NULL since the malloc result is only assigned to a local variable. Pass root in as a pointer to pointer.
Additionally you unconditionally malloc left and right, even though there might already be data there, you should call insert immediately and have the called insert handle the malloc just like for root. Again with a pointer to pointer of course.
Finally you else root->data = n doesn't make any sense, it's already n. And I presume you don't want duplicates in your tree? If you do you need to change your code.
Pass reference of node * to the insert() so that when new node is allocated, its appropriately updated.
Also, in your insert() function, don't allocate while moving into below tree. If you allocate then your code tries to check the value on that node and proceed again, which is not correct.
Updated code would be:
void insert(node **root_ptr,int n)
{
if(root==NULL) //for the first(root) node
{
root=(node *)malloc(sizeof(node));
root->data=n;
root->right=NULL;
root->left=NULL;
*root_ptr = root;
}
else
{
if(n<(root->data))
{
//root->left=(node *)malloc(sizeof(node));
insert(&root->left,n);
}
else if(n>(root->data))
{
//root->right=(node *)malloc(sizeof(node));
insert(&root->right,n);
}
else
{
root->data=n;
}
}
}
From main() call it as insert(&root1,n);