Binary Tree segmentation fault - c

Here i have written a code which inserts numbers in Binary tree. But it gives segmentation fault error.
And also it says " note: expected ‘struct tree *’ but argument is of type ‘struct node *’" in line 8.
Here is the code :-
#include<stdio.h>
#include<stdlib.h>
struct tree{
int data;
struct tree *left;
struct tree *right;
};
struct tree* insert(struct tree* node, int data)
{
if(!node){
node=malloc(sizeof(struct tree));
node->data=data;
node->left=node->right=NULL;
return node;
}
else {
if(data>node->data){
node->right= insert(node->right,data);
return node;
}
else{
node->left= insert(node->left,data);
}
return node;
}
}
printtree(struct tree* node)
{
if(node){
printf("%d",node->data);
}
printtree(node->left);
printtree(node->right);
}
main()
{
int i,n;
struct tree *NODE;
NODE= insert(NODE,5);
NODE= insert(NODE,3);
NODE= insert(NODE,8);
printtree(NODE);
}

You use if( node ) but better to use if( node != NULL )
You use if( !node ) but better to use if( node == NULL )
It make code more readable.
You have so many mistakes - so ... I make it in my way (my code formating, etc.).
printtree(node->left); printtree(node->right); was outside if( node != NULL ){} so it try to get NULL->left and NULL->right
Tested - code works.
#include<stdio.h>
#include<stdlib.h>
struct tree{
int data;
struct tree *left;
struct tree *right;
};
struct tree* insert(struct tree* node, int data)
{
if( node == NULL ) {
node = malloc( sizeof(struct tree) );
node->data = data;
node->left = node->right = NULL;
} else {
if( data > node->data ){
node->right = insert(node->right, data);
} else {
node->left = insert(node->left, data);
}
}
return node;
}
void printtree(struct tree* node)
{
if( node != NULL ){
printf("%d\n", node->data);
printtree(node->left);
printtree(node->right);
}
}
int main()
{
struct tree *NODE = NULL;
NODE = insert(NODE, 5);
NODE = insert(NODE, 3);
NODE = insert(NODE, 8);
printtree(NODE);
return 0;
}

The local variable: struct tree* node; is not initialized, so the if (!node) test will have undefined behavior. Unless you assign it something or use it to hold a malloc'd node, the expression in the else block tries to dereference an uninitialized pointer.
You should also get used to idea that a tree can be considered a 'recursive' structure, so any node is a tree, and the top-level tree is simply a node. There's no good reason for two separate types here.

You are still making the error of passing NODE by value. If you want to modify it, you must use a pointer to that pointer.
#include<stdio.h>
#include<stdlib.h>
typedef struct t
{
int data;
struct t *left;
struct t *right;
}tree;
tree* insert(tree **node, int data)
{
if(!(*node))
{
*node=malloc(sizeof(tree));
(*node)->data=data;
(*node)->left=(*node)->right=NULL;
return *node;
}
else
{
if(data>(*node)->data)
{
(*node)->right = insert(&((*node)->right),data);
return *node;
}
else
{
(*node)->left = insert(&((*node)->left),data);
return *node;
}
}
}
void printtree(tree *node)
{
if(node)
{
printf("%d",node->data);
printtree(node->left);
printtree(node->right);
}
}
void freeMemory(tree *node)
{
if(node)
{
freeMemory(node->left);
freeMemory(node->right);
free(node);
}
}
int main()
{
tree *NODE = NULL;
NODE= insert(&NODE,5);
NODE= insert(&NODE,3);
NODE= insert(&NODE,8);
printtree(NODE);
freeMemory(NODE);
return 0;
}
Link: http://ideone.com/OpZWiC

Related

free(data) tries to free the wrong address

