A nice fantasy wit
Vigilante Internsh
Kindergarten Camp
I plan to make
Gifts for the busy
This tool was crea
we’ve gotten valua
There are a lot of
I'm not very good
aieddy.comBreadth-First Search
// ------------------------------------------------------
// A Breadth-First search starts at the first node, and proceeds
// to traverse all of its immediate neighbors (except itself),
// following all arcs. The tree is traversed in left to right order.
// This algorithm has time complexity of O(number of nodes) and space
// complexity of O(max(width, height)).
private Queue queue = new ArrayDeque<>();
private BNode current = root;
private Map> neighbors = new HashMap<>();
public boolean isComplete() {
return current == null;
}
private void breadthFirstTraversal(BNode x) {
Set neighbors = neighbors.get(x);
if (neighbors != null) {
neighbors.remove(current);
}
neighbors.add(current);
queue.add(x);
if (x.getLeft() == null) {
current = x;
} else {
current = x.getLeft();
}
if (x.getRight() == null) {
current = x;
} else {
breadthFirstTraversal(x.getRight());
}
}
private void markUnused() {
Set neighbors = neighbors.get(current);
if (neighbors != null && neighbors != new HashSet()) {
neighbors.remove(current);
}
}
public void complete() {
markUnused();
current = null;
}
private boolean hasPathTo(BNode x) {
// TODO add Breadth First Search for DAG implementation
if (x == current) {
return true;
}
// if x has an in-path from current,
// return true.
if (current.getLeft() != null) {
return hasPathTo(current.getLeft());
}
// if x has an in-path from current,
// return true.
if (current.getRight() != null) {
return hasPathTo(current.getRight());
}
return false;
}
private void findPathTo(BNode x) {
// find the neighbor with the minimum distance from the current node
int index = Collections.min(neighbors.values());
Set neighbors = neighbors.get(current);
if (neighbors != null) {
neighbors.remove(current);
}
neighbors.add(index);
// get the neighbor node
BNode neighbor = neighbors.get(index);
// traverse the tree to find an edge that leads to the node
if (neighbor.getRight() == null) {
neighbor.getRight();
} else {
// mark that we've used this node so that we don't traverse it again
neighbors.put(current, new HashSet(neighbors.get(neighbor.getRight()).contains(current)));
breadthFirstTraversal(neighbor.getRight());
}
}
private void findPathToWithNoUnmarked(BNode x) {
int index = Collections.min(neighbors.values());
Set neighbors = neighbors.get(current);
if (neighbors != null) {
neighbors.remove(current);
}
neighbors.add(index);
// get the neighbor node
BNode neighbor = neighbors.get(index);
// traverse the tree to find an edge that leads to the node
if (neighbor.getRight() == null) {
neighbor.getRight();
} else {
// mark that we've used this node so that we don't traverse it again
neighbors.put(current, new HashSet(neighbors.get(neighbor.getRight()).contains(current)));
breadthFirstTraversal(neighbor.getRight());
}
}
public boolean traverse(BNode x) {
// if current node is the goal, return false. Otherwise
// begin traversal of left subgraph with the node x.
if (x == current) {
return false;
}
// if the node x has a child, follow the edge to it.
if (x.getRight() != null) {
return traverse(x.getRight());
} else {
return false;
}
}
public void finish() {
if (current == null) {
// can't run a BFS without starting point, so just mark all nodes as unvisited.
for (BNode n : root.getChildren()) {
markUnused();
}
} else {
for (BNode n : root.getChildren()) {
if (n == current) {
// don't mark as visited the root that we started at
// instead find a successor, starting at the root itself
markUnused();
current = n.getLeft();
} else if (n.getRight() != null) {
if (!hasPathTo(n.getRight())) {
markUnused();
}
current = n.getRight();
} else {
if (!hasPathToWithNoUnmarked(n.getLeft())) {
markUnused();
}
current = n.getLeft();
}
}
}
breadthFirstTraversal(root);
isComplete();
}
}
public void complete() {
System.out.println("Search complete");
}
public void preprocess() {
searchBFS(root);
System.out.println("Found: " + foundCount);
}
public boolean search(BNode x) {
return traverse(x);
}
}
private class BiSearchSolver
extends AbstractSearchSolver
{
private static final long serialVersionUID = -3846173601378894305L;
private BTree tree;
private BiSearchSolver(BiTreeGraph forest) {
this.tree = forest;
}
@Override
public void preprocess() {
System.out.println("Preprocessing tree:");
printTree(tree);
}
@Override
public void complete() {
System.out.println("Search complete");
}
@Override
public void search(BiNode x) {
System.out.println("searching " + x);
tree.findPathTo(x);
}
}
private static void getMinimalTree(BiTreeGraph graph) {
if (graph.getRoot() == null) {
return;
}
// breadth-first traversal of graph, starting at the root
BreadthFirstSearch search = new BreadthFirstSearch();
search.preprocess();
while (!search.isComplete()) {
search.complete();
}
// get minimal spanning tree
MinimalTree minimal = new MinimalTree();
if (graph.isBipartite()) {
minimal.construct(graph);
} else {
System.out.println("Warning: minimal spanning tree not feasible for this graph.");
System.out.println("Unable to get a minimal spanning tree.");
return;
}
// traverse in both directions and construct a spanning tree graph
BiTreeGraph forest = new BiTreeGraph();
for (BiTreeGraph node : minimal.getSortedSubtrees()) {
forest.addRoot(node);
}
forest.invert();
// construct a BiSearchSolver with the minimal spanning tree
new BiSearchSolver(forest);
}
public static void main(String[] args) {
// create a tree
BiTreeGraph graph = new BiTreeGraph(3);
// add nodes
graph.addRoot(null);
graph.addRoot(new BNode(1, null, null, null));
graph.addRoot(new BNode(2, null, null, null));
graph.addRoot(new BNode(3, null, null, null));
graph.addRoot(new BNode(4, null, null, null));
graph.addRoot(new BNode(5, null, null, null