I am creating a binary tree as small project to learn C. Here is all the code you need.
typedef struct {
int value;
struct Node *leftchild;
struct Node *rightchild;
struct Node *parent;
} Node;
typedef struct {
Node *root;
} BinaryTree;
void newBT( BinaryTree *_bt ) {
_bt->root = 0;
return;
};
Node* makeNode(int _value ) {
Node *node;
node->value = _value;
node->leftchild = 0;
node->rightchild = 0;
node->parent = 0;
return node;
};
void insert( BinaryTree* _bt, int _value ) {
_bt->root = makeNode(_value);
return;
};
Now, I have tested my makeNode() function and it works as intended and I checked it using a has() function which traverses the list and returns true if it finds a node with the given value, returns false otherwise. It is the first commented out line in main() below. When I pass my BinaryTree into insert() and declare the same line using a pointer I get a Bus error:10.
int main () {
BinaryTree bt;
newBT(&bt);
//bt.root = makeNode(14);
insert(&bt, 14);
//printf("%d \n", has(&bt, 14));
return 0;
}
Any suggestions?