[英]Implementing an AVL tree, but getting seg fault on large data file insertion
Creating a dictionary comparison program utilizing avl_tree, but getting a segfault when reading in a large file 200,000+ unique words.使用 avl_tree 创建字典比较程序,但在读取 200,000 多个唯一单词的大文件时出现段错误。 Smaller files seem to work fine.较小的文件似乎工作正常。
A couple of my thoughts might be my word_compre function which may interfere with the rotation.我的一些想法可能是我的 word_compre function 可能会干扰旋转。 Or likely I have implemented the rotation incorrectly.或者我可能错误地实施了轮换。
// String compare based on alphabetical order
int word_compare(char *word, struct tree_node *root)
{
return strcasecmp(word, root->word);
}
// Get the maximum of two integers
int get_max(int int_a, int int_b)
{
if (int_a > int_b)
return int_a;
else
return int_b;
}
// Get the height of the tree
int get_height(struct tree_node *root)
{
if (root == NULL)
return 0;
else
return root->height;
}
// Determine if the tree or subtree is unbalanced by comparing the max height
// of the left and right branches
int get_balance(struct tree_node *root)
{
if (root == NULL)
return 0;
else
return get_height(root->left) - get_height(root->right);
}
// Rotate left subtree rooted with right child node
struct tree_node *left_rotate(struct tree_node* root)
{
// Store the right child node to rotate
struct tree_node *right_child = root->right;
// Store the left of the right child node to rotate
struct tree_node *subtree = right_child->left;
// Perform rotation
right_child->left = root;
root->right = subtree;
// Update heights
root->height = get_max(get_height(root->left),
get_height(root->right)) + 1;
right_child->height = get_max(get_height(right_child->left),
get_height(right_child->right)) + 1;
return right_child;
}
// Rotate right subtree rooted with left child node
struct tree_node *right_rotate(struct tree_node *root)
{
// Store the left child node to rotate
struct tree_node *left_child = root->left;
// Store the right of the left child node to rotate
struct tree_node *subtree = left_child->right;
// Perform rotation
left_child->right = root;
root->left = subtree;
// Update heights
root->height = get_max(get_height(root->left),
get_height(root->right)) + 1;
left_child->height = get_max(get_height(left_child->left),
get_height(left_child->right)) + 1;
return left_child;
}
// Insert new node into a tree
struct tree_node *insert_node(struct tree_node *root, char *word, int fp_num)
{
// Viable spot found, insert a new node
if (root == NULL) {
#ifdef DEBUG
printf("INSERTED NODE\n");
printf("WORD: %s\n", word);
#endif
if (fp_num == 1) {
// Allocate a new node
root = (struct tree_node *)malloc(sizeof(struct tree_node));
// Fail to allocate a new node
if (root == NULL) {
printf("\nFailed to allocate a node\n");
return NULL;
}
// Initialize the new node
else {
root->word = strdup(word);
root->count = 1;
root->left = NULL;
root->right = NULL;
root->height = 1;
return root;
}
}
else {
return root;
}
}
int exist = word_compare(word, root);
if (exist < 0) {
root->left = insert_node(root->left, word, fp_num);
}
else if (exist > 0) {
root->right = insert_node(root->right, word, fp_num);
}
else {
if (fp_num != 1 && root->count == fp_num - 1) {
root->count = fp_num;
}
}
// Check the balance of the tree
int balance = get_balance(root);
// If the tree is imbalanced, fixed them with one of the following cases
// Left-Left-Case
if (balance > 1 && (word_compare(word, root->left) < 0))
return right_rotate(root);
// Right-Right-Case
if (balance < -1 && (word_compare(word, root->right) > 0))
return left_rotate(root);
// Left-Right-Case
if (balance > 1 && (word_compare(word, root->left) > 0)) {
root->left = left_rotate(root->left);
return right_rotate(root);
}
// Right-Left-Case
if (balance < -1 && (word_compare(word, root->right) < 0)) {
root->right = right_rotate(root->right);
return left_rotate(root);
}
return root;
}
// Perform in-order traversal sort of the tree and display results
void in_order(struct tree_node *root, int fp_num)
{
if (root == NULL)
return;
// Recur until the left most node
if (root->left)
in_order(root->left, fp_num);
// Find the amount of dollars from the cents and puts the
// remainder after a decimal point
if (fp_num != 1 && root->count == fp_num)
printf("%s\n", root->word);
// Recur until the right most node
if (root->right)
in_order(root->right, fp_num);
}
// Delete tree
void free_tree(struct tree_node *root)
{
if (root != NULL)
{
free_tree(root->left);
free(root->word);
free_tree(root->right);
free(root);
}
}
Fixed it.解决它。 Did not update current node/root height in the insertion function.没有在插入 function 中更新当前节点/根高度。
// Insert new node into a tree
struct tree_node *insert_node(struct tree_node *root, char *word, int fp_num)
{
// Viable spot found, insert a new node
if (root == NULL) {
#ifdef DEBUG
printf("INSERTED NODE\n");
printf("WORD: %s\n", word);
#endif
if (fp_num == 1) {
// Allocate a new node
root = (struct tree_node *)malloc(sizeof(struct tree_node));
// Fail to allocate a new node
if (root == NULL) {
printf("\nFailed to allocate a node\n");
return NULL;
}
// Initialize the new node
else {
root->word = strdup(word);
root->count = 1;
root->left = NULL;
root->right = NULL;
root->height = 1;
return root;
}
}
else {
return root;
}
}
int word_exist = word_compare(word, root);
if (word_exist < 0) {
root->left = insert_node(root->left, word, fp_num);
}
else if (word_exist > 0) {
root->right = insert_node(root->right, word, fp_num);
}
else {
if (fp_num != 1 && root->count == fp_num - 1) {
root->count = fp_num;
}
}
// Update height of current node
root->height = 1 + get_max(get_height(root->left), get_height(root->right));
// Check the balance of the tree
int balance = get_balance(root);
// If the tree is imbalanced, fixed them with one of the following cases
// Left-Left-Case
if (balance > 1 && (word_compare(word, root->left) < 0))
return right_rotate(root);
// Right-Right-Case
if (balance < -1 && (word_compare(word, root->right) > 0))
return left_rotate(root);
// Left-Right-Case
if (balance > 1 && (word_compare(word, root->left) > 0)) {
root->left = left_rotate(root->left);
return right_rotate(root);
}
// Right-Left-Case
if (balance < -1 && (word_compare(word, root->right) < 0)) {
root->right = right_rotate(root->right);
return left_rotate(root);
}
return root;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.