I used to use "visit" flags in relational projects to incrementally scan trees. You start by putting the top node in a node inventory table, scan it, and add any new nodes (branches) to the inventory table, with the "visit" flag initialized with False. (Index the visit flag.) You keep querying and scanning nodes until all the visit flags are True. Pseudo-Code:
1. Add first node to inventory, set node.visit=false.
2. Query for 1st of any nodes with visit=false. (Order doesn't really matter in the end.)
3. If none found, stop.
4. If the found node has branches, add them to inventory with visit=false.
When you say "relational projects" and "query for", you don't mean that you were using an RDBMS? Because, in that case, this pseudo-code represents a gigantic waste of time and resources.
1. Add first node to inventory, set node.visit=false.
2. Query for 1st of any nodes with visit=false. (Order doesn't really matter in the end.)
3. If none found, stop.
4. If the found node has branches, add them to inventory with visit=false.
5. Process current node.
6. Mark current node with visit=true.
7. Go to 2.