当我尝试使用 C 中的结构创建二叉树时出现总线错误。请建议我一个解决此总线错误的解决方案。我在 Mac OSX 上编译。
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* NewNode(int data) {
struct node* inode ;
inode->data = data;
inode->left = NULL;
inode->right = NULL;
printf("%d\n", inode->data);
return(inode);
}
struct node* insert(struct node* node ,int data){
if(node == NULL)
return(NewNode(data));
else{
if(data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return(node);
}
}
struct node* build123a() {
struct node* root = newNode(2);
struct node* lChild = newNode(1);
struct node* rChild = newNode(3);
root->left = lChild;
root->right= rChild;
return(root);
}
int main(void) {
build123a();
}
输出:总线错误 10