Package com.google.mu.util.graph
Class BinaryTreeWalker<N>
java.lang.Object
com.google.mu.util.graph.Walker<N>
com.google.mu.util.graph.BinaryTreeWalker<N>
- Type Parameters:
N
- the tree node type
Walker for binary tree topology (see
Walker.inBinaryTree()
).
Besides pre-order
, post-order
and breadth-first
traversals, also supports in-order
.
- Since:
- 4.2
-
Method Summary
Modifier and TypeMethodDescriptionbreadthFirstFrom
(Iterable<? extends N> roots) Returns a lazy stream for breadth-first traversal fromroot
.inOrderFrom
(Iterable<? extends N> roots) Returns a lazy stream for in-order traversal fromroots
.inOrderFrom
(N... roots) Returns a lazy stream for in-order traversal fromroots
.postOrderFrom
(Iterable<? extends N> roots) Returns a lazy stream for post-order traversal fromroot
.preOrderFrom
(Iterable<? extends N> roots) Returns a lazy stream for pre-order traversal fromroots
.Methods inherited from class com.google.mu.util.graph.Walker
breadthFirstFrom, inBinaryTree, inGraph, inGraph, inTree, postOrderFrom, preOrderFrom
-
Method Details
-
breadthFirstFrom
Returns a lazy stream for breadth-first traversal fromroot
. Empty stream is returned ifroots
is empty.- Specified by:
breadthFirstFrom
in classWalker<N>
-
preOrderFrom
Returns a lazy stream for pre-order traversal fromroots
. Empty stream is returned ifroots
is empty.- Specified by:
preOrderFrom
in classWalker<N>
-
postOrderFrom
Returns a lazy stream for post-order traversal fromroot
. Empty stream is returned ifroots
is empty.For small or medium sized in-memory trees, it's equivalent and more efficient to first collect the nodes into a list in "reverse post order", and then use
Collections.reverse()
, as in:
Or, use theList<Node> nodes = Walker.inBinaryTree(Tree::right, Tree::left) // 1. flip left to right .preOrderFrom(root) // 2. pre-order .collect(toCollection(ArrayList::new)); // 3. in reverse-post-order Collections.reverse(nodes); // 4. reverse to get post-order
toListAndThen()
collector to do it in one-liner:List<Node> nodes = Walker.inBinaryTree(Tree::right, Tree::left) .preOrderFrom(root) .collect(toListAndThen(Collections::reverse));
- Specified by:
postOrderFrom
in classWalker<N>
-
inOrderFrom
Returns a lazy stream for in-order traversal fromroots
. Empty stream is returned ifroots
is empty. -
inOrderFrom
Returns a lazy stream for in-order traversal fromroots
. Empty stream is returned ifroots
is empty.
-