简体   繁体   English

c二叉搜索树中的分段错误11

[英]segmentation fault 11 in c binary search tree

I am getting a segmentation fault when trying to print the nodes in my binary tree. 尝试打印我的二叉树中的节点时出现分段错误。 It looks to be an issue with the third node. 第三个节点似乎有问题。 I have searched google and stack overflow for hours but I can not understand what the problem is. 我已经搜索了谷歌和堆栈溢出了几个小时,但我不明白是什么问题。 I am trying to teach myself data structures in C and am very much a novice so I may be doing something in a frowned upon way. 我正在尝试自学C语言中的数据结构,并且是一个新手,所以我可能会皱眉。

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

typedef struct node
{
  int data;
  struct node *left;
  struct node *right;
} Node;

typedef struct
{
  Node *root;
} BinarySearchTree;

void printInOrder(Node *);
void addNode(Node *, Node *);

int main (void)
{
  BinarySearchTree tree;
  BinarySearchTree *tree_ptr = &tree; 
  Node n1, n2, n3;

  n1.data = 1;
  n2.data = 2;
  n3.data = 3;

  Node *n1_ptr = &n1;
  Node *n2_ptr = &n2;
  Node *n3_ptr = &n3;

  tree_ptr->root = n1_ptr;

  addNode(tree_ptr->root, n2_ptr);
  addNode(tree_ptr->root, n3_ptr);
  printInOrder(tree_ptr->root);
}

void printInOrder(Node *root)
{
  if (root == NULL)
  {
    return;
  } else
  {
    printInOrder(root->left);
    printf("%i\n", root->data);
    printInOrder(root->right);
  }
}

void addNode(Node *root, Node *node)
{
  if (node->data < root->data)
  {
    if (root->left == NULL)
    {
      root->left = node;
    } else
    {
      addNode(root->left, node);
    }
  } 

  else if (node->data > root->data)
  {
    if (root->right == NULL)
    {
      root->right = node;
    } else
    {
      addNode(root->right, node);
    }
  }
}

output: 输出:

1
2
Segmentation fault: 11

There doesn't seem to be an issue with any but the third node. 除第三个节点外,似乎没有其他问题。 If I comment out the line that adds the second node I get the same error (with only 1 being printed, obviously). 如果我注释掉添加第二个节点的行,则会遇到相同的错误(显然只有1个被打印)。

Your initialization is incomplete 您的初始化不完整

  n1.data = 1;
  n2.data = 2;
  n3.data = 3;

should also set the pointers 还应该设置指针

  n1.data = 1;
  n1.left = NULL;
  n1.right = NULL;

  n2.data = 2;
  n2.left = NULL;
  n2.right = NULL;

  n3.data = 3;
  n3.left = NULL;
  n3.right = NULL;

Problem is occurring because you are not initializing all the member of structure Node type variable. 因为您没有初始化结构Node类型变量的所有成员,所以出现了问题。

I would suggest, you should write a function to initialize the Node type variable, like this: 我建议,您应该编写一个函数来初始化Node类型变量,如下所示:

void init_node(Node * nodeptr, int data)
{
        nodeptr->data = data;
        nodeptr->left = NULL;
        nodeptr->right = NULL;
}

and in your main() (or from where ever you want to initialize) you can simply do: 并在您的main() (或从您要初始化的任何地方)可以简单地执行以下操作:

  init_node(&n1, 1);
  init_node(&n2, 2);
  init_node(&n3, 3);

With this, you will never miss assigning NULL to left and right pointer during initialization of Node type variable and chances of the error occurring because of it will be reduced to a greater extent. 这样,您将永远不会在Node类型变量的初始化过程中错过为left指针和right指针分配NULL的机会,并且由于发生错误的可能性会大大降低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM