如下图:
- 1、广度优先遍历
英文缩写为BFS即Breadth FirstSearch。其过程检验来说是对每一层节点依次访问,访问完一层进入下一层,而且每个节点只能访问一次。对于上面的例子来说,广度优先遍历的 结果是:8, 3, 9, 17, 31, 37, 11, 24, 92, 15, 2, 9, 67
- 2、深度优先遍历
英文缩写为DFS即Depth First Search。其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次。对于上面的例子来说深度优先遍历的结果就是:8, 3, 31, 37, 11, 9, 24, 92, 15, 17, 2, 9, 67
- 3、递归算法实现
定义树的类结构如下:
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import java.util.List;
public class TreeNode {
//内容
private String content;
//子节点集合,默认从左到右
private List<TreeNode> childs;
//父节点
private TreeNode parent;
public TreeNode(String content) {
this.content = content;
}
// 添加子节点
public void addChild(TreeNode node){
if(node == null || StringUtils.isBlank(this.content)){
return;
}
if(childs == null){
childs = Lists.newArrayList();
}
childs.add(node);
node.setParent(this);
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<TreeNode> getChilds() {
return childs;
}
public void setChilds(List<TreeNode> childs) {
this.childs = childs;
}
public TreeNode getParent() {
return parent;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
}
算法实现及测试:
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import java.util.List;
public class TraverseTest {
public static void main(String[] args) {
TreeNode parentNode = new TreeNode("8");
TreeNode child_1 = new TreeNode("3");
TreeNode child_2 = new TreeNode("9");
TreeNode child_3 = new TreeNode("17");
parentNode.addChild(child_1);
parentNode.addChild(child_2);
parentNode.addChild(child_3);
TreeNode child_1_1 = new TreeNode("31");
TreeNode child_1_2 = new TreeNode("37");
TreeNode child_1_3 = new TreeNode("11");
child_1.addChild(child_1_1);
child_1.addChild(child_1_2);
child_1.addChild(child_1_3);
TreeNode child_2_1 = new TreeNode("24");
TreeNode child_2_2 = new TreeNode("92");
TreeNode child_2_3 = new TreeNode("15");
child_2.addChild(child_2_1);
child_2.addChild(child_2_2);
child_2.addChild(child_2_3);
TreeNode child_3_1 = new TreeNode("2");
TreeNode child_3_2 = new TreeNode("9");
TreeNode child_3_3 = new TreeNode("67");
child_3.addChild(child_3_1);
child_3.addChild(child_3_2);
child_3.addChild(child_3_3);
System.out.println("breadth result="+breadthFirstTraverse(parentNode));
System.out.println("depth result="+depthFirstTraverse(parentNode));
}
/**
* 广度优先遍历
* @param node
* @return
*/
private static List<String> breadthFirstTraverse(TreeNode node){
List<String> result = Lists.newArrayList();
if(node == null){
return null;
}
if(node.getParent()==null){
result.add(node.getContent());
}
if(CollectionUtils.isNotEmpty(node.getChilds())){
for (TreeNode child : node.getChilds()) {
result.add(child.getContent());
}
for (TreeNode child : node.getChilds()) {
result.addAll(breadthFirstTraverse(child));
}
}
return result;
}
/**
* 深度优先
* @param node
* @return
*/
private static List<String> depthFirstTraverse(TreeNode node){
List<String> result = Lists.newArrayList();
if(node == null){
return null;
}
if(node.getParent()!=null){
return depthFirstTraverse(node.getParent());
}
result.add(node.getContent());
if(CollectionUtils.isNotEmpty(node.getChilds())){
for (TreeNode child : node.getChilds()) {
child.setParent(null);
result.addAll(depthFirstTraverse(child));
}
}
return result;
}
}
运行结果如下:
breadth result=[8, 3, 9, 17, 31, 37, 11, 24, 92, 15, 2, 9, 67]
depth result=[8, 3, 31, 37, 11, 9, 24, 92, 15, 17, 2, 9, 67]