Suppose you had an infinitely large general/non-binary to search. Because the tree is infinitely large, an exhaustive DFS or BFS strategy is not feasible. However, a DFS search with parameter, depth=n, is highly desirable.
Taking this Q/A for inspiration, DFS can be executed
list nodes_to_visit = {root};
while( nodes_to_visit isn't empty ) {
currentnode = nodes_to_visit.take_first();
nodes_to_visit.prepend( currentnode.children );
//do something
}
The key insight is that a stack is used such that nodes that need visiting are inserted at the start of the list and this process repeats until there are no more children to prepend to the stack.
The major problem when considering depth. We might introduce a variable, d, which is initialized at 0 and increment it each time that children are added to the stack. However, with a variable number of children, the stack has no of mechanism to know when d should be decremented (when all the children at a certain depth have been visited.)
How should a depth limited DFS be implemented? (Pseudo code or python preferred.)