c - 从文本文件中读取字符串以构建二叉搜索树

标签 c pointers binary-search-tree

这是我到目前为止的代码

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define lineSize 256

struct recordNode {
   char district[256];
   int employees;
   int employers;
   int students;
   int retried;
   int others;
   struct recordNode* left;
   struct recordNode* right;
};

struct stockNode* addRecord(struct recordNode* tree, struct recordNode node) {
   struct recordNode* newnode;
   struct recordNode* searcher;

   /* allocate memory block and assign parameter values to it */
   newnode = (struct recordNode*)malloc(sizeof(struct recordNode));

   newnode->left = NULL;
   newnode->right = NULL;
   /* check if the tree is empty, and in such case, return the newnode as*/
   /* the first node of the tree */
   if (tree == NULL)
     return newnode;
   /* searcher is the pointer to search for the correct location for insertion */
     searcher = tree;
   while (1) {
   /* see if the newnode should go to left branch */
      //if (code < searcher->code) {
      if (strcmp(tree->district, node.district) < 0) {
      /* yes, and if the left branch is empty, this is the insertion location */
         if (searcher->left == NULL) {
            searcher->left = newnode;
            return tree;
         }
      else { /* not yet, keep moving to the next level down */
         searcher = searcher->left;
         continue;
      }
   }
   /* see if the newnode should go to right branch */
      if (strcmp(tree->district, node.district) > 0) {
   /* yes, and if the right branch is empty, this is the insertion location */
         if (searcher->right == NULL) {
            searcher->right = newnode;
            return tree;
         }
      else { /* not yet, keep moving to the next level down */
        searcher = searcher->right;
        continue;
      }
   }
   else {
      free(newnode);
      return NULL; /* an error indication */
   }
   }
}

void getFile () {
   struct recordNode node;
   struct recordNode *tree;

   FILE* fpin;
   FILE* fpout;

   char line_buffer[lineSize]; /* BUFSIZ is defined if you include stdio.h */
   int counter = 0;

   //file validation
   fpin=fopen("testData.txt", "r");

   if (fpin == NULL ) exit(0);
        counter = 0;
    while (fgets(line_buffer, sizeof(line_buffer), fpin)) { 
               counter++;
               if (counter != 1) {
               sscanf(line_buffer, "%[^','],%d,%d,%d,%d", node.district, &node.employees, &node.students, &node.retried, &node.others);
               tree = addRecord(tree, node); **//ERROR**

               }

    }
        getchar();

}

void main() {

   getFile();
   getchar();

}

以下行:

tree = addRecord(tree, node);

出现此错误:

//ERROR Project Project2.exe raised exception class EAccessViolation with message 'Access violation at address 32657E39. Read of address 00000001'. Process stopped. Use Step or Run to continue

如何解决这个问题?

最佳答案

首次创建 *tree 时,请确保将其指定为 NULL。否则它可以是任何值。

 void getFile () {
       struct recordNode node;
       struct recordNode *tree=NULL;
       ...

关于c - 从文本文件中读取字符串以构建二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274517/

相关文章:

c - 为什么 printf 在 main 函数中留下一些随机字符? [C程序]

c - write(socket, buff, length) 使崩溃

python - 访问 PyObject 的底层结构

c - 修改指向数组的结构指针以更改不同数组值的结果

python - 在 Python 中将值插入二叉搜索树

在线性时间内组合两个二叉搜索树的算法

c - 使用 scanf 的程序出现段错误

c - 仅使用一个函数更新全局 C 结构而不传递整个数据结构

c++ - 指针算术题(数组大小)[C++]

c - c中二叉搜索树的删除