I'm trying to write code that takes a binary search tree and returns the max value within the struct.
However, when I compile with the test files below, I'm getting segmentation faults, and I can't figure out why. Any help would be appreciated. Thank you.
My code:
int helper_get_max(BTnode_t *root){ //helper function
int a = root->value;
int b = helper_get_max(root->left);
int c = helper_get_max(root->right);
if (b>a){
a = b;
}
if(c>a){
a = c;
return a;
}
}
int get_max(BST_t* tree){
if(tree->root==NULL)
return -1;
int a = helper_get_max(tree->root);
return a;
}
Test code it's supposed to work with:
bool test_q2a() {
BST_t *bst = create_BST();
BST_insert(bst, 125);
if (get_max(bst) == 125) {
printf("AC\n");
return true;
}
else {
printf("WA\n");
return false;
}
}
int main() {
test_q2a();
return 0;
}
As stated above, when ran with this test, it gives a segmentation error.