Improve readability of kotlin code (#1233)

* style(kotlin): Improve kotlin codes readability.

* remove redundant quotes.
This commit is contained in:
curtishd
2024-04-07 18:56:59 +08:00
committed by GitHub
parent e121665772
commit 5725b8a0f1
15 changed files with 85 additions and 101 deletions
@@ -11,9 +11,9 @@ import utils.Vertex
/* 基于邻接表实现的无向图类 */
class GraphAdjList(edges: Array<Array<Vertex?>>) {
// 邻接表,key:顶点,value:该顶点的所有邻接顶点
val adjList: MutableMap<Vertex, MutableList<Vertex>> = HashMap()
val adjList = HashMap<Vertex, MutableList<Vertex>>()
/* 构造函数 */
/* 构造方法 */
init {
// 添加所有顶点和边
for (edge in edges) {
@@ -70,7 +70,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
fun print() {
println("邻接表 =")
for (pair in adjList.entries) {
val tmp = ArrayList<Int>()
val tmp = mutableListOf<Int>()
for (vertex in pair.value) {
tmp.add(vertex.value)
}
@@ -82,7 +82,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
/* Driver Code */
fun main() {
/* 初始化无向图 */
val v: Array<Vertex?> = Vertex.valsToVets(intArrayOf(1, 3, 2, 5, 4))
val v = Vertex.valsToVets(intArrayOf(1, 3, 2, 5, 4))
val edges = arrayOf(
arrayOf(v[0], v[1]),
arrayOf(v[0], v[3]),
@@ -10,10 +10,10 @@ import utils.printMatrix
/* 基于邻接矩阵实现的无向图类 */
class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
val vertices: MutableList<Int> = ArrayList() // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
val adjMat: MutableList<MutableList<Int>> = ArrayList() // 邻接矩阵,行列索引对应“顶点索引”
val vertices = mutableListOf<Int>() // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
val adjMat = mutableListOf<MutableList<Int>>() // 邻接矩阵,行列索引对应“顶点索引”
/* 构造函数 */
/* 构造方法 */
init {
// 添加顶点
for (vertex in vertices) {
@@ -37,7 +37,7 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
// 向顶点列表中添加新顶点的值
vertices.add(value)
// 在邻接矩阵中添加一行
val newRow: MutableList<Int> = mutableListOf()
val newRow = mutableListOf<Int>()
for (j in 0..<n) {
newRow.add(0)
}
@@ -50,7 +50,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
/* 删除顶点 */
fun removeVertex(index: Int) {
if (index >= size()) throw IndexOutOfBoundsException()
if (index >= size())
throw IndexOutOfBoundsException()
// 在顶点列表中移除索引 index 的顶点
vertices.removeAt(index)
// 在邻接矩阵中删除索引 index 的行
@@ -65,7 +66,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
// 参数 i, j 对应 vertices 元素索引
fun addEdge(i: Int, j: Int) {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw IndexOutOfBoundsException()
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
adjMat[i][j] = 1;
adjMat[j][i] = 1;
@@ -75,7 +77,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
// 参数 i, j 对应 vertices 元素索引
fun removeEdge(i: Int, j: Int) {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw IndexOutOfBoundsException()
adjMat[i][j] = 0;
adjMat[j][i] = 0;
}
+8 -8
View File
@@ -11,24 +11,24 @@ import java.util.*
/* 广度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
fun graphBFS(graph: GraphAdjList, startVet: Vertex): List<Vertex> {
fun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {
// 顶点遍历序列
val res: MutableList<Vertex> = ArrayList()
val res = mutableListOf<Vertex?>()
// 哈希表,用于记录已被访问过的顶点
val visited: MutableSet<Vertex> = HashSet()
val visited = HashSet<Vertex>()
visited.add(startVet)
// 队列用于实现 BFS
val que: Queue<Vertex> = LinkedList()
val que = LinkedList<Vertex>()
que.offer(startVet)
// 以顶点 vet 为起点,循环直至访问完所有顶点
while (!que.isEmpty()) {
val vet = que.poll() // 队首顶点出队
res.add(vet) // 记录访问顶点
res.add(vet) // 记录访问顶点
// 遍历该顶点的所有邻接顶点
for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet)) continue // 跳过已被访问的顶点
que.offer(adjVet) // 只入队未访问的顶点
if (visited.contains(adjVet))
continue // 跳过已被访问的顶点
que.offer(adjVet) // 只入队未访问的顶点
visited.add(adjVet) // 标记该顶点已被访问
}
}
+6 -8
View File
@@ -15,11 +15,12 @@ fun dfs(
res: MutableList<Vertex?>,
vet: Vertex?
) {
res.add(vet) // 记录访问顶点
res.add(vet) // 记录访问顶点
visited.add(vet) // 标记该顶点已被访问
// 遍历该顶点的所有邻接顶点
for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet)) continue // 跳过已被访问的顶点
if (visited.contains(adjVet))
continue // 跳过已被访问的顶点
// 递归访问邻接顶点
dfs(graph, visited, res, adjVet)
}
@@ -27,14 +28,11 @@ fun dfs(
/* 深度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
fun graphDFS(
graph: GraphAdjList,
startVet: Vertex?
): List<Vertex?> {
fun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {
// 顶点遍历序列
val res: MutableList<Vertex?> = ArrayList()
val res = mutableListOf<Vertex?>()
// 哈希表,用于记录已被访问过的顶点
val visited: MutableSet<Vertex?> = HashSet()
val visited = HashSet<Vertex?>()
dfs(graph, visited, res, startVet)
return res
}