BFS、DFS图的遍历算法实现JAVA

1004 Counting Leaves(30 分)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0~100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1
01 1 02

Sample Output:

0 1

answer



import java.util.*; /** * Created by Boolean on 2018/8/8. */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numbers = scanner.nextInt(); int num_noleaf = scanner.nextInt(); int i = 0; int[] levels = new int[numbers]; DNode[] nodes = new DNode[numbers]; levels[0] = 0; int max_level=0; for(i=0;i<numbers;i++) { DNode dNode = new DNode(i); nodes[i] = dNode; } while(num_noleaf>0) { int vertext = scanner.nextInt(); int num_child = scanner.nextInt(); LinkedList<BNode> bNodes = new LinkedList<>(); while (num_child-->0) { int child = scanner.nextInt(); BNode newNode = new BNode(child-1); bNodes.add(newNode); levels[child-1] = levels[vertext-1]+1; if(levels[child-1]>max_level) max_level = levels[child-1]; } nodes[vertext-1].setBList(bNodes); num_noleaf--; } BFS(nodes,numbers,levels,max_level); DFS(nodes,numbers,levels,max_level); } static class DNode{ private int vertex; private LinkedList<BNode> BList; DNode(int vertex,LinkedList<BNode> BList) { this.vertex = vertex; this.BList = BList; } public DNode(int vertex) { this.vertex = vertex; } public void setBList(LinkedList<BNode> BList){ this.BList = BList; } } private static class BNode{ private int vertex; private Boolean hasChild = false; BNode(int vertex){ this.vertex = vertex; } } //广度遍历算法 private static void BFS(DNode[] dNodes,int numbers,int[] levels,int sum_level) { Queue<DNode> queue = new LinkedList<>();//辅助队列 Boolean[] visited = new Boolean[numbers];//记录结点是否被访问过 int[] res = new int[100];//结果集 for(int i=0;i<numbers;i++)visited[i]=false; visited[0] = true; queue.offer(dNodes[0]);//根结点入队,若是图,则函数参数需要加vertex作为起始结点 if(dNodes[0].BList==null){System.out.printf("1");return;}//只有一个根结点 else System.out.printf("0"); //队列为空则完成遍历 while (!queue.isEmpty()) { DNode dNode = queue.poll(); int index = 0; //遍历所有邻接结点 while (dNode.BList!=null&&index<dNode.BList.size()) { int child_ver = dNode.BList.get(index).vertex; if(!visited[child_ver]) { if(dNodes[child_ver].BList==null){ visited[child_ver]=true;//leaf-node++ res[levels[child_ver]]++; } else { visited[child_ver] = true; queue.offer(dNodes[child_ver]);//入队列 } } index++; } } //输出leaf-node数 for(int i=1;i<=sum_level;i++) { System.out.printf(" %d",res[i]); } } }
//深度遍历
    private static void DFS(DNode[] dNodes,int numbers,int[] levels,int sum_level)
    {
        DNode dNode = dNodes[0];//根结点
        Boolean[] visited = new Boolean[numbers];
        for(int i=0;i<numbers;i++)visited[i]=false;
        int[] res = new  int[numbers];
        //dfs遍历
        dfs_fun(dNode,visited,dNodes,levels,res);
        //根结点是否为空
        if(dNode.BList!=null) System.out.printf("0");
        else System.out.printf("1");
        //输出结果集
        for(int i=1;i<=sum_level;i++) System.out.printf(" %d",res[i]);
    }
    private static void dfs_fun(DNode vertex,Boolean[] visited,DNode[] dNodes,int[] levels,int[] res)
    {
        visited[vertex.vertex] = true;
        int index = 0;
        //遍历邻接结点
        for(int i=0;vertex.BList!=null&&i<vertex.BList.size();i++)
        {
            int node = vertex.BList.get(index).vertex;
            if(!visited[node])
            {
                index++;
                if(dNodes[node].BList==null){ res[levels[node]]++;}
                //递归调用
                dfs_fun(dNodes[node],visited,dNodes,levels,res);
            }
        }
    }
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/Boolean_starki/article/details/82227747
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