Changing From Assert Function in C To If Statement - c

I have found some code online for red black trees, and am trying to implement it.
I do not want to use the assert function though which the original code has located here
I am getting a seg fault on the line n->color = child->color; just before the delete fix. After debugging I discovered that the child did not exist in this case, and so the reason for the assert statement in the original code. I decided to add what I thought was appropriate with the additional if clause around everything from where child is dealt with downward.
However, now the program does not actually delete, because if the child does not exist, it never makes it into the loop. After trial and error I still cannot find where to close the if clause in order to take the place of the assert statement properly.
Please let me know your ideas!
Here is my "translated" code without the assert, and using an if statement instead.
void delete_node(int key)
{
node* child;
node* n ;
n = searchTree(key);
if(n == NULL)return;
if(n->left != NULL && n->right != NULL)
{
node* pred = n->left;
while(pred->right != NULL)
pred = pred->right;
n->value = pred->value;
n = pred;
}
if(n->right != NULL || n->left != NULL){
child = n->right == NULL ? n->left : n->right;
if(n->color == 'b')
{
n->color = child->color;
delete_fix1(n);
}
swap_nodes(n, child);
if(n->parent == NULL && child != NULL)
child->color = 'b';
free(n);
}
}
Test data (Seg Fault occurs when attempting to delete 4):
i stand for insert (insert occurs flawlessly as far as I can tell)
d stands for delete
i 7
i 8
i 1
d 8
i 4
i 10
d 4
i 11

This:
assert(n->left == NULL || n->right == NULL)
Is nowhere near this:
if (n->right != NULL || n->left != NULL)
Recheck your translation. The assert states that one of them must be NULL. your if-expr evals true if either are not NULL. Your if-expr passes if both are non-null, where the assert would fail. Likewise, your if-expr fails if both are NULL, while the assert would pass.
Don't take shortcuts when doing this kinda of thing. First. keep the asserts regardless of your added checks. Second, until it is up and working, copy the assert clauses verbatim in your if (expr): or (!(expr)) for bailout conditions.
verbatim check:
assert(n->left == NULL || n->right == NULL)
if (n->left == NULL || n->right == NULL)...
bailout check:
assert(n->left == NULL || n->right == NULL)
if (!(n->left == NULL || n->right == NULL))
bailout loud.
EDIT Translation of linked code with integrated if-expr
void rbtree_delete(rbtree t, void* key, compare_func compare)
{
node child;
node n = lookup_node(t, key, compare);
if (n == NULL) return; /* Key not found, do nothing */
if (n->left != NULL && n->right != NULL) {
/* Copy key/value from predecessor and then delete it instead */
node pred = maximum_node(n->left);
n->key = pred->key;
n->value = pred->value;
n = pred;
}
assert(n->left == NULL || n->right == NULL);
if (n->left == NULL || n->right == NULL) // << note: SAME as above
{
child = n->right == NULL ? n->left : n->right;
if (node_color(n) == BLACK) {
n->color = node_color(child);
delete_case1(t, n);
}
replace_node(t, n, child);
if (n->parent == NULL && child != NULL)
child->color = BLACK;
free(n);
}
verify_properties(t);
}

Related

BST; the sum of BST elements which are greater then the sum of their direct children

