I am getting a segmentation fault when trying to print the nodes in my binary tree. It looks to be an issue with the third node. I have searched google and stack overflow for hours but I can not understand what the problem is. I am trying to teach myself data structures in C and am very much a novice so I may be doing something in a frowned upon way.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} Node;
typedef struct
{
Node *root;
} BinarySearchTree;
void printInOrder(Node *);
void addNode(Node *, Node *);
int main (void)
{
BinarySearchTree tree;
BinarySearchTree *tree_ptr = &tree;
Node n1, n2, n3;
n1.data = 1;
n2.data = 2;
n3.data = 3;
Node *n1_ptr = &n1;
Node *n2_ptr = &n2;
Node *n3_ptr = &n3;
tree_ptr->root = n1_ptr;
addNode(tree_ptr->root, n2_ptr);
addNode(tree_ptr->root, n3_ptr);
printInOrder(tree_ptr->root);
}
void printInOrder(Node *root)
{
if (root == NULL)
{
return;
} else
{
printInOrder(root->left);
printf("%i\n", root->data);
printInOrder(root->right);
}
}
void addNode(Node *root, Node *node)
{
if (node->data < root->data)
{
if (root->left == NULL)
{
root->left = node;
} else
{
addNode(root->left, node);
}
}
else if (node->data > root->data)
{
if (root->right == NULL)
{
root->right = node;
} else
{
addNode(root->right, node);
}
}
}
output:
1
2
Segmentation fault: 11
There doesn't seem to be an issue with any but the third node. If I comment out the line that adds the second node I get the same error (with only 1 being printed, obviously).
Your initialization is incomplete
n1.data = 1;
n2.data = 2;
n3.data = 3;
should also set the pointers
n1.data = 1;
n1.left = NULL;
n1.right = NULL;
n2.data = 2;
n2.left = NULL;
n2.right = NULL;
n3.data = 3;
n3.left = NULL;
n3.right = NULL;
Problem is occurring because you are not initializing all the member of structure Node type variable.
I would suggest, you should write a function to initialize the Node type variable, like this:
void init_node(Node * nodeptr, int data)
{
nodeptr->data = data;
nodeptr->left = NULL;
nodeptr->right = NULL;
}
and in your main() (or from where ever you want to initialize) you can simply do:
init_node(&n1, 1);
init_node(&n2, 2);
init_node(&n3, 3);
With this, you will never miss assigning NULL to left and right pointer during initialization of Node type variable and chances of the error occurring because of it will be reduced to a greater extent.
Related
For homework I was given this code with the directive to implement a recursive function that calls itself on the next node in the list in main unless the current node is NULL or the value of the current node is equal to 'target'
My recent attempt is the fenced part of the code below. I can get it to print 0, but that's not the whole list. I'm not sure what I'm doing wrong as I've not had much experience with linked lists and nodes.
typedef struct node {
struct node* next;
unsigned short index;
char* value;
} node;
node* create_node(unsigned short index, char* value) {
node* n = malloc(sizeof(node));
n->value = value;
n->index = index;
n->next = NULL;
return n;
}
node* create_nodes(char* values[], unsigned short index, unsigned short num_values) {
if(num_values == 0) {
return NULL;
}
node* n = create_node(index, values[0]);
n->next = create_nodes(values + 1, index + 1, num_values - 1);
return n;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node* find_match(node* cur, char* target) {
if(cur == NULL){
return NULL;
}
if(cur->value != NULL){
node* result = create_node(cur->index, cur->value);
result->next = find_match(cur->next, cur->value);
}else{
return find_match(cur->next, cur->value);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int main() {
char* values[] = {
"foo", // 0
"bar", // 1
"baz", // 2
"duh", // 3
"dunk", // 4
"derp" // 5
};
node* head = create_nodes(values, 0, 6);
node* target = find_match(head, "dunk");
printf("%d\n", target->index);
return 0;
}
No error messages were given, except a prior segmentation fault I've already 'fixed' but I think it's supposed to print the whole list.
You can insert a single element at a time and send the each element using loop because you know the size of array.Then you have to little change in your code.
struct linkList {
int data;
linkList* next;
}node;
node create(int val){
node tmp;
tmp = (node)malloc(sizeof(struct linkList));
tmp->data = val;
return tmp;
}
node* insertNodeAtHead(linkList* llist,int data) {
node tmp;
tmp = create(data);
tmp->next = llist;
return tmp;
}
Then you can search with your Key just like printing the all element in the List
void print(linkList* head) {
while(head !=NULL){
printf("%d\n",head->data); // check here is this your key or Not
head = head->next;
}
}
But this question is known and before posting any question make sure you try and Search enough in Google !!. Hope you get the idea and implement it in your own way.
There are a few issues in the code.
The problem is in the findmatch function. In this as per the problem statement, the target node should be returned if it is present, else NULL should be returned. This can be achieved as below.
node* find_match(node* cur, char* target) {
if(cur == NULL){
return NULL;
}
if(strcmp(cur->value,target) ==0){
return (cur);
}else if (cur->value != NULL){
return find_match(cur->next, target);
}
else {
return NULL;
}
}
Additional points
In the create_node function you are directly copying the string pointer. This may work in this specific case, but you should ideally allocate memory for the value field separately.
node* create_node(unsigned short index, char* value) {
node* n = malloc(sizeof(node));
n->value = strdup(value);
n->index = index;
n->next = NULL;
return n;
}
While printing the value, you should check if the returned value from findmatch is NULL
node* target = find_match(head, "dunk");
if (target != NULL) {
printf("%d\n", target->index);
}
else {
printf (" Not found\n");
}
I am working with the following code I found on here to begin my project that involves a tree data structure.
struct node{
int ID;
struct node* next;
struct node* child;
};
typedef struct node node;
node* new_node(int);
node* add_sibling(node*, int);
node* add_child(node*, int);
int main()
{
int i;
node *root = new_node(0);
for (i = 1; i <= 3; i++)
add_child(root, i);
}
node * new_node(int ID)
{
node* new_node =(node*) malloc(sizeof(node));
if (new_node) {
new_node->next = NULL;
new_node->child = NULL;
new_node->ID = ID;
}
return new_node;
}
node* add_sibling(node* n, int ID)
{
if (n == NULL)
return NULL;
while (n->next)
n = n->next;
return (n->next = new_node(ID));
}
node* add_child(node* n, int ID)
{
if (n == NULL)
return NULL;
if (n->child)
return add_sibling(n->child, ID);
else
return (n->child = new_node(ID));
}
I am a beginner with C/C++ and programming in general. I think I understand everything about the code except for the add_child function. The function seems to return a pointer to a node, but when it is called in main, it seems to be called as though it were a void function. I would have thought to call the function this way
*root = add_child(root,i);
like how new_node is called, or to write add_child as a void function, but both of these modifications results in errors (not to mention the implementation in the code I found does work). What am I missing?
This function rewritten will look like following.
node* add_child(node* n, int ID)
{
// For null pointer return null
if (n == NULL)
{
return NULL;
}
// If element has child add a sibling to that child
if (n->child)
{
node* sibling_for_child = add_sibling(n->child, ID);
return sibling_for_child;
}
else
{
// Otherwise just add element as a child
node* child_node = new_node(ID);
n->child = child_node;
return child_node;
}
}
The result of assignment is an assigned value(*) like in this chaining assignment:
int a, b;
a = b = 3;
which is actually:
a = (b = 3);
or
b = 3;
a = b;
I'm creating a binary tree in C in which I use indexes as in a binary search tree. I'm having trouble with a function that finds a node.
My code for the tree is in a separate file and it's the following:
struct node {
struct node *left;
struct node *right;
int index;
void *data;
};
struct node * init_mt()
{
struct node *root = malloc(sizeof(struct node));
root->index = -1;
root->data = NULL;
root->left = root->right = NULL;
return root;
}
void create_mt(struct node *mt, int h, int i)
{
if (h == 0) // end of tree reached
{
mt->index = 0;
mt->left = mt->right = NULL;
return;
}
else
{
mt->index = i;
int l = i-pow(2,h-1);
int r = i+pow(2,h-1);
mt->left = malloc(sizeof(struct node));
mt->right = malloc(sizeof(struct node));
create_mt(mt->left,h-1,l);
create_mt(mt->right,h-1,r);
}
}
struct node * get_node_mt(struct node *mt, int p, int h)
{
if (h == -1) // end of tree reached
{
return NULL;
}
else
{
if (mt->index == p)
{
printf("Found %p\n", mt);
return mt;
}
else if (mt->index > p)
{
get_node_mt(mt->left,p,h-1);
}
else
{
get_node_mt(mt->right,p,h-1);
}
}
}
When I run my main like this (root->left->left is the node index #2):
struct node *root = NULL;
root = init_mt();
create_mt(root,3,8); // root index == 8
printf("%d %p\n", root->left->left->index,root->left->left);
struct node *find;
find = get_node_mt(root,2,3);
printf("%p\n", find);
I get the following output:
2 0x7ffd1dd011a0
Found 0x7ffd1dd011a0
0x0
I don't understand why I get the right pointer both when priting directly using root->left->left and inside the function get_node_mt() but not when I print the pointer I've returned. What am I doing wrong?
The get_node_mt() function calls itself in two places. Neither of these two places return a value.
struct node * get_node_mt(struct node *mt, int p, int h)
{
if (h == -1) // end of tree reached
return NULL;
else if (mt->index == p)
return mt;
else if (mt->index > p)
get_node_mt(mt->left,p,h-1);
else
get_node_mt(mt->right,p,h-1);
}
I reformatted the code a bit and removed a lot of braces. The compiler should've warned about this problem, IMHO.
I'm implementing an binary search tree but for some reasons I 'm not able to add a node
my: input was :
a.value = 5;
add_bst_node(&t,a);
mystructures:
typedef struct BST_node{
entity value;
struct BST_node* left;
struct BST_node* right;
}BST_node;
typedef struct BST_tree{
BST_node* root;
}BST_tree;
my code for add a node:
void add_bst_node2(BST_node* root,entity* e){
if(!root){
root = (BST_node*)malloc(sizeof(BST_node));
root->value = *e;
root->left = NULL;
root->right = NULL;
return;
}
else if(great_than(&root->value,e))
add_bst_node2(root->left,e);
else
add_bst_node2(root->right,e);
}
void add_bst_node(BST_tree* t,entity e){
add_bst_node2(t->root,&e);
printf("%d\n",t->root==NULL);
}
Someone can explayn why I'can't add a node?
Apart from not passing double pointer to BST_node (i.e. BST_node**) in add_bst_node2() as noted in the comments, you also didn't implement the function properly.
Your implementation never really adds a node, but instead in enters into infinite recursion.
Here you can find some clean theory about BST - http://www.zentut.com/c-tutorial/c-binary-search-tree/
Here is an untested correction of your code. Note that here we pass pointer to BST_tree instead of BST_node
void add_bst_node2(BST_tree* tree,entity* e){
if(!tree->root){
/* If the binary search tree is empty, we just create a root node */
tree->root = bst_create_node(e);
return;
}
int is_left = 0;
BST_node* current_node = tree->root;
BST_node* prev = NULL;
/* Traverse the tree until we find the proper position for the new node.
* The position is denoted by 'current_node'
*/
while(current_node != NULL) {
prev = current_node;
if(greater_than(¤t_node->value, e)) {
is_left = 1;
current_node = current_node->left;
} else {
is_left = 0;
current_node = current_node->right;
}
}
/* We finally know the position where we should add the new node */
if(is_left)
prev->left = bst_create_node(e);
else
prev->right = bst_create_node(e);
}
We introduce another function for creating and initializing a node...
BST_node *bst_create_node(entity *e)
{
BST_node *n = malloc(sizeof(BST_node));
n->value = *e;
n->left = NULL;
n->right = NULL;
return n;
}
And finally we change add_bst_node()
void add_bst_node(BST_tree* t,entity e){
add_bst_node2(t, &e);
printf("%d\n", t->root==NULL);
}
From what it seems, a is a struct BST_node and value is a variable in it. You have to either pass the value to the function and handle the node creation there, or pass the whole constructed node and just point to it from the existing tree.
first thing is that you put an unnecessary structure BST_tree.You do it in simple way like
struct node
{
int value;
node* left;
node* right;
};
struct node* root;
I suggest you try with this code
struct node* insert(struct node* r, int data)
{
if(r==NULL) // BST is not created created
{
r = (struct node*) malloc(sizeof(struct node)); // create a new node
r->value = data; // insert data to new node
// make left and right childs empty
r->left = NULL;
r->right = NULL;
}
// if the data is less than node value then we must put this in left sub-tree
else if(data < r->value){
r->left = insert(r->left, data);
}
// else this will be in the right subtree
else {
r->right = insert(r->right, data);
}
return r;
}`
`
i am writing a Dictionary using linked list in C, and all my functions work except my delete function, which is shown below along with all other necessary code. Every time i try to run my program as soon as it reaches a line in which it must delete a node, it gives me the error: Segmentation Fault (core dumped) which means it has something to do with the memory allocation or a null pointer i think. I know that the rest of my code works. All and any help is appreciated! :)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include"Dictionary.h"
// NodeObj
typedef struct NodeObj{
char* key;
char* value;
struct NodeObj* next;
} NodeObj;
// Node
typedef NodeObj* Node;
// newNode()
// constructor of the Node type
Node newNode(char* key, char* value)
{
Node N = malloc(sizeof(NodeObj));
assert(N!=NULL);
// if(key!=NULL && value!=NULL){
N->key = key;
N->value = value;
N->next = NULL;
// }
return(N);
}
// DictionaryObj
typedef struct DictionaryObj{
Node head;
int numItems;
} DictionaryObj;
// newDictionary()
// constructor for the Dictionary type
Dictionary newDictionary(void){
Dictionary D = malloc(sizeof(DictionaryObj));
assert(D!=NULL);
D->head = NULL;
D->numItems = 0;
return D;
}
Node findKey(Dictionary D, char*key){
Node N;
N = D->head;
while(N != NULL){
if(strcmp(N->key,key)==0){
return N;
}
N = N->next;
}
return NULL;
}
char* lookup(Dictionary D, char* k){
if(findKey(D, k)==NULL){
return NULL;
}else{
Node N;
N = findKey(D, k);
return N->value;
}
}
void delete(Dictionary D, char* k)
{
if(lookup(D,k) == NULL){
fprintf(stderr,
"KeyNotFoundException: Cannot delete non-existent key\n");
exit(EXIT_FAILURE);
}
int check = strcmp(D->head->key, k);
if(check == 1){
D->head = D->head->next;
return;
}
Node cur;
Node prev;
cur = D->head;
prev = NULL;
while( cur != NULL){
int ret1;
ret1 = strcmp(cur->key, k);
while( ret1 == 0){
prev = cur;
cur = cur->next;
}
}
prev->next = cur->next;
D->numItems--;
}
The NodeObject should store copy of the string and care for deleting it:
typedef struct Node Node;
struct Node {
Node *next;
char *key, *value;
};
Node* newNode(char* key, char* value) {
assert(key && value);
Node* node = (Node*)malloc(sizeof(Node));
assert(node);
node->next = NULL;
node->key = strdup(key);
node->value = strdup(value);
}
void delNode(Node* node) {
free(node->key);
free(node->value);
}
Consider using the original code (without that strdup) in this scenairo:
Node* prepare() {
char key_buf[20]; strcpy(key_buf, "mykey");
char val_buf[20]; strcpy(val_buf, "myval");
return newNode(key_buf, val_buf);
}
void examine(Node* node) {
printf("Node key=%s value=%s\n", node->key, node->value);
}
int main() {
examine(prepare());
}
the above code would crash because Node would have pointers to stack (in your case without that strdup), but key_buf+val_buf were only valid inside prepare() (garbage outside and therefore inside examine() - node->key points to random data).