Skip to content

Instantly share code, notes, and snippets.

@jackyq2015
Forked from vnprc/Tree.java
Last active January 15, 2017 16:40
Show Gist options
  • Save jackyq2015/50c6ddba65d95162e21cea12774add70 to your computer and use it in GitHub Desktop.
Save jackyq2015/50c6ddba65d95162e21cea12774add70 to your computer and use it in GitHub Desktop.
Two search algorithms for a tree, three implementations
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class Tree {
public List<Node> depthFirstSearchIterative(Node root) {
List<Node> results = new ArrayList<>();
Stack<Node> stack = new Stack<Node>();
Set<Node> visited = new HashSet<>();
stack.add(root);
// each loop pop one node starting from root.
// for not visited, we add nodes into stack;
// for visited(means all Children and itself already in the stack), we just add this node into results.
while(!stack.isEmpty()) {
Node node = stack.pop();
if (!visited.contains(node)) {
visited.add(node);
stack.push(node); // push node back with "visited" flagged. Ready to add to "results"!
for (Node child : node.getChildren()) { // push its children
stack.push(child);
}
} else { // add into results
results.add(node); // the parent will be popped after child. so it is a post-order.
}
}
return results;
}
// For DFS, Recursive is easier. loop until no child found(reach the end).
public List<Node> depthFirstSearchRecursive(Node root) {
List<Node> results = new ArrayList<>();
for (Node child : root.getChildren()) {
results.addAll(depthFirstSearchRecursive(child)); // Recursive. add its children firstly
}
results.add(root); // then add itself. so it is post-order
return results;
}
// For BFS, Iteration is easier. loop until queue empty.
public List<Node> breadthFirstSearch(Node root) {
List<Node> results = new ArrayList<>();
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while(!queue.isEmpty()) {
Node node = queue.remove();
results.add(node);
queue.addAll(node.getChildren()); // for leaf node, append nothing and eventually all the nodes in queue get consumed.
}
return results;
}
public static class Node {
private Object value;
private List<Node> children = new ArrayList<>();
public Node(Object value) {
this.value = value;
}
public void addChild(Node child) {
children.add(child);
}
public List<Node> getChildren() {
return children;
}
@Override
public String toString() {
return value.toString();
}
}
int getLongestPathLength(Node node) {
if (node == null) return 0;
int max = 0;
for (Node child : node.children) {
max = Math.max(getLongestPathLength(child), max);
}
return 1 + max;
}
public static void main(String[] args) {
Tree tree = new Tree();
Node root = new Node("home");
Node documents = new Node("documents");
root.addChild(documents);
documents.addChild(new Node("tax_return_2015.pdf"));
documents.addChild(new Node("sales_receipt.pdf"));
documents.addChild(new Node("christmas_list.txt"));
Node pictures = new Node("pictures");
root.addChild(pictures);
Node kids = new Node("kids");
pictures.addChild(kids);
kids.addChild(new Node("baby.jpg"));
kids.addChild(new Node("vacation.jpg"));
pictures.addChild(new Node("silly.gif"));
// -> home
// -> pictures
// -> silly.gif
// -> kids
// -> baby.jpg
// -> vacation.jpg
// -> documents
// -> tax_return_2015.pdf
// -> sales_receipt.pdf
// -> christmas_list.txt
System.out.println("DFS iterative : " + tree.depthFirstSearchIterative(root));
System.out.println("DFS recursive : " + tree.depthFirstSearchRecursive(root));
System.out.println("BFS iterative : " + tree.breadthFirstSearch(root));
/** Results:
* DFS iterative : [silly.gif, vacation.jpg, baby.jpg, kids, pictures, christmas_list.txt, sales_receipt.pdf, tax_return_2015.pdf, documents, home]
* DFS recursive : [tax_return_2015.pdf, sales_receipt.pdf, christmas_list.txt, documents, baby.jpg, vacation.jpg, kids, silly.gif, pictures, home]
* BFS iterative : [home, documents, pictures, tax_return_2015.pdf, sales_receipt.pdf, christmas_list.txt, kids, silly.gif, baby.jpg, vacation.jpg]
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment