2 %文件名: bfst3 % 版本号: 1.0.0
4 % 作者: Isabelle
5 %单位: specter6 % 修改时间: Sat Oct. 8 1:20:01 2017
7 % 创建时间: Sat Oct. 8 1:20:01 2017
8 %--------------------------------------------------------------------------
9 % blog:http://www.cnblogs.com/isabellezhou
10 % Copyright (Dist) 2017Isabelle All rights reserved.11 %============================================
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class BST{
public static void main(String[] args){
int[][] A=new int[][]{{0,1,1,1,0},{1,0,0,1,0},{1,0,0,0,1},{1,1,0,0,0},{0,0,1,0,0}};
int n=A.length; //元素个数
ArrayList result=new ArrayList();
Queue thequeue = new LinkedList(); //LinkedLis实现queue接口
boolean[] marked=new boolean[n]; //marked[i]标记是否被遍历过,遍历过为true
for (int i = 0; i < n; i++) {
marked[i] = false; //初始化标记数组
}
int startNode=3; //搜索起始结点
thequeue.offer(startNode); //起始结点装入队列
result.add(startNode); //起始结点装入遍历数组
marked[startNode]=true; //更新起始结点的访问标志
while(!thequeue.isEmpty()){ //队列非空
int v1=(int)thequeue.poll();
for (int k= 0; k
if (A[v1][k]>0&&marked[k]==false&&v1!=k) {
thequeue.offer(k); //压入队列
marked[k] = true; //更新访问标志位
result.add(k); //更新遍历数组
}
}
}
System.out.println(result);
}
}