For this gist, let’s consider this tree as our data.
A <--------- |
| |
B <- -> C |
| | |
D E |
| | |
F G <- -> H -> |
For the sake of simplicity here is your Node
class used to represent nodes in the a tree (without all the getters and setters).
public class Node {
private ArrayList<Node> children = new ArrayList<>();
}
Here is one implementation for depth first search that does not used a “visited” marker on the nodes. Instead it uses a list of unvisited nodes to achieve such a thing.
public List<Node> search() {
List<Node> visitedNodes = new LinkedList<>();
List<Node> unvisitedNodes = new LinkedList<>();
unvisitedNodes.add(this);
while(!unvisitedNodes.isEmpty()) {
Node currentNode = unvisitedNodes.remove(0);
List<Node> newNodes = currentNode.getChildren()
.stream()
.filter(node -> !visitedNodes.contains(node))
.collect(Collectors.toList());
unvisitedNodes.addAll(0, newNodes);
visitedNodes.add(currentNode);
}
return visitedNodes;
}
So let’s trace what the seach if we are starting from the A
node.
Step 1:
- The values for the lists are
visitedNodes = []
unvisitedNodes = [A]
. Since there is an element inunvisitedNodes
, we enter the loop. - In the loop,
currNode = A
. - We remove
A
from unvisitedNodes sounvisitedNodes = []
. - We add
B, C
to unvisitedNode and we add them at the begining of the list sounvisitedNodes = [B, C]
. - At the end of the loop,
visitedNodes = [A]
.
Step 2:
- The values for the lists are
visitedNodes = [A]
unvisitedNodes = [B, C]
. Since there are elements inunvisitedNodes
, the loop continues. - In the loop,
currNode = B
. - We remove
B
from unvisitedNodes sounvisitedNodes = [C]
. - We add
D
to unvisitedNode and we add them at the begining of the list sounvisitedNodes = [D, C]
. - At the end of the loop,
visitedNodes = [A, B]
.
Step 3:
- The values for the lists are
visitedNodes = [A, B]
unvisitedNodes = [D, C]
. Since there are elements inunvisitedNodes
, the loop continues. - In the loop,
currNode = D
. - We remove
D
from unvisitedNodes sounvisitedNodes = [C]
. - We add
F
to unvisitedNode and we add them at the begining of the list sounvisitedNodes = [F, C]
. - At the end of the loop,
visitedNodes = [A, B, D]
.
Step 4:
- The values for the lists are
visitedNodes = [A, B, D]
unvisitedNodes = [F, C]
. Since there are elements inunvisitedNodes
, the loop continues. - In the loop,
currNode = F
. - We remove
F
from unvisitedNodes sounvisitedNodes = [C]
. - We don’t add any nodes here since
F
is a leaf, sounvisitedNodes = [C]
. - At the end of the loop,
visitedNodes = [A, B, D, F]
.
Step 5:
- The values for the lists are
visitedNodes = [A, B, D, F]
unvisitedNodes = [C]
. Since there are elements inunvisitedNodes
, the loop continues. - In the loop,
currNode = C
. - We remove
C
from unvisitedNodes sounvisitedNodes = []
. - We add
E
to unvisitedNode and we add them at the begining of the list sounvisitedNodes = [E]
. - At the end of the loop,
visitedNodes = [A, B, D, F, C]
.
Step 6:
- The values for the lists are
visitedNodes = [A, B, D, F, C]
unvisitedNodes = [E]
. Since there are elements inunvisitedNodes
, the loop continues. - In the loop,
currNode = E
. - We remove
E
from unvisitedNodes sounvisitedNodes = []
. - We add
G, H
to unvisitedNode and we add them at the begining of the list sounvisitedNodes = [G, H]
. - At the end of the loop,
visitedNodes = [A, B, D, F, C, E]
.
Step 7:
- The values for the lists are
visitedNodes = [A, B, D, F, C, E]
unvisitedNodes = [G, H]
. Since there are elements inunvisitedNodes
, the loop continues. - In the loop,
currNode = G
. - We remove
G
from unvisitedNodes sounvisitedNodes = [H]
. - We don’t add any nodes here since
G
is a leaf, sounvisitedNodes = [H]
. - At the end of the loop,
visitedNodes = [A, B, D, F, C, E, G]
.
Step 8:
- The values for the lists are
visitedNodes = [A, B, D, F, C, E, G]
unvisitedNodes = [H]
. Since there are elements inunvisitedNodes
, the loop continues. - In the loop,
currNode = H
. - We remove
H
from unvisitedNodes sounvisitedNodes = []
. - We don’t add any nodes here since
H
is a not leaf but hasA
as a children and it has already been visited., sounvisitedNodes = []
. - At the end of the loop,
visitedNodes = [A, B, D, F, C, E, G, H]
. - We’re done.