I’m in the middle of integrating my nodes-rotating algorithm with my insertion algorithm until this error emerge. After the balancing function return a new node to replace the old one, the recursion stopped after 1 or 0 node. I put a couple of printf() to backtrack the problem but i still don’t find a clue what’s going on. Is this one of those scenario where the problem is just unexplainable? thanks for your time reading this post.
#include <stdio.h>
#include <stdlib.h>
struct data {
int num;
struct data *left;
struct data *right;
};
struct data *root = NULL;
struct data *create_node(int num){
struct data *new_node = (struct data*)malloc(sizeof(struct data));
new_node->num = num;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
int bf_left(struct data *bot, int depth){
int left, right;
if(bot != NULL){
left = bf_left(bot->left, depth + 1);
right = bf_left(bot->right, depth + 1);
} else{
return depth - 1;
}
if(left < right){
return right;
} else if(left >= right){
return left;
}
}
int bf_right(struct data *bot, int depth){
int left, right;
if(bot != NULL){
left = bf_right(bot->left, depth + 1);
right = bf_right(bot->right, depth + 1);
} else{
return depth - 1;
}
if(left < right){
return right;
} else if(left >= right){
return left;
}
}
int bf_finder(struct data *bot, int depth){
int left = bf_left(bot->left, depth + 1);
int right = bf_right(bot->right, depth + 1);
return left - right;
}
struct data *rl(struct data *bot){
struct data *node = bot->right;
struct data *node2;
if(node->left != NULL){
node2 = node->left;
}
node->left = bot;
bot->right = NULL;
if(node2 != NULL){
bot->right = node2;
}
return node;
}
struct data *rr(struct data *bot){
struct data *node = bot->left;
struct data *node2;
if(node->right != NULL){
node2 = node->right;
}
node->right = bot;
if(node2 != NULL){
bot->left = node2;
}
return node;
}
struct data *minus2(struct data *bot){
printf("this is minus2n");
if(bf_finder(bot->right, 0) <= 0){
printf("this is pure rln");
struct data *bot2 = rl(bot);
printf("bot2 = %dn", bot2->num);
return bot2;
} else{
bot->right = rr(bot->right);
return rl(bot);
}
}
struct data *plus2(struct data *bot){
if(bf_finder(bot->left, 0) >= 0){
return rr(bot);
} else{
bot->left = rl(bot->left);
return rr(bot);
}
}
struct data *insert(struct data *bot, int num){
if(bot == NULL){
if(root == NULL){
root = create_node(num);
} else{
return create_node(num);
}
} else if(num < bot->num){
bot->left = insert(bot->left, num);
if(bf_finder(bot, 0) == 2){
return plus2(bot);
}
} else if(num > bot->num){
bot->right = insert(bot->right, num);
printf("bot->right = %d, bf = %dn", bot->right->num, bf_finder(bot, 0));
if(bf_finder(bot, 0) == -2){
struct data *bot2 = minus2(bot);
printf("bot2 arrived on insert = %dn", bot2->num);
return bot2;
}
}
return bot;
}
struct data *find_max(struct data *bot){
if(bot->right != NULL){
find_max(bot->right);
} else{
return bot;
}
}
struct data *del(struct data *bot, int num){
if(bot == NULL){
return NULL;
} else if(num < bot->num){
bot->left = del(bot->left, num);
} else if(num > bot->num){
bot->right = del(bot->right, num);
} else{
if(bot->left == NULL && bot->right == NULL){
if(bot == root){
root = NULL;
}
free(bot);
return NULL;
} else if(bot->left == NULL || bot->right == NULL){
struct data *temp;
if(bot->left != NULL){
temp = bot->left;
if(bot == root){
root = temp;
}
free(bot);
return temp;
} else{
temp = bot->right;
if(bot == root){
root = temp;
}
free(bot);
return temp;
}
} else{
struct data *temp = find_max(bot->left);
bot->num = temp->num;
bot->left = del(bot->left, bot->num);
}
}
return bot;
}
void print_all(struct data *bot){
if(bot != NULL){
print_all(bot->left);
printf("%d ", bot->num);
printf("%dn", bf_finder(bot, 0));
print_all(bot->right);
}
}
int main(){
insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
insert(root, 90);
insert(root, 100);
print_all(root); printf("n");
// del(root, 20);
//
// print_all(root); printf("n");
//
// del(root, 40);
//
// print_all(root);
// printf("bf root: %d", bf_finder(root, 0));
return 0;
}
I expect for the backward recursion on insert() function to continue until root node.