So the task is to write a function which returns the sum of BST elements which are greater then the sum of their direct children. Do not count leaf nodes. I did it this way, the base case when the tree is empty return 0, and if it doesn't have sons also return 0. Then I checked if node has one or two children and the condition for the sums.
int sum(struct node* root)
{
if (root== NULL) return 0;
if (root->right == NULL && root->left==NULL) return 0;
if (root->right!= NULL && root->left != NULL)
{
if (((root->right)->key + (root->left)->key) < root->key) return root->key;
}
if (root->right != NULL && root->left==NULL)
{
if ((root->right)->key< root->key) return root->key;
}
if (root->left != NULL && root->right==NULL)
{
if ((root->left)->key < root->key) return root->key;
}
else return sum(root->right) + sum(root->left);
}
Main:
struct node* root = NULL;
add(&root,-4);
add(&root,6);
add(&root,8);
add(&root,-11);
add(&root,5);
add(&root,7);
add(&root,-20);
printf("%d",sum(root));
It should return -1 (6+8-11-4), but my function doesn't work I don't know why.
In your code, the else clause will never be executed; your previous conditions have dealt with all possibilities.
However, the expression should be executed. You either need to add + sum(root->left) + sum(root->right) as part of the three non-degenerate return statements, or you need to save the root->key in a local variable (defaulting to zero) and fall through to return return_value + sum(root->left) + sum(root->right);.
Hence (using the abbreviation rv for 'return value':
int sum(struct node* root)
{
int rv = 0;
if (root == NULL || (root->right == NULL && root->left == NULL))
return rv;
if (root->right != NULL && root->left != NULL)
{
if ((root->right->key + root->left->key) < root->key)
rv = root->key;
}
if (root->right != NULL && root->left == NULL)
{
if (root->right->key < root->key)
rv = root->key;
}
if (root->left != NULL && root->right == NULL)
{
if (root->left->key < root->key)
rv = root->key;
}
return rv + sum(root->right) + sum(root->left);
}
Warning: pristine code, unsullied by any attempt to compile it.
You could use an else if chain for the last three 'outer' if tests (with an else clause instead of the last test) to avoid repeated tests. Whether there'd be a measurable difference is debatable; an optimizer might even make the change for itself.

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;
}

Error removing entry and freeing bucket node in C linked list traversal

I am trying to develop a Hash Table with chained linked-list hashing to remedy collisions, but I seem to be having an error with my remove_entry function. I am working almost exclusively with pointers, and as such I am dynamically allocation and freeing memory as necessary.
Here are the structures for my Table and Bucket datatypes:
typedef struct bucket {
char *key;
void *value;
struct bucket *next;
} Bucket;
typedef struct {
int key_count;
int table_size;
void (*free_value)(void *);
Bucket **buckets;
} Table;
And here is my remove function. I tried to include commentary to explain what is going on:
int remove_entry(Table *table, const char *key) {
int hash;
Bucket *cur, *prev;
if (table == NULL || key == NULL) {
return FAILURE;
}
hash = hash_code(key) % (table->table_size);
/* case: key not present at lead bucket position */
if (table->buckets[hash] == NULL) {
return FAILURE;
} else {
cur = table->buckets[hash];
/* traverse thru the chain from table->buckets[hash]
* to see if the key exists somewhere. loop only can run the
* first time if its key does not match the paramteter key, so
* if they do match then we proceed to the logic below this
* loop (2nd if statement), acting on the FIRST (lead) bucket */
while (strcmp(cur->key, key) != 0 && cur != NULL) {
prev = cur;
cur = cur->next;
}
/* case - key not found anywhere (cur == null) */
if (cur == NULL) {
return FAILURE;
}
/* case - key found in chain (strcmp returned 0, keys match) */
if (strcmp(cur->key, key) == 0) {
if (table->free_value != NULL) {
table->free_value(cur->value);
cur->value = NULL;
}
free(cur->key);
cur->key = NULL;
if (cur->next != NULL) {
prev->next = cur->next;
}
free(cur);
cur = NULL;
table->key_count -= 1;
return SUCCESS;
}
}
return FAILURE;
}
valgrind tells me that there is an issue calling free() on cur at the bottom of the function, but I don't understand why that's an issue. The overall issue I found I am having is that the address of the bucket (at the appropriate index "hash") undergoes no change even though cur is changed.
Thanks in advance.
I think your error involves removing the FIRST bucket when more that one bucket exists.
In this code segment:
cur = table->buckets[hash];
while (strcmp(cur->key, key) != 0 && cur != NULL) {
prev = cur;
cur = cur->next;
}
If key does indeed equal the FIRST bucket pointed to by the cur pointer then your WHILE loop's inner code never gets invoked. And the upshot is that pointer prev is undefined ... you never pre-set it to NULL and it did not get set in that bypassed loop.
So this subsequent code snippet:
if (cur->next != NULL) {
prev->next = cur->next;
}
is going to fail since prev has either zero (NULL) in it, or has some undefined memory location stored in it.
Also the logic of the above code snippet is not quite correct; if this is the last bucket being removed (so cur->next indeed equals NULL), you still want to move that NULL back into the prev->next (or the list head) so that prev "knows" it is now the end of the linked list.
You are also failing to account for the case that you need to reset you linked list header (and not prev->next) when the very first bucket is the one being removed. This is a common mistake, and one I've answered several times recently just this week.
So I think you need to change your code to:
int remove_entry(Table *table, const char *key) {
int hash;
Bucket *cur;
Bucket *prev = NULL;
if (table == NULL || key == NULL) {
return FAILURE;
}
hash = hash_code(key) % (table->table_size);
/* case: key not present at lead bucket position */
if (table->buckets[hash] == NULL) {
return FAILURE;
} else {
cur = table->buckets[hash];
/* traverse thru the chain from table->buckets[hash]
* to see if the key exists somewhere. loop only can run the
* first time if its key does not match the paramteter key, so
* if they do match then we proceed to the logic below this
* loop (2nd if statement), acting on the FIRST (lead) bucket */
while (strcmp(cur->key, key) != 0 && cur != NULL) {
prev = cur;
cur = cur->next;
}
/* case - key not found anywhere (cur == null) */
if (cur == NULL) {
return FAILURE;
}
/* case - key found in chain (strcmp returned 0, keys match) */
if (strcmp(cur->key, key) == 0) {
if (table->free_value != NULL) {
table->free_value(cur->value);
cur->value = NULL;
}
free(cur->key);
cur->key = NULL;
if (prev != NULL) {
prev->next = cur->next; // even want to copy-back when cur->next == NULL
}
else
{
table->buckets[hash] = cur->next; // even want to copy-back when cur->next == NULL
}
free(cur);
cur = NULL;
table->key_count -= 1;
return SUCCESS;
}
}
return FAILURE;
}
I'm not sure why free(cur) would be giving you an error, but once you've fixed up the code to handle the linked list properly and don't have a prev pointer that is not properly initialized, you may be able to figure out what remains wrong.
One other trivial nit, you really don't need the following standalone IF statement: if (strcmp(cur->key, key) == 0) {. When you exit the WHILE loop, the only two conditions are cur indeed == NULL, or strcmp(cur->key, key) indeed == 0. You do not need the second IF statement. Its not wrong, just slightly inefficient.