I have implemented a tree with the nodes looking like that:
typedef struct node_s {
struct node_s *parent;
struct node_s *left;
struct node_s *right;
data_t *data;
}node_t;
with *data pointing to a struct of data with a key which is used to sort the tree.
when I add a new node it looks like this:
node_t *add_Item (node_t *root){ //root is the root of the tree
...
data_t *new_data;
new_data = calloc(1,sizeof(data_t));
if(!new_data){
... //Error handling
}
root = insert_Node(root, new_data);
... //modifying new_data
return root;
}
node_t insert_Node(node_t *root, data_t *data){
... \\tree traversing and searching
if(root->data->key == data->key){ \\if the key is already in the tree it should be replaced
data_t *temp_data = root->data;
root->data = data;
free(temp_data); \\freeing the old data
}
...
return root;
}
What I don't understand is that the free(temp_data) doesn't work. Additionally the debug information doesn't make sense to me:
temp_data = 0x0000024C856DA950
root->data = 0x0000024C856DA9D0
and the debug message: HEAP[Program.exe]: Invalid address specified to RtlValidateHeap( 0000024C856D0000, 0000024C856DA920 )
The address in the error message is always 0x30 smaller than temp_data.
Just if it helps anybody: Windows 10, Visual Studio
EDIT- minimal reproducable example
#include <stdio.h>
#include <stdlib.h>
typedef struct data_s {
long key;
}data_t;
typedef struct node_s {
struct node_s* parent;
struct node_s* left;
struct node_s* right;
data_t* data;
}node_t;
node_t* init_node(data_t* data) {
node_t* new_node;
if ((new_node = calloc(1, sizeof(node_t)))) {
new_node->data = data;
fprintf(stderr, "node created\n");
}
return new_node;
}
node_t* insert_node(node_t* root, data_t* data) {
node_t* node;
if (root) {
if (root->data->key == data->key) {
printf("This item alreay exists!\n%ld will be replaced!\n", data->key);
data_t* temp_data;
temp_data = root->data;
root->data = data;
free(temp_data);
}
else if (root->data->key > data->key) {
node = insert_node(root->right, data);
root->right = node;
node->parent = root;
}
else {
node = insert_node(root->left, data);
root->left = node;
node->parent = root;
}
}
else {
root = init_node(data);
}
return root;
}
int main(int argc, char** argv) {
data_t* data;
node_t* root = 0;
while (1) {
if (!(data = calloc(1, sizeof(data_t)))) {
return(EXIT_FAILURE);
}
printf("What is the key for the new data?\n");
scanf("%ld", &(data->key));
root = insert_node(root, data);
//there would be more editing of data afterwards
}
return(EXIT_SUCCESS);
}
This seems to work but I don't know why this works and my other progam not.

Inorder traversal for binary search tree, shows some errors

This is the function which I am using for BST inorder Traversal
void inOrder(struct node* root)
{
if(root==NULL)
{
return;
}
inOrder(root->left);
printf("Key:%d,Pointer:%p\n",root->key,root);
inOrder(root->right);
}
The structure for the same is
struct node{
int key;
struct node *left,*right;
};
When I run the function I get two 0 extra elements for the following inserts:
root=insert(root,7);
insert(root,4);
insert(root,10);
insert(root,3);
insert(root,5);
insert(root,1);
insert(root,2);
insert(root,0);
insert(root,8);
insert(root,14);
insert(root,6);
insert(root,9);
insert(root,16);
insert(root,12);
insert(root,15);
insert(root,17);
inOrder(root);printf("\n");
And the insert function is given below:
struct node *insert(struct node * root,int ele)
{
struct node *temp=newNode(ele);
struct node *tree=root;
struct node *saver;
if(root==NULL)
{
root=temp;
}
else
{
while(tree!=NULL)
{
saver=tree;
if(ele<tree->key)
tree=tree->left;
else
tree=tree->right;
}
if(ele<saver->key)
{
saver->left=temp;
}
else
{
saver->right=temp;
}
}
return saver;
}
The output shows two 0s apart from the main elements, which I added. I have recently learned the Binary-Search-Tree, and I tried to implement it, Can someone please help me rectify this error. Thank you in advance
in insert
if(root==NULL)
{root=temp;
}
must be
if(root==NULL)
{
saver=temp;
}
because you return saver (not initialized in your version)
I suppose also in your main the code is not what you give but something like
struct node * root = NULL;
root=insert(root,7);
insert(root,4);
...
If I define newNode like that :
struct node * newNode(int ele)
{
struct node * r = malloc(sizeof(struct node));
r->key = ele;
r->left = r->right = NULL;
return r;
}
and I do the other changes :
pi#raspberrypi:/tmp $ gcc -g -pedantic -Wextra n.c
pi#raspberrypi:/tmp $ ./a.out
Key:0,Pointer:0xf04078
Key:1,Pointer:0xf04058
Key:2,Pointer:0xf04068
Key:3,Pointer:0xf04038
Key:4,Pointer:0xf04018
Key:5,Pointer:0xf04048
Key:6,Pointer:0xf040a8
Key:7,Pointer:0xf04008
Key:8,Pointer:0xf04088
Key:9,Pointer:0xf040b8
Key:10,Pointer:0xf04028
Key:12,Pointer:0xf040d8
Key:14,Pointer:0xf04098
Key:15,Pointer:0xf040e8
Key:16,Pointer:0xf040c8
Key:17,Pointer:0xf040f8
The full code is :
#include <stdio.h>
#include <stdlib.h>
struct node{
int key;
struct node *left,*right;
};
void inOrder(struct node* root)
{
if(root==NULL)
{return;}
inOrder(root->left);
printf("Key:%d,Pointer:%p\n",root->key,root);
inOrder(root->right);
}
struct node * newNode(int ele)
{
struct node * r = malloc(sizeof(struct node));
r->key = ele;
r->left = r->right = NULL;
return r;
}
struct node *insert(struct node * root,int ele)
{
struct node *temp=newNode(ele);
struct node *tree=root;
struct node *saver;
if(root==NULL)
{
saver=temp;
}
else
{
while(tree!=NULL)
{
saver=tree;
if(ele<tree->key)
tree=tree->left;
else
tree=tree->right;
}
if(ele<saver->key)
{
saver->left=temp;
}
else
{
saver->right=temp;
}
}
return saver;
}
int main()
{
struct node * root = NULL;
root=insert(root,7);
insert(root,4);
insert(root,10);
insert(root,3);
insert(root,5);
insert(root,1);
insert(root,2);
insert(root,0);
insert(root,8);
insert(root,14);
insert(root,6);
insert(root,9);
insert(root,16);
insert(root,12);
insert(root,15);
insert(root,17);
inOrder(root);printf("\n");
}

