Reputation: 65
I'm trying to insert something into a binary tree, and I wanted to check whats inside by using an in-order print function. An exception seems to be thrown at "print_bst_node(root->left);" that says "Exception thrown: read access violation." each time I run the program. Is there something wrong with this function?
Edit, added the insert_bst wrapper function.
//bst.h
typedef struct bstNode { //pointer to the bst
long data; //storing long data types from -2,147,483,648 to 2,147,483,647
List courses; //this is the list of courses
struct bstNode *left; //left child of the tree
struct bstNode *right; //right child of the tree
} *BSTNodePtr;
typedef struct bst {
BSTNodePtr root; //points to the root of the tree
} BST;
//bst.c
void print_bst_node(BSTNodePtr root) {
if (root != NULL) {
print_bst_node(root->left);
printf("%ld ", root->data);
print_bst_node(root->right);
}
else {
printf("Nothing to see here");
}
}
BSTNodePtr insert_bst_node(BSTNodePtr self, long n) {
if (self == NULL) {
self = malloc(sizeof *self);
self->data = n;
self->courses = new_list(); //creates a new linked list in the binary search tree nodes.
self->left = self->right = NULL;
}
else if (n < self->data) {
self->left = insert_bst_node(self->left, n);
}
else if (n > self->data) {
self->right = insert_bst_node(self->right, n);
}
return self;
}
void insert_bst(BST *self, long n) { //wrapper function for insert_bst_node
self->root = insert_bst_node(self->root, n);
}
//main.c
void add_to_bst(BST *self) {
long input = 0;
printf("Enter student ID\n");
scanf("%ld", &input);
insert_bst(self, input);
print_bst_node(self);
}
Upvotes: 2
Views: 219
Reputation: 12659
The problem is occurring because you are passing an incompatible pointer as argument to print_bst_node()
function. The print_bst_node()
parameter is of type BSTNodePtr
:
void print_bst_node(BSTNodePtr root) {
and you are passing self
to it:
print_bst_node(self);
which is a pointer to BST
type:
void add_to_bst(BST *self) {
Instead, you should pass self->root
to print_bst_node()
:
print_bst_node(self->root);
The compiler must be throwing a warning message for passing incompatible pointer type.
Upvotes: 2
Reputation: 407
When adding new nodes, you should attribute its value and then ->left ->right to NULL, or you will have problems in functions like that. Since the stop case is looking for NULL and there wont be one (it will most likely read garbish that is at that memory point).
After that the function you created is perfectly fine :)
Upvotes: 0
Reputation: 144685
The print_bst_node
function will print an awful lot of "Nothing to see here"
messages!
Apart from that, is seems fine, as long as we can infer... you did not provide the declaration for BSTNodePtr
.
The problem is elsewhere:
left
and right
might not be properly initialized upon allocation or declarationUpdate your question with code for a complete example for a more elaborate answer.
Upvotes: 1
Reputation: 7118
Whenever you create a node, the node must be having all its left and right children set as NULL
or (void *) 0
Upvotes: 0