I am implementing a function for the node removal from the binary search tree.
The prototype of the function is set and I can't change it, it is a school assignment.
My code:
typedef struct tBSTNode {
char Key;
struct tBSTNode * LPtr;
struct tBSTNode * RPtr;
} *tBSTNodePtr;
void BSTDelete (tBSTNodePtr *RootPtr, char K) {
tBSTNodePtr *tmp;
if (*RootPtr != NULL) {
if (K < (*RootPtr)->Key)
BSTDelete(&(* RootPtr)->LPtr, K);
else if (K > (*RootPtr)->Key)
BSTDelete(&(* RootPtr)->RPtr, K);
else {
if ((* RootPtr)->LPtr == NULL) {
/* there is only right branch or none*/
tmp = RootPtr;
*RootPtr = (* RootPtr)->RPtr;
free(*tmp);
*tmp = NULL;
}
else if ((* RootPtr)->RPtr == NULL) {
/* there is only left branch or none*/
tmp = RootPtr;
*RootPtr = (* RootPtr)->LPtr;
free(*tmp);
*tmp = NULL;
}
else
/* there are both branches, but that is for another topic*/
}
}
}
This code works correctly just in case when there are no branches connected to the node I am deleting. I expect that there is a problem with *tmp = NULL; line and I am losing my address to the rest of the branch but on the other hand if this line isn't included I am getting a SEGFAULT and I am trying to figure out why.
EDIT:
ok, now I know where the mistake was. It is stupid mistake, I should have used tBSTNodePtr tmp; instead of tBSTNodePtr *tmp;
you have problems with using pointers. If we have sometype *ptr and we check if this ptr adresses some space we write (ptr!=NULL).
*ptr is refering to the value itself, for example to your structre.
Read more about pointer types in C.
your logic for deleting is wrong
if ((* RootPtr)->LPtr == NULL) {
/* there is only right branch or none*/
tmp = RootPtr;
*RootPtr = (* RootPtr)->RPtr;
free(*tmp);
*tmp = NULL;
}
in this code you are deleting the required node but not adding the child root of that node
if ((* RootPtr)->LPtr == NULL) {
/* there is only right branch or none*/
tmp = RootPtr;
*RootPtr = (* RootPtr)->RPtr;
free(*tmp);
*tmp = NULL;
insert(RootPtr); //insert the child node again in the tree
}
Related
Is this a right way to do a linked list ? I am having a problem in a big school project and now i want to make sure that this is true.
void addnode(int a){
struct house* tmp = houses[i].next;
while (tmp != NULL) {
tmp = tmp->next;
}
tmp = (struct house*)malloc(sizeof(struct house));
tmp->id=a;
tmp->next=NULL;
}
i figured out that the error can be in other parts of the code. Now i will share the parts i suspect i hope you can help me.
houses[i] is an array of linked lists. if houses[i].id==-1 it is empty
struct house get_house_byid(int id) {
for (int i = 0; i < 1000; i++) {
if (houses[i].id != -1) {
if (houses[i].id == id) {
return houses[i];
}
if (houses[i].next != NULL) {
struct house* tmp = houses[i].next;
while (tmp != NULL) {
if (tmp->id == id) {
return *tmp;
}
tmp = tmp->next;
}
}
}
}
struct house housep;
housep.id = -1;
return housep;//if it cant find that id it returns housep
}
There may be other issues with your code that is not shown, but there are issues with addnode:
addnode does not set the head of the list (i.e. houses[i].next).
Thus, the newly added node is never connected to anything [and is a memory leak].
Ignoring the [obvious] typo/syntax error: void addnode{int a} instead of void addnode(int a).
The loop on tmp discards the pointer to the tail of the list. We need a separate variable (e.g. prev).
Note that i is global. That's fine, but the function would be cleaner if i was an argument to addnode instead.
Don't cast the return of malloc: Do I cast the result of malloc?
Here's is some refactored code. It is annotated:
void
addnode(int i,int a)
{
struct house *tmp;
struct house *prev;
// find the tail of the list
prev = NULL;
for (tmp = houses[i].next; tmp != NULL; tmp = tmp->next)
prev = tmp;
// allocate the new node
tmp = malloc(sizeof(*tmp));
tmp->id = a;
tmp->next = NULL;
// append to the tail of the [non-empty] list
if (prev != NULL)
prev->next = tmp;
// add to front of the empty list
else
houses[i].next = tmp;
}
I am relatively new to C, and have been learning about linked lists with pointers.
I learned that
(*foo).bar is the same ad foo->bar.
foo->bar is used because it is more readable.
Therefore I do not understand why these code snippets behave differently:
1)
void appendCourse(CourseNode** pLL, Course c){
CourseNode * root = *pLL;
CourseNode* last = makeCourseNode(c);
if(root != NULL){
CourseNode node = *root;
while(node.pNext != NULL){
node = *node.pNext;
}
node.pNext = last;
} else {
*pLL = last;
}
}
and
2)
void appendCourse(CourseNode** pLL, Course c){
CourseNode * root = *pLL;
CourseNode* last = makeCourseNode(c);
if(root != NULL){
CourseNode *node = root;
while(node->pNext != NULL){
node = node->pNext;
}
node->pNext = last;
} else {
*pLL = last;
}
}
to me it looks like 1) should behave as if dereferencing first, then member access. Sort of like (*foo).bar
but 1) doesn't seem to work right at all, it can only successfully add the first element.
2) does however add all elements into the linked list.
In case this helps: my structs and other method:
typedef struct CourseNode {
struct CourseNode* pNext;
Course course;
} CourseNode;
typedef struct
{
StudentNode *pWaitlistHead; // Waitlist for this course
char szCourseId[12]; // Course Identifier
char szRoom[15]; // Room number of the course
char szDays[15]; // What days the course will meet, ex: MWF, TR, etc
char szTimes[15]; // Meeting Time, ex: 10:00-11:15am
int iAvailSeats; // Number of available seats in the course
double dFee; // Additional fees for the course
} Course;
CourseNode* makeCourseNode(Course c){
CourseNode * node = malloc(sizeof(CourseNode));
node->pNext = NULL;
node->course = c;
return node;
}
CourseNode node = *root;
while(node.pNext != NULL){
node = *node.pNext;
}
This creates a new CourseNode called node. The value of that new CourseNode is modified, but that has no affect on the linked list.
CourseNode *node = root;
while(node->pNext != NULL){
node = node->pNext;
}
Here, node points to a CourseNode that is on the linked list.
The simplest way to understand the difference is that the first code excerpt creates new CourseNodes. It's like the difference between these two:
int foo (int *i)
{
int *j = i; // j is a pointer to the same int i points to
*j = 2; // this changes the value of the int i points to
int j = *i; // this creates a new int
j = 2; // this changes the value of that new int
}
could you help me?
Program reads words from a file and puts them into binary search tree, but I get "Segmentation fault: 11" when running my print function.
struct node {
char * item;
struct node * left;
struct node * right;
};
struct node * new(char * a) {
struct node * new;
new = (struct node *)malloc(sizeof(struct node *));
new->item = a;
new->left = new->right = NULL;
return new;
}
struct node * insert(struct node * a, char * b) {
if(a == NULL) {
a = new(b);
}
else if (b <= a->item) {
a->left = insert(a->left, b);
}
else {
a->right = insert(a->right, b);
}
return a;
}
void print(struct node * a) {
if (a->left == NULL && a->right == NULL)
printf("%s", a->item);
else if (a->left != NULL)
print(a->left);
else
print(a->right);
}
from main.c :
struct node * root = NULL;
struct node * start;
start = root;
while (fscanf(fp, "%s", temp) != EOF) {
root = insert(root, temp); // insert function works ok
}
print(start);
UPDATE:
I've made a change in main.c:
int i = 0;
while (fscanf(fp, "%s", temp) != EOF) {
root = insert(root, temp);
if (!i) {
start = root;
i = 1;
}
}
Now it doesn't show error, but it prints only the last word from the tree instead of printing it recursively. Any suggestions?
UPDATE #2:
Thank you for your help. Following your suggestions I've made changes to this function:
struct node * new(char * a) {
struct node * new;
char * stringcopy;
stringcopy = malloc(strlen(a) + 1);
strcpy(stringcopy, a);
new = malloc(sizeof(* new));
new->item = stringcopy;
new->left = new->right = NULL;
return new;
}
Now everything works fine.
The original problem was almost certainly that start was NULL since you did not update it when you updated root. (Meanwhile it seems that the whole start is unnecessary; just use root directly.)
The new problem (printing only the last word) is that you are not traversing the tree correctly: your print function only prints if both left and right are NULL, so only a leaf node is ever printed, and furthermore it does not descend into the right branch if there is a left branch.
You could try something like this instead (untested code):
void print(struct node * a) {
if (a == NULL) { return; }
print(a->left);
(void) puts(a->item);
print(a->right);
}
In particular, note that if you are at a non-NULL node, you need to print its item unconditionally, or the complete output will be missing that node.
Another problem seems to be that you are not copying item when you create the node. So if your temp in insert(root, temp) is indeed a temporary object that will be overwritten or freed, all of your items (except possibly the last) will be invalid by the time you try to print them. Instead of assigning new->item = a, do the equivalent of new->item = strdup(a) and then remember to free it when you free the node.
(strdup is not in the C standard library, but it is easy to implement: allocate enough space for the string, including NUL terminator, and copy.)
Also, the comparison b <= a->item is almost certainly not doing what you expect it to; see strcmp.
I'm trying to write an implementation for a Red Black Tree algorithm in C but after executing function insert() I get a crash and program stops working. This function firstly find a place in which new value should be added and then it execute another function called Correct_Tree which is responsible for correcting nodes with right order and colors.
There are few warnings I get but don't know how too fix them, other functions built in same way work fine.
|69|warning: conflicting types for 'correct_tree' [enabled by default]|
|40|note: previous implicit declaration of 'correct_tree' was here|
Same warnings point to function Rot_L, I don't know if this warnings cause my crashes. I will be thankful for every answer, if you need more information, let me know. Sorry for my english, I'm not a native speaker.
Here are these functions: http://ideone.com/hsYyES
and structure looks like this:
struct node {
int value;
int key_amounts;
char color;
struct node *parent;
struct node *left;
struct node *right;
} *root;
int insert(int n, struct node *start) {
//if node doesnt exist then add it to the tree otherwise increase amount of keys
//if tree is empty add root
if (root == NULL) {
root = (struct node*)malloc(sizeof *root);
root->value = n;
root->keys_amount = 0;
root->left = NULL;
root->right = NULL;
root->up = NULL;
} else
if (search(root, n) != NULL) {
struct node *tmp = search(root, n);
tmp->keys_amount += 1;
return 0;
} else
//if value is lower than root val then go to left son
if (n < start->value) {
//if left son exist then apply function insert for it
if (start->left != NULL) {
insert(n, start->left);
} else {
//if it doesnt exist then create
struct node *new = (struct node*)malloc(sizeof *root);
new->value = n;
new->keys_amount = 0;
new->left = NULL;
new->right = NULL;
new->up = start;
start->left = new;
correct_tree(new);
}
} else {
//if new value is higher than root
//if right son exist then apply function for it
if (start->right != NULL) {
insert(n, start->right);
} else {
//if it doesnt exist create new one
struct node *new = (struct node*)malloc(sizeof *root);
new->value = n;
new->keys_amount = 0;
new->left = NULL;
new->right = NULL;
new->up = start;
start->right = new;
correct_tree(new);
}
}
return 0;
}
//////////////////////////////////////////////////////////////////
void correct_tree(struct node *start) {
struct node *tmp = (struct node*)malloc(sizeof *root);
start->color = 'R';
while ((start != root) && (start->up->color == 'R')) {
if (start->up == start->up->up->left) {
tmp = start->up->up->right; //uncle of start for tmp
if (tmp->color == 'R') { //case 1
start->up->color = 'B';
tmp->color = 'B';
start->up->up->color='R';
start = start->up->up;
continue;
}
if (start == start->up->right) { //case 2
start = start->up;
rot_L(start);
}
start->up->color = 'B'; //case3
start->up->up->color = 'R';
rot_R(start->up->up);
break;
} else { //mirror cases
tmp = start->up->up->left;
if (tmp->color == 'R') { //case 1
start->up->color = 'B';
tmp->color = 'B';
start->up->up->color = 'R';
start = start->up->up;
continue;
}
if (start == start->up->left) { //case 2
start = start->up;
rot_R(start);
}
start->up->color = 'B'; //case3
start->up->up->color = 'R';
rot_L(start->up->up);
break;
}
}
root->color = 'B';
}
//////////////////////////////////////////////////////////////
void rot_L(struct node *start) {
struct node *tmp = (struct node*)malloc(sizeof *root);
struct node *tmp2 = (struct node*)malloc(sizeof *root);
tmp = start->right;
if (tmp != NULL) {
tmp2 = start->up;
start->right = tmp->left;
if (start->right != NULL)
start->right->up = start;
tmp->left = start;
tmp->up = tmp2;
start->up = tmp;
if (tmp2 != NULL) {
if (tmp2->left == start)
tmp2->left = tmp;
else
tmp2->right = tmp;
} else
root = tmp;
}
}
The warnings issued by the compiler tell you that you did not declare nor define correct_node before calling it. The prototype inferred by the compiler from the call is int correct_tree(struct node *start); which is incompatible with the actual definition it encounters later: void correct_tree(struct node *start). Same problem for rot_L(). Declare all functions before calling them.
Function correct_node is bound to fail because you dereference the up links without first checking that they are not NULL. For example the first time you call correct_node on theleftorrightchild of theroot` node, you have:
start->color = 'R';
while ((start != root) && (start->up->color == 'R')) {
if (start->up == start->up->up->left) {
You do not initialize the color of the root node allocated by malloc(). There is a small chance that root->color may be equal to 'R', which will cause start->up->up->left to have undefined behavior as start->up->up is NULL.
Another issue is this:
struct node *tmp = (struct node*)malloc(sizeof *root);
The object allocated for tmp is never used, never freed and tmp is overwritten in the loop. This is a blatant case of memory leak.
The same issue is present twice in rot_L, for tmp and tmp2.
I'm currently dealing with a generic Tree with this structure:
typedef struct NODE {
//node's keys
unsigned short *transboard;
int depth;
unsigned int i;
unsigned int j;
int player;
int value;
struct NODE *leftchild; //points to the first child from the left
struct NODE *rightbrothers; //linked list of brothers from the current node
}NODE;
static NODE *GameTree = NULL;
While the function that allocates the different nodes is (don't bother too much at the keys' values, basically allocates the children-nodes. If there aren't any the new child goes to leftchild, otherwise it goes at the end of the list "node->leftchild->rightbrothers"):
static int AllocateChildren(NODE **T, int depth, unsigned int i, unsigned int j, int player, unsigned short *transboard) {
NODE *tmp = NULL;
if ((*T)->leftchild == NULL) {
if( (tmp = (NODE*)malloc(sizeof(NODE)) )== NULL) return 0;
else {
tmp->i = i;
tmp->j = j;
tmp->depth = depth;
(player == MAX ) ? (tmp->value = 2 ): (tmp->value = -2);
tmp->player = player;
tmp->transboard = transboard;
tmp->leftchild = NULL;
tmp->rightbrothers = NULL;
(*T)->leftchild = tmp;
}
}
else {
NODE *scorri = (*T)->leftchild;
while (scorri->rightbrothers != NULL)
scorri = scorri->rightbrothers;
if( ( tmp = (NODE*)malloc(sizeof(NODE)) )== NULL) return 0;
else {
tmp->i = i;
tmp->j = j;
tmp->depth = depth;
(player == MAX) ? (tmp->value = 2) : (tmp->value = -2);
tmp->player = player;
tmp->transboard = transboard;
tmp->leftchild = NULL;
tmp->rightbrothers = NULL;
}
scorri->rightbrothers = tmp;
}
return 1;
}
I need to come up with a function, possibly recursive, that deallocates the whole tree, so far I've come up with this:
void DeleteTree(NODE **T) {
if((*T) != NULL) {
NODE *tmp;
for(tmp = (*T)->children; tmp->brother != NULL; tmp = tmp->brother) {
DeleteTree(&tmp);
}
free(*T);
}
}
But it doesn't seem working, it doesn't even deallocate a single node of memory.
Any ideas of where I am being wrong or how can it be implemented?
P.s. I've gotten the idea of the recursive function from this pseudocode from my teacher. However I'm not sure I've translated it correctly in C with my kind of Tree.
Pseudocode:
1: function DeleteTree(T)
2: if T != NULL then
3: for c ∈ Children(T) do
4: DeleteTree(c)
5: end for
6: Delete(T)
7: end if
8: end function
One thing I like doing if I'm allocating lots of tree nodes, that are going to go away at the same time, is to allocate them in 'batches'. I malloc then as an array of nodes and dole them out from a special nodealloc function after saving a pointer to the array (in a function like below). To drop the tree I just make sure I'm not keeping any references and then call the free routine (also like below).
This can also reduce the amount of RAM you allocate if you're lucky (or very smart) with your initial malloc or can trust realloc not to move the block when you shrink it.
struct freecell { struct freecell * next; void * memp; } * saved_pointers = 0;
static void
save_ptr_for_free(void * memp)
{
struct freecell * n = malloc(sizeof*n);
if (!n) {perror("malloc"); return; }
n->next = saved_pointers;
n->memp = memp;
saved_pointers = n;
}
static void
free_saved_memory(void)
{
while(saved_pointers) {
struct freecell * n = saved_pointers;
saved_pointers = saved_pointers->next;
free(n->memp);
free(n);
}
}
I've just realized my BIG mistake in the code and I'll just answer myself since no one had found the answer.
The error lies in this piece of code:
for(tmp = (*T)->children; tmp->brother != NULL; tmp = tmp->brother) {
DeleteTree(&tmp);
}
First of all Ami Tavory was right about the for condition, i need to continue as long as tmp != NULL
Basically it won't just work because after the DeleteTree(&tmp), I can no longer access the memory in tmp because it's obviously deleted, so after the first cycle of for ends I can't do tmp = tmp->rightbrother to move on the next node to delete because tmp->rightbrother no longer exists as I just deleted it.
In order to fix it I just needed to save the tmp->brother somewhere else:
void DeleteTree(NODE **T) {
if((*T) != NULL) {
NODE *tmp, *deletenode, *nextbrother;
for(tmp = (*T)->children; tmp != NULL; tmp = nextbrother) {
nextbrother = tmp->rightbrother;
DeleteTree(&tmp);
}
canc = (*T);
free(*T);
(*T) = NULL;
}
}
Just for the sake of completeness I want to add my version of DeleteTree
void DeleteTree(NODE *T) {
if(T != NULL) {
DeleteTree(T->rightbrothers);
DeleteTree(T->leftchild);
free(T);
}
}
I think it is much less obscure and much easier to read. Basically it solves the issue in DeleteTree but through eliminating the loop.
Since we free the nodes recursively we might as well do the whole process recursively.