Code to insert node at last position in C programming language

I am writing a code on linklist in C programming language. When I am using an online compiler my code is working fine but when I am using Codeblock to run the code, the code is not working. I am posting the code kindly provide me solution. My code is to add node in a linklist on the last position.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* Head;
void insert(int);
void print();
int main()
{
Head = NULL;
insert(2);
insert(3);
insert(4);
print();
return 0;
}
void insert(int a)
{
struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));
temp1-> data = a;
temp1-> next = NULL;
if(Head == NULL)
{
Head = temp1;
return;
}
struct Node* temp = Head;
while(temp->next!= NULL)
{
temp = temp->next;
}
temp-> next = temp1;
}
void print()
{
struct Node* temp2=Head;
while(temp2 != NULL)
{
printf("%d \n", temp2->data);
temp2 = temp2->next;
}
return;
}
Here is how you can achieve what you want to do.
I suggest you to use a top-down approach when programming.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);
int main(int argc, char **argv)
{
struct node *head;
head = NULL;
head = Insert(head, 10);
head = Insert(head, 20);
Print_List(head);
Remove_List(head);
head = NULL;
return 0;
}
struct node *Create_New_Node(int);
struct node *Head_Insert(struct node *, int);
struct node *Queue_Insert(struct node *, int);
struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);
struct node *Insert(struct node *top, int elem)
{
if(top == NULL)
{
top = Head_Insert(top, elem);
}
else
{
top = Queue_Insert(top, elem);
}
return top;
}
struct node *Create_New_Node(int elem)
{
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
if(new_node != NULL)
{
new_node -> info = elem;
new_node -> next = NULL;
}
return new_node;
}
struct node *Head_Insert(struct node *top, int elem)
{
struct node *new_node = Create_New_Node(elem);
if(new_node != NULL)
{
new_node -> next = top;
}
return new_node;
}
struct node *Queue_Insert(struct node *top, int elem)
{
if(top != NULL)
{
if(top -> next != NULL)
{
top -> next = Queue_Insert(top -> next, elem);
}
else
{
struct node *new_node = Create_New_Node(elem);
if(new_node != NULL)
{
top -> next = new_node;
}
}
}
return top;
}
void Print_List(struct node *top)
{
while(top != NULL)
{
printf("\nInfo : %d\tAddress : %u\tNext link address : %u\n", top -> info, top, top -> next);
top = top -> next;
}
return;
}
void Remove_List(struct node *top)
{
if(top != NULL)
{
Remove_List(top -> next);
top -> next = NULL;
free(top);
}
return;
}
Sample output :
Info : 10 Address : 39149584 Next link address : 39149616
Info : 20 Address : 39149616 Next link address : 0

Add a left "leaf" to binary tree in C

