I'm new to stackoverflow so feel free to change or edit my question as necessary.
I have the following code that I'm trying to fix, it is essentially just a inorder tree traversal:
void *inorder_traversal(void *heapstart, uint32_t size, void *address) {
struct node *head = (struct node* )heapstart;
if (head->left_child == NULL) {
if (condition1) {
if (condition2) {
address = head->address;
}
}
} else {
inorder_traversal(head->left_child, size, address);
inorder_traversal(head->right_child, size, address);
}
return address;
}
I'm currently trying to return a void * that is stored inside one of the nodes I'm going through. This node must satisfy the condition1 (size < pow(2, head->initial_size) && size > pow(2, (head->initial_size) - 1)) and condition2 head->current_state == free. I have removed these from the code for ease of reading.
My issue arises when I am trying to extract the address and return it. I have that address = head->address. However because head is changing whenever recursion occurs, head->address always points to the last nodes head->address and not head->address that fulfils my condition.
Essentially i'm asking if there is a way to break the traversal whenever the condition is satisfied and return the address that I have stored.
Someone suggested I wasn't returning the right value, however i'm not sure what is meant by that.
Please let me know if there is any confusion.
head->left_child == NULLcheck?left_childandright_childand they will beNULLwhen initialised. So checking if theres aleft_childis enough for me to verify its a leaf node.addressin this function. You only use it as an assignment target and, being a value parameter, it is ultimately worthless, since that reaping is lost anyway once the function returns.void* address;uninitialised in my main. Then pass it into this function and instead of returningvoid*i would instead havevoid, I would access it in my main after it had been initialised in this function.