Binary Search Tree in C: remove node function

I'm putting together functions for a binary search tree and ran into a wall. I'm working on each situation that might be encountered when a node holding a specified value needs to be removed from the tree. I'm uncertain how to handle freeing the node if it does not have a left and right child. The function must return a node. Do I back up, examine each left and right child, and remove the value while it's in a child? But then if the value is in the root, wouldn't I have a similar problem with how to remove it? Just by way of explanation, the program uses a void pointer then casts the TYPE val in a separate function compare() which evaluates both values and returns -1 for <, 0 for ==, and 1 for >.
struct Node *_removeNode(struct Node *cur, TYPE val)
{
if (compare(cur->val, val) == 0) { //if val == cur->val
if (cur->right != NULL && cur->left != NULL) { //if cur has right and left
cur = _leftMost(cur->right);
free(_leftMost(cur->right));
}
else if (cur->right == NULL && cur->left != NULL) { //if cur has left
cur = cur->left;
free(cur->left);
}
else if (cur->right != NULL && cur->left == NULL){ //if cur has right
cur = cur->right;
free(cur->right);
}
else if (cur->right == NULL && cur->left == NULL){ //if cur has no child
//free cur if cur = val
}
}
else if (compare(cur->val, val) == -1) {
cur->right = _removeNode(cur->right, val);
}
else if (compare(cur->val, val) == 1) {
cur->left = _removeNode(cur->left, val);
}
return cur;
}
If the node has neither child then it can simply be deleted. In order to make your recursion in the other cases work, you should return NULL from _removeNode. In all cases, cur should be deleted (freed) as it is no longer needed. In each case, you need to return the replacement subtree. The complication occurs in the first case where the left most descendent of the right child is pulled up. After pulling it up, you need to remove it from the right sub-tree (note that it may be the right sub-tree).
I wrote all of the below off the top of my head so be prepared for a few errors/a bit of debugging. Also, _leftMost and _removeLeftMost can be merged with a bit of work.
The block in question should look something like:
Node *replacement;
if (cur->right != NULL && cur->left != NULL) { //if cur has right and left
replacement = _leftMost(cur->right);
replacement->right = _removeLeftMost(cur->right,replacement);
replacement->left = cur->left;
}
else if (cur->right == NULL && cur->left != NULL) { //if cur has left
replacement = cur->left;
}
else if (cur->right != NULL && cur->left == NULL){ //if cur has right
replacement = cur->right;
}
else if (cur->right == NULL && cur->left == NULL){ //if cur has no child
replacement = NULL;
}
free(cur);
cur = replacement;
The function _removeLeftMost walks down the left child pointers until it sees the node to be replaced and then replaces it with the right child of that node. Something like:
Node *_removeLeftMost(node, remove) {
if (node == remove) {
return node->right; // Note that remove->left should be null
}
else {
node->left = _removeLeftMost(node->left,remove);
return node;
}
}
Also, the main call is something like
root = _removeNode(root, val);
So that handles your concern when the node is the root.

