Marco? Polo!
-
What data structure is used to implement DFS?
Stack
-
What data structure is typically used to implement BFS?
Queue
-
Which one can be done recursively? (the clue should be the data structure)
A DFS. It is possible to treat each first node as the beginning of a new recursive block, performing the same operations on it's children.
-
Which one would you use to print a list of all the nodes in a tree, starting with depth 1, then depth 2, then depth 3 etc.?
The BFS is best suited for this, this is because the printing of each depth can be done during the natural path of the BFS algorithm.
-
Searching a simple tree of nodes where each Node has an array of child nodes (some_node.children) using DFS.
- Push the root node onto the stack
- Pop a node off the stack
- Return if is the search value
- Else look at any unsearched children
- Push "worst" nodes onto the stack first
- Push "best" nodes onto the stack last
- Repeat from step 2 until search value is found or no nodes left
-
Searching the same tree using BFS.
- Starting at the root node
- Return if is the search value
- Else look at all the direct children of current node
- Repeat from step 2 until search value is found or no nodes left
A data structures and algorithms Ruby challenge from the Viking Code School