diff --git a/codes/cpp/chapter_graph/graph_adjacency_list.cpp b/codes/cpp/chapter_graph/graph_adjacency_list.cpp index a7f249d70..5a38b3925 100644 --- a/codes/cpp/chapter_graph/graph_adjacency_list.cpp +++ b/codes/cpp/chapter_graph/graph_adjacency_list.cpp @@ -14,8 +14,9 @@ struct Vertex { /* 基于邻接表实现的无向图类 */ class GraphAdjList { - // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象 - unordered_map> adjList; // 邻接表(使用哈希表实现) + // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率 + // 请注意,adjList 中的元素是 Vertex 对象 + unordered_map> adjList; public: /* 构造方法 */ @@ -52,7 +53,7 @@ public: /* 添加顶点 */ void addVertex(Vertex* vet) { if (adjList.count(vet)) return; - // 在邻接表中添加一个新链表(即 HashSet) + // 在邻接表中添加一个新链表 adjList[vet] = unordered_set(); } @@ -60,9 +61,9 @@ public: void removeVertex(Vertex* vet) { if (!adjList.count(vet)) throw invalid_argument("不存在顶点"); - // 在邻接表中删除顶点 vet 对应的链表(即 HashSet) + // 在邻接表中删除顶点 vet 对应的链表 adjList.erase(vet); - // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边 + // 遍历其它顶点的链表,删除所有包含 vet 的边 for (auto& [key, set_] : adjList) { set_.erase(vet); } diff --git a/codes/cpp/chapter_tree/binary_tree_bfs.cpp b/codes/cpp/chapter_tree/binary_tree_bfs.cpp index c3694eb9c..8b2ba2d2e 100644 --- a/codes/cpp/chapter_tree/binary_tree_bfs.cpp +++ b/codes/cpp/chapter_tree/binary_tree_bfs.cpp @@ -7,7 +7,7 @@ #include "../include/include.hpp" /* 层序遍历 */ -vector hierOrder(TreeNode* root) { +vector levelOrder(TreeNode* root) { // 初始化队列,加入根结点 queue queue; queue.push(root); @@ -35,7 +35,7 @@ int main() { PrintUtil::printTree(root); /* 层序遍历 */ - vector vec = hierOrder(root); + vector vec = levelOrder(root); cout << endl << "层序遍历的结点打印序列 = "; PrintUtil::printVector(vec); diff --git a/codes/csharp/chapter_tree/binary_tree_bfs.cs b/codes/csharp/chapter_tree/binary_tree_bfs.cs index 0cbbf62f5..2edd07eef 100644 --- a/codes/csharp/chapter_tree/binary_tree_bfs.cs +++ b/codes/csharp/chapter_tree/binary_tree_bfs.cs @@ -13,7 +13,7 @@ public class binary_tree_bfs { /* 层序遍历 */ - public List hierOrder(TreeNode root) + public List levelOrder(TreeNode root) { // 初始化队列,加入根结点 Queue queue = new(); @@ -41,7 +41,7 @@ public class binary_tree_bfs Console.WriteLine("\n初始化二叉树\n"); PrintUtil.PrintTree(root); - List list = hierOrder(root); + List list = levelOrder(root); Console.WriteLine("\n层序遍历的结点打印序列 = " + string.Join(",", list.ToArray())); } } diff --git a/codes/go/chapter_graph/graph_adjacency_list.go b/codes/go/chapter_graph/graph_adjacency_list.go index 887384dd8..0d5916e37 100644 --- a/codes/go/chapter_graph/graph_adjacency_list.go +++ b/codes/go/chapter_graph/graph_adjacency_list.go @@ -24,8 +24,8 @@ func newVertex(val int) vertex { /* 基于邻接表实现的无向图类 */ type graphAdjList struct { - // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象 - // 邻接表(使用哈希表实现), 使用哈希表模拟集合 + // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率 + // 请注意,adjList 中的元素是 Vertex 对象 adjList map[vertex]map[vertex]struct{} } @@ -78,7 +78,7 @@ func (g *graphAdjList) addVertex(vet vertex) { if ok { return } - // 在邻接表中添加一个新链表(即 set) + // 在邻接表中添加一个新链表 g.adjList[vet] = make(map[vertex]struct{}) } @@ -90,7 +90,7 @@ func (g *graphAdjList) removeVertex(vet vertex) { } // 在邻接表中删除顶点 vet 对应的链表 delete(g.adjList, vet) - // 遍历其它顶点的链表(即 Set),删除所有包含 vet 的边 + // 遍历其它顶点的链表,删除所有包含 vet 的边 for _, set := range g.adjList { // 操作 delete(set, vet) diff --git a/codes/go/chapter_tree/binary_tree_bfs.go b/codes/go/chapter_tree/binary_tree_bfs.go index c10f618e0..8ec46fbe7 100644 --- a/codes/go/chapter_tree/binary_tree_bfs.go +++ b/codes/go/chapter_tree/binary_tree_bfs.go @@ -11,7 +11,7 @@ import ( ) /* 层序遍历 */ -func hierOrder(root *TreeNode) []int { +func levelOrder(root *TreeNode) []int { // 初始化队列,加入根结点 queue := list.New() queue.PushBack(root) diff --git a/codes/go/chapter_tree/binary_tree_bfs_test.go b/codes/go/chapter_tree/binary_tree_bfs_test.go index eafd51bcc..7b1bbf1f5 100644 --- a/codes/go/chapter_tree/binary_tree_bfs_test.go +++ b/codes/go/chapter_tree/binary_tree_bfs_test.go @@ -11,7 +11,7 @@ import ( . "github.com/krahets/hello-algo/pkg" ) -func TestHierOrder(t *testing.T) { +func TestLevelOrder(t *testing.T) { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 root := ArrToTree([]any{1, 2, 3, 4, 5, 6, 7}) @@ -19,6 +19,6 @@ func TestHierOrder(t *testing.T) { PrintTree(root) // 层序遍历 - nums := hierOrder(root) + nums := levelOrder(root) fmt.Println("\n层序遍历的结点打印序列 =", nums) } diff --git a/codes/java/chapter_tree/binary_tree_bfs.java b/codes/java/chapter_tree/binary_tree_bfs.java index 578ed2b81..246921dc6 100644 --- a/codes/java/chapter_tree/binary_tree_bfs.java +++ b/codes/java/chapter_tree/binary_tree_bfs.java @@ -11,7 +11,7 @@ import java.util.*; public class binary_tree_bfs { /* 层序遍历 */ - static List hierOrder(TreeNode root) { + static List levelOrder(TreeNode root) { // 初始化队列,加入根结点 Queue queue = new LinkedList<>() {{ add(root); }}; // 初始化一个列表,用于保存遍历序列 @@ -35,7 +35,7 @@ public class binary_tree_bfs { PrintUtil.printTree(root); /* 层序遍历 */ - List list = hierOrder(root); + List list = levelOrder(root); System.out.println("\n层序遍历的结点打印序列 = " + list); } } diff --git a/codes/javascript/chapter_graph/graph_adjacency_list.js b/codes/javascript/chapter_graph/graph_adjacency_list.js index 586e4a06b..86b934ca1 100644 --- a/codes/javascript/chapter_graph/graph_adjacency_list.js +++ b/codes/javascript/chapter_graph/graph_adjacency_list.js @@ -14,7 +14,10 @@ class Vertex { /* 基于邻接表实现的无向图类 */ class GraphAdjList { + // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率 + // 请注意,adjList 中的元素是 Vertex 对象 adjList; + /* 构造方法 */ constructor(edges) { this.adjList = new Map(); @@ -54,7 +57,7 @@ class GraphAdjList { /* 添加顶点 */ addVertex(vet) { if (this.adjList.has(vet)) return; - // 在邻接表中添加一个新链表(即 HashSet) + // 在邻接表中添加一个新链表 this.adjList.set(vet, new Set()); } @@ -63,9 +66,9 @@ class GraphAdjList { if (!this.adjList.has(vet)) { throw new Error("Illegal Argument Exception"); } - // 在邻接表中删除顶点 vet 对应的链表(即 HashSet) + // 在邻接表中删除顶点 vet 对应的链表 this.adjList.delete(vet); - // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边 + // 遍历其它顶点的链表,删除所有包含 vet 的边 for (let set of this.adjList.values()) { set.delete(vet); } diff --git a/codes/javascript/chapter_tree/binary_tree_bfs.js b/codes/javascript/chapter_tree/binary_tree_bfs.js index 008ace0ad..c400853ea 100644 --- a/codes/javascript/chapter_tree/binary_tree_bfs.js +++ b/codes/javascript/chapter_tree/binary_tree_bfs.js @@ -8,7 +8,7 @@ const { arrToTree } = require("../include/TreeNode"); const { printTree } = require("../include/PrintUtil"); /* 层序遍历 */ -function hierOrder(root) { +function levelOrder(root) { // 初始化队列,加入根结点 let queue = [root]; // 初始化一个列表,用于保存遍历序列 @@ -33,5 +33,5 @@ console.log("\n初始化二叉树\n"); printTree(root); /* 层序遍历 */ -let list = hierOrder(root); +let list = levelOrder(root); console.log("\n层序遍历的结点打印序列 = " + list); diff --git a/codes/python/chapter_tree/binary_tree_bfs.py b/codes/python/chapter_tree/binary_tree_bfs.py index 657fd51da..dff32bd82 100644 --- a/codes/python/chapter_tree/binary_tree_bfs.py +++ b/codes/python/chapter_tree/binary_tree_bfs.py @@ -10,7 +10,7 @@ from include import * """ 层序遍历 """ -def hier_order(root: Optional[TreeNode]): +def level_order(root: Optional[TreeNode]): # 初始化队列,加入根结点 queue = collections.deque() queue.append(root) @@ -35,6 +35,6 @@ if __name__ == "__main__": print_tree(root) # 层序遍历 - res = hier_order(root) + res = level_order(root) print("\n层序遍历的结点打印序列 = ", res) assert res == [1, 2, 3, 4, 5, 6, 7] diff --git a/codes/swift/chapter_graph/graph_adjacency_list.swift b/codes/swift/chapter_graph/graph_adjacency_list.swift index 7185c542d..c5cafeb7e 100644 --- a/codes/swift/chapter_graph/graph_adjacency_list.swift +++ b/codes/swift/chapter_graph/graph_adjacency_list.swift @@ -23,9 +23,11 @@ class Vertex: Hashable { /* 基于邻接表实现的无向图类 */ class GraphAdjList { - // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象 - private var adjList: [Vertex: Set] // 邻接表(使用哈希表实现) + // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率 + // 请注意,adjList 中的元素是 Vertex 对象 + private var adjList: [Vertex: Set] + /* 构造方法 */ init(edges: [[Vertex]]) { adjList = [:] // 添加所有顶点和边 @@ -66,7 +68,7 @@ class GraphAdjList { if adjList[vet] != nil { return } - // 在邻接表中添加一个新链表(即 HashSet) + // 在邻接表中添加一个新链表 adjList[vet] = [] } @@ -75,9 +77,9 @@ class GraphAdjList { if adjList[vet] == nil { fatalError("参数错误") } - // 在邻接表中删除顶点 vet 对应的链表(即 HashSet) + // 在邻接表中删除顶点 vet 对应的链表 adjList.removeValue(forKey: vet) - // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边 + // 遍历其它顶点的链表,删除所有包含 vet 的边 for key in adjList.keys { adjList[key]?.remove(vet) } diff --git a/codes/swift/chapter_tree/binary_tree_bfs.swift b/codes/swift/chapter_tree/binary_tree_bfs.swift index a0aa3cd00..326214a92 100644 --- a/codes/swift/chapter_tree/binary_tree_bfs.swift +++ b/codes/swift/chapter_tree/binary_tree_bfs.swift @@ -7,7 +7,7 @@ import utils /* 层序遍历 */ -func hierOrder(root: TreeNode) -> [Int] { +func levelOrder(root: TreeNode) -> [Int] { // 初始化队列,加入根结点 var queue: [TreeNode] = [root] // 初始化一个列表,用于保存遍历序列 @@ -36,7 +36,7 @@ enum BinaryTreeBFS { PrintUtil.printTree(root: node) /* 层序遍历 */ - let list = hierOrder(root: node) + let list = levelOrder(root: node) print("\n层序遍历的结点打印序列 = \(list)") } } diff --git a/codes/typescript/chapter_graph/graph_adjacency_list.ts b/codes/typescript/chapter_graph/graph_adjacency_list.ts index 66fc8de9c..fd1e6fecc 100644 --- a/codes/typescript/chapter_graph/graph_adjacency_list.ts +++ b/codes/typescript/chapter_graph/graph_adjacency_list.ts @@ -14,7 +14,10 @@ class Vertex { /* 基于邻接表实现的无向图类 */ class GraphAdjList { + // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率 + // 请注意,adjList 中的元素是 Vertex 对象 adjList: Map>; + /* 构造方法 */ constructor(edges: Vertex[][]) { this.adjList = new Map(); @@ -54,7 +57,7 @@ class GraphAdjList { /* 添加顶点 */ addVertex(vet: Vertex): void { if (this.adjList.has(vet)) return; - // 在邻接表中添加一个新链表(即 HashSet) + // 在邻接表中添加一个新链表 this.adjList.set(vet, new Set()); } @@ -63,9 +66,9 @@ class GraphAdjList { if (!this.adjList.has(vet)) { throw new Error("Illegal Argument Exception"); } - // 在邻接表中删除顶点 vet 对应的链表(即 HashSet) + // 在邻接表中删除顶点 vet 对应的链表 this.adjList.delete(vet); - // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边 + // 遍历其它顶点的链表,删除所有包含 vet 的边 for (let set of this.adjList.values()) { set.delete(vet); } diff --git a/codes/typescript/chapter_tree/binary_tree_bfs.ts b/codes/typescript/chapter_tree/binary_tree_bfs.ts index f723e0486..83fba5c8b 100644 --- a/codes/typescript/chapter_tree/binary_tree_bfs.ts +++ b/codes/typescript/chapter_tree/binary_tree_bfs.ts @@ -9,7 +9,7 @@ import { arrToTree } from '../module/TreeNode'; import { printTree } from '../module/PrintUtil'; /* 层序遍历 */ -function hierOrder(root: TreeNode | null): number[] { +function levelOrder(root: TreeNode | null): number[] { // 初始化队列,加入根结点 const queue = [root]; // 初始化一个列表,用于保存遍历序列 @@ -35,7 +35,7 @@ console.log('\n初始化二叉树\n'); printTree(root); /* 层序遍历 */ -const list = hierOrder(root); +const list = levelOrder(root); console.log('\n层序遍历的结点打印序列 = ' + list); export {}; diff --git a/codes/zig/chapter_tree/binary_tree_bfs.zig b/codes/zig/chapter_tree/binary_tree_bfs.zig index eaa0d0d5a..dc0cb6720 100644 --- a/codes/zig/chapter_tree/binary_tree_bfs.zig +++ b/codes/zig/chapter_tree/binary_tree_bfs.zig @@ -6,7 +6,7 @@ const std = @import("std"); const inc = @import("include"); // 层序遍历 -fn hierOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) { +fn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) { // 初始化队列,加入根结点 const L = std.TailQueue(*inc.TreeNode(T)); var queue = L{}; @@ -48,7 +48,7 @@ pub fn main() !void { try inc.PrintUtil.printTree(root, null, false); // 层序遍历 - var list = try hierOrder(i32, mem_allocator, root.?); + var list = try levelOrder(i32, mem_allocator, root.?); defer list.deinit(); std.debug.print("\n层序遍历的结点打印序列 = ", .{}); inc.PrintUtil.printList(i32, list);