Is a Binary Tree Contained Within Another Binary Tree - C

So I just had an interview that I'm confident I screwed up royally. I had a bunch of questions thrown at me and didn't have enough time to answer the last one.
After getting all beginning questions correct, I was asked to write a function that would determine whether a binary tree b is contained within another binary tree a. I coded the question prior to that correctly, in which he asked me to write a function to determine whether two trees are equal:
int sameTree(struct node *a, struct node *b){
//both empty = TRUE
if(a == NULL && b == NULL)
return TRUE;
//both not empty, compare them
else if(a != NULL && b != NULL){
return(
a->data == b->data &&
sameTree(a->left, b->left) &&
sameTree(a->right, b->right)
);
}
//one empty, one not = FALSE
else
return FALSE;
}
Ugh. Just for clearing my conscience, again how would you determine whether tree b is inside tree a?
Thanks for any help guys.
int in(struct node* outer, struct node* inner){
if(inner == null){
return true; // say every tree contains the empty tree
} else if(outer == null){
return false;
} else if(same(outer, inner)){
return true;
} else return in(outer->left, inner) || in(outer->right, inner);
}
We must not use the OP's sameTree but rather this function:
int same(struct node* outer, struct node* inner){
return !inner || outer && outer->data == inner->data && same(outer->left, inner->left) && same(outer->right, inner->right);
}
Or, more verbosely,
int same(struct node* outer, struct node* inner){
if(inner == null){
return true;
} else if(outer == null){
return false;
} else if(outer->data == inner->data){
return same(outer->left, inner->left) && same(outer->right, inner->right);
} else return false;
}
This assumes you want the same tree with the same structure, contains in a:
For one, if b is null and a isn't, a contains b (you should check that in your last else).
Second, these aren't binary search trees (unsorted), so to check if b is inside a you should also traverse a (assuming you rename the function):
int containsTree(struct node *a, struct node *b){
//both empty = TRUE
if(a == NULL && b == NULL)
return TRUE;
//both not empty, compare them
else if(a != NULL && b != NULL){
return(
// sameTree should be changed to allow nulls, as below
sameTree(a, b)
// check recursively
|| containsTree(a->left, b)
|| containsTree(a->right, b)
);
//one empty, one not = FALSE
else
return B == NULL;
To check if tree A is contained as-is in tree B, find the node C in B such that C.data == A.data. If there is no such node, A is not contained in B. If C exists, check if A and C are equal using a modified sameTree function - one that ignores mismatches between null children of A and non-null children of C (return true if A.left/right is null).
Thanks #Kobi for the correction.

Resources