English 中文(简体)
C. 执行二树 c 时的截分误差
原标题:Segmentation fault while implementing a binary tree in c

我是新来的,但对于一个项目,我正在执行一棵二进制树。 这是我的函数代码:

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

typedef struct BSTnode{
  int key;
  int subtree_size;
  struct BSTnode* left;
  struct BSTnode* right;
} TreeNode;

TreeNode* newNode(int value){
  TreeNode *node = malloc(sizeof(TreeNode));
  node->key = value;
  node->left = NULL;
  node->right = NULL;
  node->subtree_size = 1;
  return node;
}

TreeNode* insert(TreeNode* root, TreeNode* to_insert){
  if(root == NULL){
    root = newNode(to_insert->key);
  }
  else if(to_insert->key> root->key){
    if(root->right == NULL){
      root->right = to_insert;
      return root;
    }
    else{
      root->right = insert(root->right, to_insert);
    }
  }
  else if(to_insert->key< root->key){
    if(root->left == NULL){
      root->left = to_insert;
      return root;
    }
    root->left = insert(root->left, to_insert);

  }

  root->subtree_size = 1 + (root->left? root->left->subtree_size: 0) + (root->right? root->right->subtree_size: 0);
  return root;
}

int find(TreeNode* root, int value){
  if(root == NULL || root->key == value){
    return root->key;
  }
  else if(value > root->key){
    return find(root->right, value);
  }
  else{
    return find(root->left, value);
  }
}

在我的主要函数中,我读取的文件是,我必须在每一行中插入一个数字,每个行以字母开头,然后是空格分隔的编号。

int main (int argc, char* argv[]){
  FILE* input;

  input = fopen(argv[1], "r");

  char oper;
  int num;

  TreeNode* root = NULL;
  
  while(fscanf(input, "%c", &oper) != EOF){
    if(oper ==  i ){
      fscanf(input, " %d
", &num);
      TreeNode* to_insert = newNode(num);
      insert(root, to_insert);
      //printf("operation: %c, num: %d
", oper, num);
    }
    if(oper ==  r ){
//do some other thing
    }
  }
  if(root == NULL){
    printf("yes
");
  }
  printf("size: %d
", root->subtree_size);
  printf("left size: %d
", root->left->subtree_size);
  printf("right size: %d
", root->right->subtree_size);
  fclose(input);
}

运行代码打印“ 是 ”, 这给了我一个分类错误。 我看到了另一个关于类似问题的文章, 解决办法是将插入功能更改为根的双指针。 这有用吗? Id so? 会是什么样子呢? 我对双指针不太行 。

编辑 1: 假设 < code> fscanf () 和输入文件得到正确使用和打开 。

编辑2: 输入文件将看起来像这样 :

i 10
i 20
i 30
i 40
i 50
i 60
i 70
i 80
i 90
i 99
i 15
i 14
i 12
i 25
i 28
i 95
i 35
i 38
i 11
i 23
i 22
最佳回答

您的问题: < 强/ 强 >

您的 Indige () 函数返回 root 。 但您从未为任何事物指定返回值, 至少您在 main () 中没有指定 :

TreeNode* root = NULL;

while(fscanf(input, "%c", &oper) != EOF){
  // ...
  insert(root, to_insert);

您的 root 总是保留 NULLL

" 强" 我如何发现:

gcc -Wall -Wextra -g testme.c -o testme
gdb -tui --args ./testme input.txt
break main
run

然后“ n” 用于下一步( 跨步), “ s” 用于一步( 跨步) 。 或者从那里学习 gdb, 或者熟悉您选择的不同调试器。 没有调试器进行软件开发是 < em> hard < / em > 。 不要尝试 。

< 坚固> 我的固定:

Ibhor 双指针。 幸运的是, 您的问题有一个简单的修正 。 在 < code> Indig () 中, 对于收到的第一个节点, 不要 < code> newNode () , 只要选择您给定的节点 :

if(root == NULL){
  root = to_insert;
}

main () 中,将返回值指定为 root :

  TreeNode* to_insert = newNode(num);
  root = insert(root, to_insert);

<强> 可用时间:

请注意您的程序泄漏了它分配到的所有记忆 。

您在访问 NULLL 之前,需要检查 root root , left 和 root-gt;right (如果您没有阅读任何数据), 并且您需要再次检查其中任何一个 root , 然后再访问它们各自的 ...- & gt;subtree_size (如果您与您的示例数据一样,在一个子树中根本没有数据 ) 。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签