-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraph.java
More file actions
53 lines (48 loc) · 1.56 KB
/
Graph.java
File metadata and controls
53 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package Graph;
public abstract class Graph<E extends Number> {
protected int vertexNum;
Graph(int vertexNum){
this.vertexNum=vertexNum;
}
/**
* 遍历
* @param choice true为广度优先,false为深度优先
*/
public void traverse(boolean choice){
boolean[] flags = new boolean[vertexNum];
if (choice) {
System.out.print("广度优先遍历结果为:");
for (int i=0;i<vertexNum;++i){
if (!flags[i]){//如果当前节点没有访问过,则从当前节点开始深度优先遍历
bfsTraverse(i,flags);
}
}
}else{
System.out.print("深度优先遍历结果为:");
for (int i=0;i<vertexNum;++i){
if (!flags[i]){//如果当前节点没有访问过,则从当前节点开始深度优先遍历
dfsTraverse(i,flags);
}
}
}
}
/**
* 给图中增加一条边
* @param headIndex 起点索引
* @param tailIndex 终点索引
* @param weight 权重值
*/
protected void addEdge(int headIndex,int tailIndex,E weight){}
/**
* 深度优先遍历
* @param curNode 当前访问节点
* @param flags 访问标记数组
*/
protected abstract void dfsTraverse(int curNode,boolean[] flags);
/**
* 广度优先遍历
* @param curNode 当前访问节点
* @param flags 访问标记数组
*/
protected abstract void bfsTraverse(int curNode,boolean[] flags);
}