This is more of a logic problem I'm having a hard time figuring out, I'll post psuedo code, please let me know if anything is unclear.
I'm trying to write a function that will search for multiple instances of a target in an N-array tree (each node has 0 to N children).
- If it finds 1 or more instances, it prints them all.
- If it finds no instances of the target, the whole tree must be deleted
I'm trying to solve this recursively, this is what I have:
bool foo(node){
bool found = false
if node.value = target:
print target
return true
else:
return false
for child in node.children:
found = found or foo(child)
if not found:
*run deletion process*
}
The problem with this is the very first if statement:
if node.value = target:
print target
return true
else:
return false
As soon as it finds the first instance of the target, it returns and stops running. But I want it to find and print ALL instances. But doing this recursively, I'm having a hard time figuring out how to order the code.