我正在尝试创建avl树,该树从文件中一对一地读取(键,值)对,并根据键的数据形成树。
首先,将元组读入键和值,并将其传递给create函数,在该函数中,我已使用结构初始化了树
typedef struct AVLTree{
int size; // count of items in avl tree
AVLTreeNode *root; // root
} AVLTree;
AVLTree *newAVLTree()
{
AVLTree *T;
T = malloc(sizeof (AVLTree));
assert (T != NULL);
T->size = 0;
T->root = NULL;
return T;
}
然后我将树的根值最初为NULL分配给AVLTreeNode,其结构如下:
typedef struct AVLTreeNode {
int key; //key of this item
int value; //value (int) of this item
int height; //height of the subtree rooted at this node
struct AVLTreeNode *parent; //pointer to parent
struct AVLTreeNode *left; //pointer to left child
struct AVLTreeNode *right; //pointer to right child
} AVLTreeNode;
//data type for AVL trees
typedef struct AVLTree{
int size; // count of items in avl tree
AVLTreeNode *root; // root
} AVLTree;
// create a new AVLTreeNode
AVLTreeNode *newAVLTreeNode(int k, int v )
{
AVLTreeNode *new;
new = malloc(sizeof(AVLTreeNode));
assert(new != NULL);
new->key = k;
new->value = v;
new->height = 0; // height of this new node is set to 0
new->left = NULL; // this node has no child
new->right = NULL;
new->parent = NULL; // no parent
return new;
}
现在,对于我从文件中读取的每个键,值对,我将其传递给create函数并检查3个条件,如下所示:
void insert_in_tree(int key, int value, struct AVLTreeNode **node){
// if the tree is empty
if(*node == NULL){
node = newNode;
}
// insert on left if the data in the key is less than the data in the node.
else if (key<(*node)->key){
insert_in_tree(key,value,&(*node)->left);
}
// insert on right if the data in the key is greater than the data in the node.
else if(key>(*node)->key)
{
insert_in_tree(key,value,&(*node)->right);
}
}
PS:不用担心newAVLTreeNode中的“值”部分,因为稍后我将使用它来处理重复项。
使用上面的代码,我期望树会形成,但是那没有发生。经过进一步调查和调试,我发现 在使用新的键和值传递insert_in_tree时,该节点也是新的,而不是已经创建的节点。
AVLTree *CreateAVLTree(const char *filename)
{
//Inititalising a new tree
AVLTree *tree = newAVLTree();
// initialising the head to root of tree i.e. null
AVLTreeNode *head = tree->root;
int key, value;
FILE* file = fopen(filename, "r"); // open a file
if(file == NULL) {
return 1; // error checking
}
while (fscanf (file, " (%d,%d)", &key, &value) == 2) // check for number of conversions
// space added here ^
{
insert_in_tree(key, value, &head);
//printf("%d,%d\n", key, value);
}
fclose(file);
return tree;
}
int main() //sample main for testing
{
AVLTree *tree1;
tree1=CreateAVLTree("File1.txt");
//PrintAVLTree(tree1);
return 0;
}
答案 0 :(得分:2)
Mikhail几乎在那儿,但是没有发现内存泄漏。我在下面纠正了这个问题:
void insert_in_tree(int key, int value, struct AVLTreeNode **node){
// if the tree is empty
if(*node == NULL){
*node = newAVLTreeNode(key, value);
}
// insert on left if the data in the key is less than the data in the node.
else if (key<(*node)->key){
insert_in_tree(key,value,&(*node)->left);
}
// insert on right if the data in the key is greater than the data in the node.
else if(key>(*node)->key)
{
insert_in_tree(key,value,&(*node)->right);
}
}
作为修复摘要:
您需要传递一个指针到放置新分配的节点的位置,因为C按值传递。这就是所谓的“双指针”。
您在每个递归调用上都分配了内存,但是仅在知道要插入的位置时才需要分配内存。
答案 1 :(得分:1)
在函数insert_in_tree
中,您试图修改按值传递的参数。您需要通过引用来传递这样的信息:
void insert_in_tree(int key, int value, struct AVLTreeNode **node){
// if the tree is empty
if(*node == NULL){
*node = newAVLTreeNode(key, value);
}
// insert on left if the data in the key is less than the data in the node.
else if (key<(*node)->key){
insert_in_tree(key,value,&(*node)->left);
}
// insert on right if the data in the key is greater than the data in the node.
else if(key>(*node)->key)
{
insert_in_tree(key,value,&(*node)->right);
}
}
此外,在node != NULL
情况下,此函数导致内存泄漏,因为它分配新节点,但不将指向该节点的指针保存在任何地方。
顺便说一句,您要创建的不是AVL树,而是binary search tree。
答案 2 :(得分:0)
更多答案中已经描述的更正内容:
void insert_in_tree(int key, int value, struct AVLTreeNode **node){
// if the tree is empty
if(*node == NULL){
*node = newAVLTreeNode(key, value);
}
// insert on left if the data in the key is less than the data in the node.
else if (key<(*node)->key){
insert_in_tree(key,value,&(*node)->left);
}
// insert on right if the data in the key is greater than the data in the node.
else if(key>(*node)->key)
{
insert_in_tree(key,value,&(*node)->right);
}
}
在 CreateAVLTree 中,您(现在)将节点的树设置为 head ,但是您错过了更新tree->root
的操作,这很容易一种方法是不使用临时var head ,而是直接使用tree->root
:
AVLTree *CreateAVLTree(const char *filename)
{
//Inititalising a new tree
AVLTree *tree = newAVLTree();
// initialising the head to root of tree i.e. null
int key, value;
FILE* file = fopen(filename, "r"); // open a file
if(file == NULL) {
return NULL; // error checking
}
while (fscanf (file, " (%d,%d)", &key, &value) == 2) // check for number of conversions
// space added here ^
{
insert_in_tree(key, value, &tree->root);
//printf("%d,%d\n", key, value);
}
fclose(file);
return tree;
}
当文件无法打开时,我也用return 1;
替换了无效的return NULL;
。
请注意,未设置 size 字段,也许它必须包含节点列表的大小,在这种情况下,只需在调用tree->size += 1;
的附近添加insert_in_tree
>
如果我添加定义以打印结果:
void PrintNodes(struct AVLTreeNode * l)
{
if (l == NULL)
printf("()");
else {
putchar('(');
PrintNodes(l->left);
printf(" %d %d %d ", l->key, l->value, l->height);
PrintNodes(l->right);
putchar(')');
}
}
void PrintAVLTree(AVLTree * tree)
{
printf("%d elements : ", tree->size);
PrintNodes(tree->root);
putchar('\n');
}
并将PrintAVLTree(tree1);
放在 main ,编译和执行的注释中:
pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall t.c
pi@raspberrypi:/tmp $ cat File1.txt
(2, 50) (4, 30) (9, 30) (10, 400) (-5, -40)
(7, 20) (19, 200) (20, 50) (-18, -200) (-2, 29)
(2, 67) (4, 35) (9, 45) (-18, 100)
pi@raspberrypi:/tmp $ ./a.out
14 elements : (((() -18 -200 0 ()) -5 -40 0 (() -2 29 0 ())) 2 50 0 (() 4 30 0 ((() 7 20 0 ()) 9 30 0 (() 10 400 0 (() 19 200 0 (() 20 50 0 ()))))))