【发布时间】:2018-05-20 17:36:13
【问题描述】:
我正在尝试实现一个 r&b 树,但首先我想要一个简单的二叉树(它不会在其叶子上保存内容),然后实现 r&b 属性。问题是我遇到了无法解释的分段错误。
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
typedef struct node
{
unsigned long int val;
bool black;
struct node* parent;
struct node* lchild;
struct node* rchild;
}mynode;
mynode* createNode(unsigned long int ival, mynode* father);
mynode* createLeaf(unsigned long int ival, mynode* father);
mynode* search (unsigned long int ival, mynode *root);
void insert ( unsigned long int ival, mynode *root);
int main()
{
mynode root;
mynode *rootptr;
mynode *leafptr;
FILE *fp;
int ch;
unsigned long long lines=0, i=0;
unsigned long *myArr;
unsigned long int ival;
fp = fopen("integers.txt","r");
if(fp == NULL)
{
printf("Error in opening file.");
return(-1);
}
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
lines++;
printf("lines = %lu", lines);
myArr = (unsigned long*)calloc(lines, sizeof(unsigned long));
fseek(fp, 0, SEEK_SET);
while(!feof(fp))
{
fscanf(fp, "%lu,", &myArr[i] ); // des ta pos k giati tou input.
i++;
}
fclose(fp);
root.val = myArr[0];
root.parent = NULL;
root.lchild = NULL;
root.rchild = NULL;
root.black = true;
rootptr = &root;
leafptr = createLeaf(rootptr->val, rootptr);
rootptr->lchild = leafptr;
leafptr = createLeaf(rootptr->val, rootptr);
rootptr->rchild = leafptr;
for(i=1; i<lines; i++)
{
ival = myArr[i];
insert(ival, rootptr);
}
return 0;
}
mynode* createNode(unsigned long int ival, mynode* father)
{
mynode* nodeptr;
mynode node;
nodeptr = &node;
nodeptr->val = ival;
nodeptr->lchild = NULL;
nodeptr->rchild = NULL;
nodeptr->parent = father;
nodeptr->black = true;
return nodeptr;
}
mynode* createLeaf(unsigned long int ival, mynode* father)
{
mynode* nodeptr;
mynode leaf;
nodeptr = &leaf;
nodeptr->val = ival;
nodeptr->lchild = NULL;
nodeptr->rchild = NULL;
nodeptr->parent = father;
nodeptr->black = true;
return nodeptr;
}
mynode* search (unsigned long int ival, mynode *rootptr)
{
mynode* myptr;
myptr = rootptr;
while ( ( (myptr->lchild) != NULL) && ( (myptr->rchild) != NULL))
{
if ( ival < myptr->val)
{
myptr = myptr->lchild;
}
else
{
myptr = myptr->rchild;
}
}
return myptr;
}
void insert (unsigned long int ival, mynode *root)
{
mynode * current;
mynode * leafptr;
mynode * father;
unsigned long int max, min;
unsigned long int num;
current = search(ival, root);
num = current->val;
if((current->val) == ival)
{
return ;
}
else
{
if(ival>(current->val))
{
max = ival;
min = current->val;
}
else
{
max = current->val;
min = ival;
}
father = current->parent;
current = createNode(min, father);
if(num == (father->lchild)->val)
{
father->lchild = current;
}
else
{
father->rchild = current;
}
leafptr = createLeaf(min, current);
current->lchild = leafptr;
leafptr = createLeaf(max, current);
current->rchild = leafptr;
return ;
}
}
我打开一个文件。计算行数,因为我知道每一行都有一个数字。使用上述信息创建一个数组。然后我创建根和它的 2 片叶子。然后我将数组的其余部分插入(在那里我得到分段错误)到我的数据结构中。我认为问题在于功能。
【问题讨论】:
-
这应该会发出警告:
createNode()和createLeaf()返回一个指向本地对象的指针(node的地址),一旦您从函数返回,这些指针就无效了。
标签: c tree binary segmentation-fault binary-search-tree