Binary search tree insertion (C) - c

Thanks, now for some reason it's not working as intended. When I run the program it just gives an error "bst.exe has stopped working" and it happens in this function.
static NODE *insert_i(NODE *r, int x)
{
NODE *leaf;
while(r)
{
if(r->val == x)
return r;
if(x < r->val && r->left != NULL)
r = r->left;
else if(x > r->val && r->right != NULL)
r = r->right;
}
leaf = malloc(sizeof(NODE));
leaf->left = NULL;
leaf->right = NULL;
leaf->val = x;
count++;
if(x < r->val)
r->left = leaf;
else
r->right = leaf;
return r;
}
void bst_insert(BST_PTR t, int x)
{
t->root = insert_i(t->root, x);
}

You have
while(r)
{
if(r == NULL)
The if condition will never be true, as if r is NULL then the loop will end, without returning anything from the function.

What will happen if you while loop is not entered or exited without returning? It will not return anything and behaviour will be undefined.
So, return NULL to indicate that not found or move if(r==NULL) out of the loop. It will not be executed inside the loop.

Related

binary search tree delete function deleting the same thing twice

I am trying to create a binary search tree that deletes the 2 leftmost nodes in a bst. For some reason, my code deletes the first value twice instead of moving onto the next.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct treeNode {
char *word; // the string word that is stored
int origin; // position of the word in the original text input’s line 1
struct treeNode *left; // pointer for the left children of the node
struct treeNode *right; // pointer for the right children of the node
struct treeNode *parent; // pointer for the parent of the node
};
typedef struct treeNode NODE;
NODE *
addNode(NODE * r, char *x)
{
// r = root pointer of the tree
// x = value to add into this tree
// return the root of the updated tree
NODE *p = malloc(sizeof(NODE));
p->word = malloc(strlen(x) + 1);
strcpy(p->word, x); // strcpy input: (destination, data to be copied)
// printf("%s", x);
p->parent = NULL;
p->left = NULL;
p->right = NULL;
// if tree is empty, tree consists of p only
if (r == NULL)
return p;
if (strcmp(x, r->word) > 0) {
r->right = addNode((r->right), x);
return r;
}
else {
// add new node the left subtree
r->left = addNode((r->left), x);
return r;
}
return r;
}
NODE *
getLeftMostNode(NODE * root)
{
// return the pointer to the right most node
if (root == NULL)
return NULL;
NODE *p = root;
while (p->left != NULL)
p = p->left;
return p;
}
NODE *
addTree2Tree(NODE * X, NODE * Y)
{
if (X == NULL)
return Y;
if (Y == NULL)
return X;
X = addNode(X, Y->word);
X = addTree2Tree(X, Y->left);
X = addTree2Tree(X, Y->right);
return X;
}
NODE *
removeNode(NODE * r)
{
// remove any node that store value x from tree
// r: root pointer of this tree
// return root pointer of the updated tree after removal
NODE *p = getLeftMostNode(r);
NODE *C = p->parent;
NODE *A = p->left;
NODE *B = p->right;
if (C == NULL) {
// p is root of the tree
free(p);
return addTree2Tree(A, B); // add tree A and tree B and return the new combination tree
}
if (A != NULL) {
// make A a child of C assuming position of P
if (p == C->left)
C->left = A;
else
C->right = A;
A->parent = C;
free(p);
return addTree2Tree(r, B);
}
if (B != NULL) {
if (p == C->left)
C->left = B;
else
C->right = B;
B->parent = C;
free(p);
return r;
}
if (p == C->left)
C->left = NULL;
else
C->right = NULL;
free(p); // free allocation for p
return r;
}
void
printArray(NODE * r)
{
// print all the values on the tree rooted at node "r" // print in alphabetical order
// if the tree is empty, return // print all the values on the tree rooted at node "r" // print in the in-order order: print left first, followed by root, followed by right values
if (r == NULL) {
return;
}
else {
printArray(r->left); // print all values in left subtree
printf("%s ", r->word);
printArray(r->right); // print all values in right subtree
}
}
int
main()
{
// input must be implemented by linked list, not array
NODE *root = NULL;
int ch;
char in[1000]; // input array
int len = 0;
char del[100]; // word to be deleted
char word[1000];
while ((ch = getchar()) != '\n')
in[len++] = ch;
in[len] = '\0';
// printf("%s\n", in);
int i = 0,
j = 0;
while (i <= len) {
// space to store a word
if ((in[i] == ' ') || (in[i] == '\0') || (in[i] == '\t')) {
word[j] = '\0'; // end of word
j = 0;
root = addNode(root, word);
// printf("%s\n", word);
}
else
word[j++] = in[I];
i++;
}
int k = 0;
removeNode(root);
removeNode(root);
printArray(root);
printf("\n");
return 0;
}
this is the error that I got
The function removeNode is looking for parent, but parent is never assigned in addNode. You want to assign r->right->parent = r; and r->left->parent = r;.
BST doesn't keep duplicate keys. If strcmp(x, r->word) == 0, then don't add a new node.
addNode should also be corrected so that if r is NULL, the function returns the new node immediately.
NODE* addNode(NODE* r, char* x)
{
if(!x)
return NULL;
if (!r)
{
NODE* p = malloc(sizeof(NODE)); if (!p) return NULL;
p->word = strdup(x);
p->parent = NULL;
p->left = NULL;
p->right = NULL;
return p;
}
if (strcmp(x, r->word) > 0)
{
r->right = addNode((r->right), x);
r->right->parent = r;
return r;
}
else if (strcmp(x, r->word) < 0)
{
r->left = addNode((r->left), x);
r->left->parent = r;
return r;
}
return r;
}
Modify the insert functions such that root is assigned only once:
while (i <= len)
{
if ((in[i] == ' ') || (in[i] == '\0') || (in[i] == '\t'))
{
word[j] = '\0';
j = 0;
if(!root)
root = addNode(root, word);
else
addNode(root, word);
}
else
word[j++] = in[i];
i++;
}
Double check the pointers to make sure NULL pointers are avoided. For example:
NODE* removeNode(NODE* r)
{
if(!r)
return NULL;
NODE* p = getLeftMostNode(r);
if(!p)
return NULL;
...
}
I have not checked the rest of the code but the example will work with these changes made.

Faulty function that finds a node in a binary tree

There is something wrong with this function. It's supposed to find a node with the same value phone. I believe it's got problems when it tries to find a node that that doesn't exist.
Here it is:
bst_node* find_node(bstree* bst, unsigned long phone) {
bst_node* x = bst->root;
if(x == NULL)
return NULL;
if(x->phone == phone)
return bst->root;
while (x->phone != phone && (x->left != NULL && x->right != NULL) ) {
if(phone <= x->phone) {
x = x->left;
} else {
x = x->right;
}
}
if (x->phone == phone) {
return x;
} else {
return NULL;
}
}
Basically your problem was in your while loop. Remember that you can't guarantee that every node will have two children, some nodes may only have one child and you were not exploring those nodes that only had one child. What I do below is separate out the check for the left and right node existence into the conditionals within the while loop. This lets us explore the whole tree :) I also added in an else statement within the while loop because if the node has no children then we have finished exploring and there is no way the node can be in the tree.
bst_node* find_node(bstree* bst, unsigned long phone) {
bst_node* x = bst->root;
if(x == NULL)
return NULL;
if(x->phone == phone)
return bst->root;
while (x->phone != phone){
if(phone <= x->phone && x->left != NULL){
x = x->left;
}
else if (phone > x->phone && x->right != NULL){
x = x->right;
}
else {return NULL;}
return x;
}

Fibonacci Sequence using Linked List crashes after linked list is destroyed. How can I trace the error?

I'm working on a program that calculates any number in the Fibonacci Sequence without using the int data type since it would overflow. Instead I am using linked lists to hold the digits that represent each number. My current issue is with freeing memory allocated to linked lists that I no longer need. If I'm calculating F(10000), I'd like the thousands of previous lists to be freed. The program as is produces each value up to "F(7) = 13" before crashing and showing "exit status -1". I'd really just like to know what's causing this error and go from there. Any help is appreciated. Thank you and I apologize for the large amount of code.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int digit;
struct Node *next;
} Node;
typedef struct ListyInt
{
Node *head;
int length;
} ListyInt;
Node *create_node(unsigned int digit, ListyInt *listy);
Node *removeNode(Node *node, ListyInt *listy);
void listyPrintHelper(Node *current);
ListyInt *destroyListyInt(ListyInt *listy);
ListyInt *fib(unsigned int n);
void listyPrint(ListyInt *p)
{
if (p == NULL || p->head == NULL)
{
printf("(null pointer)\n");
return;
}
listyPrintHelper(p->head);
printf("\n");
}
void listyPrintHelper(Node *current)
{
if (current == NULL)
return;
listyPrintHelper(current->next);
printf("%d", current->digit);
}
int main()
{
int i;
ListyInt *p;
for (i = 0; i <= 1000; i++)
{
printf("F(%d) = ", i);
listyPrint(p = fib(i));
destroyListyInt(p);
}
return 0;
}
ListyInt *listyAdd(ListyInt *p, ListyInt *q)
{
ListyInt *listy = NULL;
Node *ptemp = NULL;
Node *qtemp = NULL;
Node *temp = NULL;
ListyInt *temp_list = NULL;
unsigned int x = 0;
unsigned int count = 0;
if (p == NULL || q == NULL)
{
return NULL;
}
listy = malloc(sizeof(ListyInt));
if (listy == NULL)
{
return NULL;
}
listy->length = 0;
if (q->length > p->length)
{
temp_list = q;
q = p;
p = temp_list;
}
while (count < p->length)
{
if (count == 0)
{
x = p->head->digit + q->head->digit;
ptemp = p->head->next;
qtemp = q->head->next;
listy->head = create_node(x, listy);
temp = listy->head;
temp->next = create_node(0, listy);
if (temp->digit > 9)
{
temp->digit = temp->digit - 10;
temp->next->digit = temp->next->digit + 1;
}
}
else
{
temp->next->next = create_node(0, listy);
if (qtemp == NULL)
{
temp->next->digit += ptemp->digit;
ptemp = ptemp->next;
temp = temp->next;
}
else
{
x = ptemp->digit + qtemp->digit;
temp->next->digit += x;
if (temp->next->digit > 9)
{
temp->next->digit = temp->next->digit - 10;
temp->next->next->digit = temp->next->next->digit + 1;
}
qtemp = qtemp->next;
ptemp = ptemp->next;
temp = temp->next;
}
}
if (count == p->length - 1 && temp->next->digit == 0)
{
temp->next = removeNode(temp->next, listy);
}
count++;
}
return listy;
}
ListyInt *destroyListyInt(ListyInt *listy)
{
if (listy == NULL)
{
return NULL;
}
Node *current = listy->head;
Node *temp;
while (current != NULL)
{
temp = current->next;
free(current);
current = temp;
}
free(listy);
return NULL;
}
ListyInt *fib(unsigned int n)
{
ListyInt *spiral = malloc(sizeof(ListyInt));
ListyInt *p = NULL;
ListyInt *q = NULL;
unsigned int count = 2;
if (spiral == NULL)
{
return NULL;
}
if (n == 0)
{
spiral->head = create_node(0, spiral);
return spiral;
}
if (n == 1)
{
spiral->head = create_node(1, spiral);
return spiral;
}
p = malloc(sizeof(ListyInt));
p->head = create_node(0, p);
q = malloc(sizeof(ListyInt));
q->head = create_node(1, q);
while (count <= n)
{
spiral = listyAdd(p, q);
destroyListyInt(p);
p = q;
q = spiral;
count++;
}
return spiral;
}
Node *create_node(unsigned int digit, ListyInt *listy)
{
if (listy == NULL)
{
return NULL;
}
Node *new_node = malloc(sizeof(Node));
new_node->digit = digit;
new_node->next = NULL;
listy->length++;
return new_node;
}
Node *removeNode(Node *node, ListyInt *listy)
{
if (node == NULL)
{
return NULL;
}
if (listy == NULL)
{
return NULL;
}
free(node);
node = NULL;
listy->length--;
return NULL;
}
I'd really just like to know what's causing this error and go from there.
Long story short, C's equivalent of a NullPointerException, as far as I can tell.
Long story longer, I haven't had time to fully examine your code or debug it, but I have had time to run it through gdb, which is included with most Linux installations. If you're using Visual Studio, I vaguely remember there being a debug mode, which should show you roughly the same information, just in a different place. This is GDB's output:
Starting program: /home/ubuntu/C/a.out
F(0) = 0
F(1) = 1
F(2) = 1
F(3) = 2
F(4) = 3
F(5) = 5
F(6) = 8
F(7) = 13
Program received signal SIGSEGV, Segmentation fault.
0x00000000004008db in listyAdd (p=0x6036a0, q=0x6036e0) at main.c:117
117 temp->next->digit += ptemp->digit;
(Okay, that's not all of it, but that's the relevant bit.)
What those last three lines mean is that you got a segfault. There are a bunch of things that can cause it, but based on that line, it looks like it's been caused by trying to dereference an invalid pointer. That's either a NULL pointer (a value of 0x0) or a pointer you've already freed.
If you're on Linux, you can then run Valgrind on it to figure out what, exactly, happened. It'll tell you if it's using a freed pointer or a NULL one, and that'll give you a good starting point to find the actual bug. You can also use your IDE's debugger (or GDB, if you want to try playing with the command-line version, but I wouldn't recommend it) to step through your program and see what the values of the variables involved are, which you can walk backwards from to see where they're being changed and invalidated.
If I had to guess, though, I'd say 0andriy's comments hit it on the nose -- you seem to be freeing things twice, and you probably meant to free them once, at the end.
I'm somewhat intentionally leaving this vague. Segfaults are common and (as you've noticed) difficult to debug, and you can only really learn how through experience. I think being shown the answer would honestly be less helpful than working through it yourself, and with tools like Valgrind and your debugger, that's not actually that hard, just tedious.
I found the issue. It was because the length of my linked lists weren't being reset when fib() was called. Thanks for your help everyone.

