7 Mart 2021 Pazar

Traverser Sınıfı

Giriş
Şu satırı dahil ederiz.
import com.google.common.graph.Traverser;
Traverse algoritmaları olarak şunlar kullanılabilir
breadthFirst(Iterable<? extends N> startNodes)
Iterable over the nodes reachable from any of the startNodes, in the order of a breadth-first traversal
breadthFirst(N startNode)
Iterable over the nodes reachable from startNode, in the order of a breadth-first traversal
Örnek
Şöyle yaparız
Node root = new Node("root");
MutableGraph<Node> myGraph = GraphBuilder.directed().build();
Node node1=new Node(" 1 ");
Node node2=new Node(" 2 ");
Node node3=new Node(" 3 ");
Node node4=new Node(" 4 ");
Node node5=new Node(" 5 ");
myGraph.putEdge(root, node1);
myGraph.putEdge(node1, node2);
myGraph.putEdge(node1, node3);
myGraph.putEdge(node2, node4);
myGraph.putEdge(node2, node5);
//Print the nodes Depth First
Traverser.forGraph(myGraph).breadthFirst(root)
  .forEach(x->System.out.println(x));
Çıktı olarak şunu alırız
1
3 2 5 4
depthFirstPostOrder(Iterable<? extends N> startNodes)
Iterable over the nodes reachable from any of the startNodes, in the order of a depth-first post-order traversal
Örnek
Şöyle yaparız
Node root = new Node("root");
MutableGraph<Node> myGraph = GraphBuilder.directed().build();
Node node1=new Node(" 1 ");
Node node2=new Node(" 2 ");
Node node3=new Node(" 3 ");
Node node4=new Node(" 4 ");
Node node5=new Node(" 5 ");
myGraph.putEdge(root, node1);
myGraph.putEdge(node1, node2);
myGraph.putEdge(node1, node3);
myGraph.putEdge(node2, node4);
myGraph.putEdge(node2, node5);
//Print the nodes Depth First
Traverser.forGraph(myGraph).depthFirstPostOrder(root)
  .forEach(x->System.out.println(x));
root ->(1)
1 -> (2,3)
2 -> (4,5)
Çıktı olarak şunu alırız
1
3
5
4
2
depthFirstPostOrder(N startNode) metodu
Iterable over the nodes reachable from startNode, in the order of a depth-first post-order traversal
Örnek
Şöyle yaparız
Node root = new Node("root");
Graph<Node> graph = GraphBuilder.directed()
  .nodeOrder(ElementOrder.insertion())
  .<Node>immutable()
  .putEdge(root, new Node("one"))
  .putEdge(root, new Node("two"))
  .putEdge(root, new Node("three"))
  .build();

//Print the nodes in traversal order.
Traverser.forGraph(graph).depthFirstPostOrder(root)
  .forEach(x->System.out.println(x));
depthFirstPreOrder(Iterable<? extends N> startNodes)
Iterable over the nodes reachable from any of the startNodes, in the order of a depth-first pre-order traversal
forGraph(SuccessorsFunction<N> graph)
Creates a new traverser for the given general graph.
forTree(SuccessorsFunction<N> tree)
Creates a new traverser for a directed acyclic graph that has at most one path from the start node(s) to any node reachable from the start node(s), and has no paths from any start node to any other start node, such as a tree or forest