Tell us more about the Component graph.
- I see that it's a circuit. Does that mean the graph can have cycles?
- Do you have a separate collection of the nodes in the graph? That would make it trivial.
Normally you use a "visited" flag to indicate that you have processed a node. In this case, it's already set and you want clear it.
1 2 3 4 5 6 7 8 9 10
// Clear the visited flag. This assumes that upon the first call, the visited flag is set in all nodes.
void Circuit :: clearVisit(Component* node){
if (node == nullptr) return; // empty node
if (!node->getVisited()) return; // flag was already cleared by a recursive call
node->clearVisited(); // Clear the flag on this node. This line and the line
// above prevent infinite recursion
// when the circuit has loops
clearVisited(node->leftChild); // recursive call to (possibly null) left child
clearVisited(node->rightChild); // recursive call to (possibly null) right child
}
Can you show the full code and the full problem description if this is a homework assignment. Sometimes students omit details that turn out to be important.