Constructor and Description 

Traverser() 
Modifier and Type  Method and Description 

abstract Iterable<N> 
breadthFirst(N startNode)
Returns an unmodifiable iterable over the nodes in the graph, using breadthfirst traversal.

abstract Iterable<N> 
depthFirstPostOrder(N startNode)
Returns an unmodifiable iterable over the nodes in the graph, using depthfirst postorder
traversal.

abstract Iterable<N> 
depthFirstPreOrder(N startNode)
Returns an unmodifiable iterable over the nodes in the graph, using depthfirst preorder
traversal.

static <N> Traverser<N> 
forGraph(SuccessorsFunction<N> graph)
Creates a new traverser for the given general
graph . 
static <N> Traverser<N> 
forTree(SuccessorsFunction<N> tree)
Creates a new traverser for a directed acyclic graph that has at most one path from the start
node to any node reachable from the start node, such as a tree.

public Traverser()
public static <N> Traverser<N> forGraph(SuccessorsFunction<N> graph)
graph
.
If graph
is known to be treeshaped, consider using forTree(SuccessorsFunction)
instead.
Performance notes
equals()
and
hashCode()
implementations.
graph
 SuccessorsFunction
representing a general graph that may have cycles.public static <N> Traverser<N> forTree(SuccessorsFunction<N> tree)
Providing graphs that don't conform to the above description may lead to:
forGraph(SuccessorsFunction)
instead.
Performance notes
Examples
This is a valid input graph (all edges are directed facing downwards):
a b c
/ \ / \ 
/ \ / \ 
d e f g


h
This is not a valid input graph (all edges are directed facing downwards):
a b
/ \ / \
/ \ / \
c d e
\ /
\ /
f
because there are two paths from b
to f
(b>d>f
and b>e>f
).
Note on binary trees
This method can be used to traverse over a binary tree. Given methods leftChild(node)
and rightChild(node)
, this method can be called as
Traverser.forTree(node > ImmutableList.of(leftChild(node), rightChild(node)));
tree
 SuccessorsFunction
representing a directed acyclic graph that has at most
one path between any two nodespublic abstract Iterable<N> breadthFirst(N startNode)
Example: The following graph with startNode
a
would return nodes in
the order abcdef
(assuming successors are returned in alphabetical order).
b  a  d
 
 
e  c  f
The behavior of this method is undefined if the nodes, or the topology of the graph, change while iteration is in progress.
The returned iterable can be iterated over multiple times. Every iterator will compute its next element on the fly. It is thus possible to limit the traversal to a certain number of nodes as follows:
Iterables.limit(Traverser.forGraph(graph).breadthFirst(node), maxNumberOfNodes);
See Wikipedia for more info.
public abstract Iterable<N> depthFirstPreOrder(N startNode)
Example: The following graph with startNode
a
would return nodes in
the order abcdef
(assuming successors are returned in alphabetical order).
b  a  f
 
 
c  d  e
The behavior of this method is undefined if the nodes, or the topology of the graph, change while iteration is in progress.
The returned iterable can be iterated over multiple times. Every iterator will compute its next element on the fly. It is thus possible to limit the traversal to a certain number of nodes as follows:
Iterables.limit(
Traverser.forGraph(graph).depthFirstPreOrder(node), maxNumberOfNodes);
See Wikipedia for more info.
public abstract Iterable<N> depthFirstPostOrder(N startNode)
Example: The following graph with startNode
a
would return nodes in
the order edcbfa
(assuming successors are returned in alphabetical order).
b  a  f
 
 
c  d  e
The behavior of this method is undefined if the nodes, or the topology of the graph, change while iteration is in progress.
The returned iterable can be iterated over multiple times. Every iterator will compute its next element on the fly. It is thus possible to limit the traversal to a certain number of nodes as follows:
Iterables.limit(
Traverser.forGraph(graph).depthFirstPostOrder(node), maxNumberOfNodes);
See Wikipedia for more info.
Copyright © 2010–2017. All rights reserved.