My code is to find a value in AVL tree that is strictly higher than the value inputted. I have tried to use inorder traversal approach it got stuck at saving data to array
I'm trying to traverse my BST using recursion and also save data to an array for every recursion. The problem is that although I have already incremented my pos every time I save data, the data seems to be overridden, not as my expectation.
void TreeSet::inorderRec(AVLNode *root, int *arr, int pos) {
if (root) {
inorderRec(root->left, arr, pos);
arr[pos++] = root->key;
inorderRec(root->right, arr, pos + 1);
}
}
int TreeSet::higher(int val) {
// TODO
AVLNode *temp = root;
int *arr = new int[count];
int pos = 0;
if (root) {
inorderRec(root, arr, pos);
for (int i = 0; i < count; i++)
if (arr[i] > val)
return arr[i];
}
return -1;
}
I expected to get an array in order and to find the value I want