I am learning the C language and trying to build a C binary tree library. My structure is defined as:
struct Node {
int value;
struct Node *left;
struct Node *right;
};
typedef struct Node TNode;
typedef struct Node *binary_tree;
It is created like this:
binary_tree NewBinaryTree(int value_root) {
binary_tree newRoot = malloc(sizeof(TNode));
if (newRoot) {
newRoot->value = value_root;
newRoot->left = NULL;
newRoot->right = NULL;
}
return newRoot;
}
I add elements to it like:
void Insert(binary_tree *tree, int val) {
if (*tree == NULL) {
*tree = (binary_tree)malloc(sizeof(TNode));
(*tree)->value = val;
(*tree)->left = NULL;
(*tree)->right = NULL;
} else {
if (val < (*tree)->value) {
Insert(&(*tree)->left, val);
} else {
Insert(&(*tree)->right, val);
}
}
}
This kinda "sort" the tree by checking the value.However I would like a function that simply add the val value to the left(left) without doing any sorting. I did:
void InsertLeft(binary_tree *tree, int val) {
Insert(&(*tree)->left, val);
}
I call the function like this:
InsertLeft(&tree, 8);
I have a main program that initialise the tree like this:
tree = NewBinaryTree(3);
Also a tree display method:
void display_binary_tree( binary_tree tree )
{
if( tree != NULL )
{
display_binary_tree( tree->left ) ;
printf( "%d " , tree->value ) ;
display_binary_tree( tree->right ) ;
}
}
and afterwards I can add elements by calling the Insert(binary_tree *tree, int val) function, however when calling this function, it just crashes the program. I dont understand why.

segmenation fault error in my linkedList -C

I put together a few pieces of code to make a linked list that adds to head(Has a special function) and in the middle(also special function).
my problem is, i need to provide the program with numbers and insert them as nodes in my LINKEDLIST. However, my display function(to display the tree of nodes) gives back segmentation fault and so does just taking values in without any display function.
I'm fairly new to malloc so i suspect the problem is there?
Thanks
#include<stdio.h>
#include<stdlib.h>
/*LINKEDLIST STRUCT*/
struct node {
int data;
struct node *next;
};
/*Inserting head-Node*/
struct node *insert_head(struct node *head, int number)
{
struct node *temp;
temp = malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Not enough memory\n");
exit(1);
}
temp->data = number;
temp->next = head;
head = temp;
return head;
}
/*Inserting inside a list*/
void after_me(struct node *me, int number)
{
struct node *temp;
temp = malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Not enough memory\n");
exit(1);
}
temp->data = number;
temp->next = me->next;
me->next = temp;
}
/*PRINTING LIST*/
void display(struct node *head)
{
struct node *moving_ptr = head;
while(moving_ptr != NULL)
{
printf("%d-->",moving_ptr->data);
moving_ptr = moving_ptr->next;
}
}
int main()
{
int index;
struct node *head;
struct node *previous_node;
scanf("%d", &index);
while(index > 0)
{
/*allocating in List */
if(head == NULL)
head = insert_head(head,index);
else
if((head != NULL) && (index <= (head->data)))
{
struct node *temp;
head->next = temp;
temp->next = head;/*TRY INSERT HEAD FUNC.*/
}
else
if((head != NULL) && (index > (head->data)))
{
previous_node->data = index-1;
after_me(previous_node,index);
}
scanf("%d", &index);
}
display(head);
}
I suggest as follows.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
//aggregated into one place
struct node *new_node(int number){
struct node *temp;
if(NULL == (temp = malloc(sizeof(*temp)))){
printf("\nNot enough memory\n");
exit(1);
}
temp->data = number;
temp->next = NULL;
return temp;
}
struct node *insert_head(struct node *head, int number) {
struct node *temp = new_node(number);
temp->next = head;
return temp;
}
void after_me(struct node *me, int number){
struct node *temp = new_node(number);
temp->next = me->next;
me->next = temp;
}
void display(struct node *head){
struct node *moving_ptr = head;
while(moving_ptr != NULL){
printf("%d", moving_ptr->data);
if(moving_ptr = moving_ptr->next)
printf("-->");
}
putchar('\n');
}
struct node *insert(struct node *me, int number){
if(me){
if(number <= me->data){
me = insert_head(me, number);
} else {
me->next = insert(me->next, number);
}
} else {
me = new_node(number);
}
return me;
}
void release(struct node *list){//Of course, you will be able to replace a simple loop(e.g while-loop).
if(list){
release(list->next);
free(list);
}
}
int main(void){
struct node *head = NULL;
int index;
while(1==scanf("%d", &index) && index > 0){
head = insert(head, index);
}
display(head);
release(head);
return 0;
}

Resources