return is not breaking my recursive function in c

struct Node *xFromEnd(struct Node *pHead, int x)
{
static int temp = 1;
if (pHead->next != NULL)
xFromEnd(pHead->next, x);
if ((temp++) == x)
return pHead;
}
How do I break out of the function when the condition is met? The return is just going further up on the call stack(going to its previous function call) rather than exiting and going to main. How can I do that?
struct Node *xFromEnd(struct Node *pHead, int x){
static int temp = 1;//but can not reset !!
if(pHead == NULL)
return NULL;
struct Node *p = xFromEnd(pHead->next, x);
if (p == NULL){
return (x == temp++) ? pHead : NULL;
}
return p;
}
I think than your function never break because in your function you have an increment of temp variable which return true value, and not the value of temp variable.
Try :
temp++;
if (temp == x)
return pHead;

warning: control reaches end of non-void function. C Binary Search Tree

I have an insert function for a Binary Search Tree and i can not find out why i am getting that error. Even if i put a return FALSE; right before the function ends it still happens. Any help is appreciated.
boolean insert(NODE **root, Employee e){
NODE *cursor,*temp;
// boolean status;
temp = (NODE *)malloc(sizeof(NODE));
assert(temp != NULL);
temp->element = (Employee *)malloc(sizeof(Employee));
assert(temp -> element != NULL);
*(temp -> element) = e;
temp -> left = NULL;
temp -> right = NULL;
if (*root == NULL) {
*root = temp;
return TRUE;
}
// tree is non-empty
cursor = *root;
while (cursor != NULL) {
if(e.ID < cursor -> element -> ID){
if(cursor -> left == NULL){
cursor -> left = temp;
return TRUE;
}
else cursor = cursor -> left;
}
//e goes to the right
else {
if (e.ID > cursor -> element -> ID){
if(cursor -> right == NULL){
cursor -> right = temp;
return TRUE;
}
else
cursor = cursor -> right;
}
else { // e is already in the tree
free(temp -> element);
free(temp);
return FALSE;
}
}
} // while cursor != NULL
} // insert
Even though a function actually does return something on all possible paths, the compiler can't necessarily determine that - to be able to do so in all cases would effectively prove the halting problem to be solvable. That's why this diagnostic is a warning, not an error.
Here's an example of a function that is easily seen to always return 0, but many compilers that try to warn you about unhandled return paths will issue the warning:
int foo( int x)
{
while (x >0) {
--x;
}
if (x == 0) return 0;
while (x < 0) {
++x;
}
if (x == 0) return 0;
}
However sometimes a simple analysis can determine that all control paths do return a value. The following will typically not generate a diagnostic:
int bar(int x)
{
if (x == 0)
return 0;
else
return 1;
}
In a function as complex as yours, it can be very difficult anyone (compiler or human) to determine that the while() loop never terminates except via a return inside it. You may want to add an assertion that always fails after the while if you expect that to be the case. Also you might consider changing the while (cursor != NULL) to for (;;) which 'documents' that this is a loop that will only end because of a return or break statement inside (and maybe have an assert(cursor != NULL) at the top).
Just for the record, putting a return FALSE; just before the end of your function does silence the warning.

Resources