找不到我从排序数组构建平衡二叉树时的错误(段错误)

标签 c data-structures segmentation-fault

我正在完成这项作业,虽然我对 C 语言编程还很陌生,但我认为我对内存引用和指针的使用有了更好的掌握...

我想做的是从排序数组创建平衡二叉树。当我尝试调用我的树构建函数时,我抛出了段错误。如果我保留该函数调用并仅打印我的数组,则所有内容都会编译并按预期输出正常运行。

我的树节点是这样构建的:

typedef struct leaf{
    int value;
    struct leaf* left;
    struct leaf* right;
} LEAF;

我的排序/建树函数库:

LEAF* balance( int n[], int first, int last ){
    int mid;
    LEAF* ptr = NULL;
    printf( "in Balance" );
    if ( first > last ) return NULL;

    mid = first + (last - first)/2;
    ptr->value = n[mid]; 

    ptr->left = ( balance( n, first, mid-1 ) );
    ptr->right = ( balance( n, mid+1, last ) );

    return ptr;        
}
void ins ( int* n, int length ){  //Don't THINK the problem lies here, 
                                    I've used the algorithm before and can 
                                    print out the sorted array
    int i, j, key;

    for( i = 0; i < length; i++ ){/*all indexes*/
            key = n[i]; /*pick out index and store in variable*/
            for( j = i-1; j >= 0; j = j-1 ){/*all objects left of i*/
                    if ( n[j] < key) break;/*stop and insert*/
                    n[j + 1] = n[j];/*shifting all objects left*/
            }
            n[j + 1] = key;/*insertion expression*/
    }
}

回到 main(),我构建数组 n[] 并调用 Balance(),如下所示:

int main (void){
/****** initializations */
    int* n;
    char buffer[20];
    FILE* fp;
    int numMax = 5;
    int lastIndex = 0;
    int j;
    char* filename = "numbers.txt";
    LEAF* root = NULL;
    n = malloc ( numMax * sizeof(int*) );

    if ( ( fp = fopen(filename, "r")) == NULL ){
            printf( "cannot open %s\n", filename );
            exit( 1 );
    }

/****** allocating storage array and inputting */
    printf( "initial array size: %d", numMax );
    while( fgets( buffer, sizeof(buffer), fp ) != NULL){
            n[lastIndex] = atoi( buffer );
            lastIndex++;

            if ( lastIndex == numMax ){  /*When max num reached, double allocation*/
                    numMax = numMax * 2;
                    if ( ( n = realloc( n, numMax * sizeof(int) ) ) == NULL ){
                            printf( "Cannot allocate more mem." );
                            exit( 1 );
                    }

                    printf( "\nReached limit... increasing array to %d possible indexes.", numMax );
            }

    }
    lastIndex--;

/****** sort*/
    ins( n, lastIndex+1 );

/****** build tree*/
    root = balance( n, 0, lastIndex );

我知道这里有很多代码可能不需要解决我的问题。我把几乎所有的代码都放在这篇文章中,只是以防我在意想不到的地方搞砸了。我希望这可能是一个非常简单的解决方案,但会让我感觉很愚蠢。我觉得自己越笨,犯两次错误的可能性就越小!

但有一件奇怪的事情:我什至看不到 printf( "in Balance") 语句,并且如果我将其他 printf() 放在 root = Balance() 函数调用上方的几行内,它们也不在段错误之前打印。也许这是我不明白的编译器中的一些动态?

最佳答案

我看不到 ptr 变量是如何在 balance 函数中分配的。 ptr->value 总是会导致取消引用 null,除非您将指针指向某处(为其分配一些内存,或将其指向某些现有内存)。

LEAF* ptr = NULL;
printf( "in Balance" );
if ( first > last ) return NULL;

mid = first + (last - first)/2;
ptr->value = n[mid]; 

关于找不到我从排序数组构建平衡二叉树时的错误(段错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277821/

相关文章:

c - 文件描述符标志和函数

c - 到达终点所需的最小跳跃次数的线性时间算法

python - 我们可以使用映射来搜索而不是二分搜索吗?

c - 这个程序中的段错误是什么

c - 链接列表程序中的段错误

c - 尝试在 C 和 Ubuntu 中使用 FFMPEG

c - "make clean"结果 "No rule to make target ` 干净 '"

C - 函数指针的问题

algorithm - 适合基于最高有效位存储和查询整数的数据结构

c - C中二维数组的最大尺寸