数据结构 树的广度优先遍历和深度优先遍历,java递归算法实现

  如下图:

  •  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]

 

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值