From 300016393bcc2ed5fc259ac79559049446677667 Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 10 Feb 2023 01:04:26 +0800 Subject: [PATCH] =?UTF-8?q?Add=20code=20source=20blocks=20to=20the=20chapt?= =?UTF-8?q?er=20Graph.=20Fix=20"=E5=87=BD=E6=95=B0"=20and=20"=E6=96=B9?= =?UTF-8?q?=E6=B3=95"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../c/chapter_array_and_linkedlist/my_list.c | 4 +- codes/c/chapter_heap/my_heap.c | 2 +- codes/c/chapter_stack_and_queue/array_queue.c | 4 +- .../linkedlist_stack.c | 8 +- codes/c/chapter_tree/avl_tree.c | 4 +- .../chapter_array_and_linkedlist/my_list.cpp | 4 +- codes/cpp/chapter_heap/my_heap.cpp | 2 +- codes/cpp/chapter_tree/avl_tree.cpp | 8 +- .../chapter_array_and_linkedlist/my_list.cs | 2 +- codes/csharp/chapter_tree/avl_tree.cs | 4 +- .../chapter_array_and_linkedlist/my_list.dart | 2 +- .../chapter_array_and_linkedlist/my_list.go | 2 +- .../go/chapter_graph/graph_adjacency_list.go | 3 +- .../chapter_graph/graph_adjacency_matrix.go | 1 + codes/go/chapter_heap/my_heap.go | 4 +- codes/go/chapter_tree/avl_tree.go | 4 +- .../chapter_array_and_linkedlist/my_list.java | 2 +- .../chapter_graph/graph_adjacency_list.java | 2 +- .../chapter_graph/graph_adjacency_matrix.java | 2 +- codes/java/chapter_heap/my_heap.java | 2 +- codes/java/chapter_tree/avl_tree.java | 4 +- .../chapter_array_and_linkedlist/my_list.js | 2 +- .../chapter_graph/graph_adjacency_list.js | 2 +- codes/javascript/chapter_heap/my_heap.js | 2 +- codes/javascript/chapter_tree/avl_tree.js | 6 +- .../chapter_array_and_linkedlist/my_list.py | 2 +- codes/python/chapter_tree/avl_tree.py | 4 +- .../my_list.swift | 2 +- .../graph_adjacency_matrix.swift | 2 +- codes/swift/chapter_heap/my_heap.swift | 2 +- codes/swift/chapter_tree/avl_tree.swift | 4 +- .../chapter_array_and_linkedlist/my_list.ts | 2 +- .../chapter_graph/graph_adjacency_list.ts | 2 +- codes/typescript/chapter_heap/my_heap.ts | 2 +- codes/typescript/chapter_tree/avl_tree.ts | 6 +- .../chapter_array_and_linkedlist/my_list.zig | 4 +- codes/zig/chapter_hashing/array_hash_map.zig | 4 +- codes/zig/chapter_heap/my_heap.zig | 4 +- .../chapter_stack_and_queue/array_queue.zig | 4 +- .../chapter_stack_and_queue/array_stack.zig | 4 +- .../linkedlist_deque.zig | 4 +- .../linkedlist_queue.zig | 4 +- .../linkedlist_stack.zig | 4 +- codes/zig/chapter_tree/avl_tree.zig | 8 +- codes/zig/chapter_tree/binary_search_tree.zig | 4 +- docs/chapter_graph/graph_operations.md | 355 ++---------------- docs/utils/extract_code_go.py | 10 +- 47 files changed, 108 insertions(+), 411 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index 40ab3345e..72a942291 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -19,7 +19,7 @@ typedef struct myList myList; /* 前置声明 */ void extendCapacity(myList *list); -/* 构造函数 */ +/* 构造方法 */ myList *newMyList() { myList *list = malloc(sizeof(myList)); list->capacity = 10; @@ -29,7 +29,7 @@ myList *newMyList() { return list; } -/* 析构函数 */ +/* 析构方法 */ void delMyList(myList *list) { free(list->nums); free(list); diff --git a/codes/c/chapter_heap/my_heap.c b/codes/c/chapter_heap/my_heap.c index 3f5aa1ee2..1c0766e6f 100644 --- a/codes/c/chapter_heap/my_heap.c +++ b/codes/c/chapter_heap/my_heap.c @@ -20,7 +20,7 @@ void siftDown(maxHeap *h, int i); void siftUp(maxHeap *h, int i); -/* 构造函数,根据切片建堆 */ +/* 构造方法,根据切片建堆 */ maxHeap *newMaxHeap(int nums[], int size) { // 所有元素入堆 maxHeap *h = (maxHeap *) malloc(sizeof(maxHeap)); diff --git a/codes/c/chapter_stack_and_queue/array_queue.c b/codes/c/chapter_stack_and_queue/array_queue.c index f0edbe6af..6a67c6393 100644 --- a/codes/c/chapter_stack_and_queue/array_queue.c +++ b/codes/c/chapter_stack_and_queue/array_queue.c @@ -16,7 +16,7 @@ struct ArrayQueue { typedef struct ArrayQueue ArrayQueue; -/* 构造函数 */ +/* 构造方法 */ ArrayQueue *newArrayQueue(int capacity) { ArrayQueue *queue = (ArrayQueue *) malloc(sizeof(ArrayQueue)); // 初始化数组 @@ -26,7 +26,7 @@ ArrayQueue *newArrayQueue(int capacity) { return queue; } -/* 析构函数 */ +/* 析构方法 */ void delArrayQueue(ArrayQueue *queue) { free(queue->nums); queue->queCapacity = 0; diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index 7463ea73c..71d86f8e6 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -14,7 +14,7 @@ struct linkedListStack { typedef struct linkedListStack linkedListStack; -/* 构造函数 */ +/* 构造方法 */ linkedListStack *newLinkedListStack() { linkedListStack *s = malloc(sizeof(linkedListStack)); s->top = NULL; @@ -22,7 +22,7 @@ linkedListStack *newLinkedListStack() { return s; } -/* 析构函数 */ +/* 析构方法 */ void delLinkedListStack(linkedListStack *s) { while (s->top) { ListNode *n = s->top->next; @@ -80,7 +80,7 @@ int pop(linkedListStack *s) { /* Driver Code */ int main() { /* 初始化栈 */ - // 构造函数 + // 构造方法 linkedListStack *stack = newLinkedListStack(); /* 元素入栈 */ @@ -109,7 +109,7 @@ int main() { bool empty = isEmpty(stack); printf("栈是否为空 = %s\n", empty ? "true" : "false"); - /* 析构函数 */ + /* 析构方法 */ delLinkedListStack(stack); return 0; } diff --git a/codes/c/chapter_tree/avl_tree.c b/codes/c/chapter_tree/avl_tree.c index 9bce9c9b5..6b64deb0c 100644 --- a/codes/c/chapter_tree/avl_tree.c +++ b/codes/c/chapter_tree/avl_tree.c @@ -110,7 +110,7 @@ TreeNode *rotate(TreeNode *node) { return node; } -/* 递归插入结点(辅助函数) */ +/* 递归插入结点(辅助方法) */ TreeNode *insertHelper(TreeNode *node, int val) { if (node == NULL) { return newTreeNode(val); @@ -151,7 +151,7 @@ TreeNode *getInOrderNext(TreeNode *node) { return node; } -/* 递归删除结点(辅助函数) */ +/* 递归删除结点(辅助方法) */ TreeNode *removeHelper(TreeNode *node, int val) { TreeNode *child, *grandChild, *temp; if (node == NULL) { diff --git a/codes/cpp/chapter_array_and_linkedlist/my_list.cpp b/codes/cpp/chapter_array_and_linkedlist/my_list.cpp index 8085cec85..57c9dcd86 100644 --- a/codes/cpp/chapter_array_and_linkedlist/my_list.cpp +++ b/codes/cpp/chapter_array_and_linkedlist/my_list.cpp @@ -15,12 +15,12 @@ private: int extendRatio = 2; // 每次列表扩容的倍数 public: - /* 构造函数 */ + /* 构造方法 */ MyList() { nums = new int[numsCapacity]; } - /* 析构函数 */ + /* 析构方法 */ ~MyList() { delete[] nums; } diff --git a/codes/cpp/chapter_heap/my_heap.cpp b/codes/cpp/chapter_heap/my_heap.cpp index faf2f5e88..54d58f67f 100644 --- a/codes/cpp/chapter_heap/my_heap.cpp +++ b/codes/cpp/chapter_heap/my_heap.cpp @@ -62,7 +62,7 @@ private: } public: - /* 构造函数,根据输入列表建堆 */ + /* 构造方法,根据输入列表建堆 */ MaxHeap(vector nums) { // 将列表元素原封不动添加进堆 maxHeap = nums; diff --git a/codes/cpp/chapter_tree/avl_tree.cpp b/codes/cpp/chapter_tree/avl_tree.cpp index a6b523143..210c4ce25 100644 --- a/codes/cpp/chapter_tree/avl_tree.cpp +++ b/codes/cpp/chapter_tree/avl_tree.cpp @@ -75,7 +75,7 @@ private: return node; } - /* 递归插入结点(辅助函数) */ + /* 递归插入结点(辅助方法) */ TreeNode* insertHelper(TreeNode* node, int val) { if (node == nullptr) return new TreeNode(val); /* 1. 查找插入位置,并插入结点 */ @@ -102,7 +102,7 @@ private: return node; } - /* 递归删除结点(辅助函数) */ + /* 递归删除结点(辅助方法) */ TreeNode* removeHelper(TreeNode* node, int val) { if (node == nullptr) return nullptr; /* 1. 查找结点,并删除之 */ @@ -183,10 +183,10 @@ public: return cur; } - /*构造函数*/ + /*构造方法*/ AVLTree() : root(nullptr) {} - /*析构函数*/ + /*析构方法*/ ~AVLTree() { freeMemoryTree(root); } diff --git a/codes/csharp/chapter_array_and_linkedlist/my_list.cs b/codes/csharp/chapter_array_and_linkedlist/my_list.cs index 999e98ee2..e415e08d0 100644 --- a/codes/csharp/chapter_array_and_linkedlist/my_list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/my_list.cs @@ -16,7 +16,7 @@ class MyList private int numsSize = 0; // 列表长度(即当前元素数量) private int extendRatio = 2; // 每次列表扩容的倍数 - /* 构造函数 */ + /* 构造方法 */ public MyList() { nums = new int[numsCapacity]; diff --git a/codes/csharp/chapter_tree/avl_tree.cs b/codes/csharp/chapter_tree/avl_tree.cs index ffe1f690b..776268b60 100644 --- a/codes/csharp/chapter_tree/avl_tree.cs +++ b/codes/csharp/chapter_tree/avl_tree.cs @@ -113,7 +113,7 @@ class AVLTree return root; } - /* 递归插入结点(辅助函数) */ + /* 递归插入结点(辅助方法) */ private TreeNode? insertHelper(TreeNode? node, int val) { if (node == null) return new TreeNode(val); @@ -138,7 +138,7 @@ class AVLTree return root; } - /* 递归删除结点(辅助函数) */ + /* 递归删除结点(辅助方法) */ private TreeNode? removeHelper(TreeNode? node, int val) { if (node == null) return null; diff --git a/codes/dart/chapter_array_and_linkedlist/my_list.dart b/codes/dart/chapter_array_and_linkedlist/my_list.dart index 7f4334406..13b38f08c 100644 --- a/codes/dart/chapter_array_and_linkedlist/my_list.dart +++ b/codes/dart/chapter_array_and_linkedlist/my_list.dart @@ -11,7 +11,7 @@ class MyList { int _size = 0; // 列表长度(即当前元素数量) int _extendRatio = 2; // 每次列表扩容的倍数 - /* 构造函数 */ + /* 构造方法 */ MyList() { _nums = List.filled(_capacity, 0); } diff --git a/codes/go/chapter_array_and_linkedlist/my_list.go b/codes/go/chapter_array_and_linkedlist/my_list.go index cb2c7c95b..46f52dc32 100644 --- a/codes/go/chapter_array_and_linkedlist/my_list.go +++ b/codes/go/chapter_array_and_linkedlist/my_list.go @@ -12,7 +12,7 @@ type myList struct { extendRatio int } -/* 构造函数 */ +/* 构造方法 */ func newMyList() *myList { return &myList{ numsCapacity: 10, // 列表容量 diff --git a/codes/go/chapter_graph/graph_adjacency_list.go b/codes/go/chapter_graph/graph_adjacency_list.go index 762ad1140..887384dd8 100644 --- a/codes/go/chapter_graph/graph_adjacency_list.go +++ b/codes/go/chapter_graph/graph_adjacency_list.go @@ -15,6 +15,7 @@ type vertex struct { val int } +/* 构造方法 */ func newVertex(val int) vertex { return vertex{ val: val, @@ -28,7 +29,7 @@ type graphAdjList struct { adjList map[vertex]map[vertex]struct{} } -/* 构造函数 */ +/* 构造方法 */ func newGraphAdjList(edges [][]vertex) *graphAdjList { g := &graphAdjList{ adjList: make(map[vertex]map[vertex]struct{}), diff --git a/codes/go/chapter_graph/graph_adjacency_matrix.go b/codes/go/chapter_graph/graph_adjacency_matrix.go index a182b469f..c705253b5 100644 --- a/codes/go/chapter_graph/graph_adjacency_matrix.go +++ b/codes/go/chapter_graph/graph_adjacency_matrix.go @@ -14,6 +14,7 @@ type graphAdjMat struct { adjMat [][]int } +/* 构造方法 */ func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat { // 添加顶点 n := len(vertices) diff --git a/codes/go/chapter_heap/my_heap.go b/codes/go/chapter_heap/my_heap.go index ab870eec0..e621f6187 100644 --- a/codes/go/chapter_heap/my_heap.go +++ b/codes/go/chapter_heap/my_heap.go @@ -15,14 +15,14 @@ type maxHeap struct { data []any } -/* 构造函数,建立空堆 */ +/* 构造方法,建立空堆 */ func newHeap() *maxHeap { return &maxHeap{ data: make([]any, 0), } } -/* 构造函数,根据切片建堆 */ +/* 构造方法,根据切片建堆 */ func newMaxHeap(nums []any) *maxHeap { // 将列表元素原封不动添加进堆 h := &maxHeap{data: nums} diff --git a/codes/go/chapter_tree/avl_tree.go b/codes/go/chapter_tree/avl_tree.go index 46356c4e3..117a7fb58 100644 --- a/codes/go/chapter_tree/avl_tree.go +++ b/codes/go/chapter_tree/avl_tree.go @@ -112,7 +112,7 @@ func (t *aVLTree) insert(val int) *TreeNode { return t.root } -/* 递归插入结点(辅助函数) */ +/* 递归插入结点(辅助方法) */ func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode { if node == nil { return NewTreeNode(val) @@ -140,7 +140,7 @@ func (t *aVLTree) remove(val int) *TreeNode { return root } -/* 递归删除结点(辅助函数) */ +/* 递归删除结点(辅助方法) */ func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode { if node == nil { return nil diff --git a/codes/java/chapter_array_and_linkedlist/my_list.java b/codes/java/chapter_array_and_linkedlist/my_list.java index 06a403132..15fe63c37 100644 --- a/codes/java/chapter_array_and_linkedlist/my_list.java +++ b/codes/java/chapter_array_and_linkedlist/my_list.java @@ -15,7 +15,7 @@ class MyList { private int size = 0; // 列表长度(即当前元素数量) private int extendRatio = 2; // 每次列表扩容的倍数 - /* 构造函数 */ + /* 构造方法 */ public MyList() { nums = new int[capacity]; } diff --git a/codes/java/chapter_graph/graph_adjacency_list.java b/codes/java/chapter_graph/graph_adjacency_list.java index 3e55b5468..a1ca10173 100644 --- a/codes/java/chapter_graph/graph_adjacency_list.java +++ b/codes/java/chapter_graph/graph_adjacency_list.java @@ -21,7 +21,7 @@ class GraphAdjList { // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象 Map> adjList; // 邻接表(使用哈希表实现) - /* 构造函数 */ + /* 构造方法 */ public GraphAdjList(Vertex[][] edges) { this.adjList = new HashMap<>(); // 添加所有顶点和边 diff --git a/codes/java/chapter_graph/graph_adjacency_matrix.java b/codes/java/chapter_graph/graph_adjacency_matrix.java index 7796c1ac6..5c7edb15c 100644 --- a/codes/java/chapter_graph/graph_adjacency_matrix.java +++ b/codes/java/chapter_graph/graph_adjacency_matrix.java @@ -14,7 +14,7 @@ class GraphAdjMat { List vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” List> adjMat; // 邻接矩阵,行列索引对应“顶点索引” - /* 构造函数 */ + /* 构造方法 */ public GraphAdjMat(int[] vertices, int[][] edges) { this.vertices = new ArrayList<>(); this.adjMat = new ArrayList<>(); diff --git a/codes/java/chapter_heap/my_heap.java b/codes/java/chapter_heap/my_heap.java index 4a39149b7..6b862f714 100644 --- a/codes/java/chapter_heap/my_heap.java +++ b/codes/java/chapter_heap/my_heap.java @@ -14,7 +14,7 @@ class MaxHeap { // 使用列表而非数组,这样无需考虑扩容问题 private List maxHeap; - /* 构造函数,根据输入列表建堆 */ + /* 构造方法,根据输入列表建堆 */ public MaxHeap(List nums) { // 将列表元素原封不动添加进堆 maxHeap = new ArrayList<>(nums); diff --git a/codes/java/chapter_tree/avl_tree.java b/codes/java/chapter_tree/avl_tree.java index 945719dd5..c982b7a35 100644 --- a/codes/java/chapter_tree/avl_tree.java +++ b/codes/java/chapter_tree/avl_tree.java @@ -96,7 +96,7 @@ class AVLTree { return root; } - /* 递归插入结点(辅助函数) */ + /* 递归插入结点(辅助方法) */ private TreeNode insertHelper(TreeNode node, int val) { if (node == null) return new TreeNode(val); /* 1. 查找插入位置,并插入结点 */ @@ -119,7 +119,7 @@ class AVLTree { return root; } - /* 递归删除结点(辅助函数) */ + /* 递归删除结点(辅助方法) */ private TreeNode removeHelper(TreeNode node, int val) { if (node == null) return null; /* 1. 查找结点,并删除之 */ diff --git a/codes/javascript/chapter_array_and_linkedlist/my_list.js b/codes/javascript/chapter_array_and_linkedlist/my_list.js index 141d11d07..71aa5cb4d 100644 --- a/codes/javascript/chapter_array_and_linkedlist/my_list.js +++ b/codes/javascript/chapter_array_and_linkedlist/my_list.js @@ -11,7 +11,7 @@ class MyList { #size = 0; // 列表长度(即当前元素数量) #extendRatio = 2; // 每次列表扩容的倍数 - /* 构造函数 */ + /* 构造方法 */ constructor() { this.#nums = new Array(this.#capacity); } diff --git a/codes/javascript/chapter_graph/graph_adjacency_list.js b/codes/javascript/chapter_graph/graph_adjacency_list.js index ed42819f2..586e4a06b 100644 --- a/codes/javascript/chapter_graph/graph_adjacency_list.js +++ b/codes/javascript/chapter_graph/graph_adjacency_list.js @@ -15,7 +15,7 @@ class Vertex { /* 基于邻接表实现的无向图类 */ class GraphAdjList { adjList; - /* 构造函数 */ + /* 构造方法 */ constructor(edges) { this.adjList = new Map(); // 添加所有顶点和边 diff --git a/codes/javascript/chapter_heap/my_heap.js b/codes/javascript/chapter_heap/my_heap.js index 3ec8a6231..73a10c6e5 100644 --- a/codes/javascript/chapter_heap/my_heap.js +++ b/codes/javascript/chapter_heap/my_heap.js @@ -10,7 +10,7 @@ const { printHeap } = require("../include/PrintUtil"); class MaxHeap { #maxHeap; - /* 构造函数,建立空堆或根据输入列表建堆 */ + /* 构造方法,建立空堆或根据输入列表建堆 */ constructor(nums) { // 将列表元素原封不动添加进堆 this.#maxHeap = nums === undefined ? [] : [...nums]; diff --git a/codes/javascript/chapter_tree/avl_tree.js b/codes/javascript/chapter_tree/avl_tree.js index c949d5e45..1d237318e 100644 --- a/codes/javascript/chapter_tree/avl_tree.js +++ b/codes/javascript/chapter_tree/avl_tree.js @@ -9,7 +9,7 @@ const { printTree } = require("../include/PrintUtil"); /* AVL 树*/ class AVLTree { - /*构造函数*/ + /*构造方法*/ constructor() { this.root = null; //根结点 } @@ -98,7 +98,7 @@ class AVLTree { return this.root; } - /* 递归插入结点(辅助函数) */ + /* 递归插入结点(辅助方法) */ insertHelper(node, val) { if (node === null) return new TreeNode(val); /* 1. 查找插入位置,并插入结点 */ @@ -118,7 +118,7 @@ class AVLTree { return this.root; } - /* 递归删除结点(辅助函数) */ + /* 递归删除结点(辅助方法) */ removeHelper(node, val) { if (node === null) return null; /* 1. 查找结点,并删除之 */ diff --git a/codes/python/chapter_array_and_linkedlist/my_list.py b/codes/python/chapter_array_and_linkedlist/my_list.py index a5a0eabd0..f6a2b394e 100644 --- a/codes/python/chapter_array_and_linkedlist/my_list.py +++ b/codes/python/chapter_array_and_linkedlist/my_list.py @@ -10,7 +10,7 @@ from include import * """ 列表类简易实现 """ class MyList: - """ 构造函数 """ + """ 构造方法 """ def __init__(self): self.__capacity = 10 # 列表容量 self.__nums = [0] * self.__capacity # 数组(存储列表元素) diff --git a/codes/python/chapter_tree/avl_tree.py b/codes/python/chapter_tree/avl_tree.py index e4858c362..a3b7e22a6 100644 --- a/codes/python/chapter_tree/avl_tree.py +++ b/codes/python/chapter_tree/avl_tree.py @@ -89,7 +89,7 @@ class AVLTree: self.root = self.__insert_helper(self.root, val) return self.root - """ 递归插入结点(辅助函数)""" + """ 递归插入结点(辅助方法)""" def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode: if node is None: return TreeNode(val) @@ -111,7 +111,7 @@ class AVLTree: root = self.__remove_helper(self.root, val) return root - """ 递归删除结点(辅助函数) """ + """ 递归删除结点(辅助方法) """ def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]: if node is None: return None diff --git a/codes/swift/chapter_array_and_linkedlist/my_list.swift b/codes/swift/chapter_array_and_linkedlist/my_list.swift index b53dc422b..6081237a8 100644 --- a/codes/swift/chapter_array_and_linkedlist/my_list.swift +++ b/codes/swift/chapter_array_and_linkedlist/my_list.swift @@ -11,7 +11,7 @@ class MyList { private var _size = 0 // 列表长度(即当前元素数量) private let extendRatio = 2 // 每次列表扩容的倍数 - /* 构造函数 */ + /* 构造方法 */ init() { nums = Array(repeating: 0, count: _capacity) } diff --git a/codes/swift/chapter_graph/graph_adjacency_matrix.swift b/codes/swift/chapter_graph/graph_adjacency_matrix.swift index 46eac587c..51d7ed94c 100644 --- a/codes/swift/chapter_graph/graph_adjacency_matrix.swift +++ b/codes/swift/chapter_graph/graph_adjacency_matrix.swift @@ -11,7 +11,7 @@ class GraphAdjMat { private var vertices: [Int] // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” private var adjMat: [[Int]] // 邻接矩阵,行列索引对应“顶点索引” - /* 构造函数 */ + /* 构造方法 */ init(vertices: [Int], edges: [[Int]]) { self.vertices = [] adjMat = [] diff --git a/codes/swift/chapter_heap/my_heap.swift b/codes/swift/chapter_heap/my_heap.swift index 5b153cdc7..b85d57f27 100644 --- a/codes/swift/chapter_heap/my_heap.swift +++ b/codes/swift/chapter_heap/my_heap.swift @@ -10,7 +10,7 @@ import utils class MaxHeap { private var maxHeap: [Int] - /* 构造函数,根据输入列表建堆 */ + /* 构造方法,根据输入列表建堆 */ init(nums: [Int]) { // 将列表元素原封不动添加进堆 maxHeap = nums diff --git a/codes/swift/chapter_tree/avl_tree.swift b/codes/swift/chapter_tree/avl_tree.swift index c0a8092c6..d7d084c4b 100644 --- a/codes/swift/chapter_tree/avl_tree.swift +++ b/codes/swift/chapter_tree/avl_tree.swift @@ -95,7 +95,7 @@ class AVLTree { return root } - /* 递归插入结点(辅助函数) */ + /* 递归插入结点(辅助方法) */ private func insertHelper(node: TreeNode?, val: Int) -> TreeNode? { var node = node if node == nil { @@ -123,7 +123,7 @@ class AVLTree { return root } - /* 递归删除结点(辅助函数) */ + /* 递归删除结点(辅助方法) */ private func removeHelper(node: TreeNode?, val: Int) -> TreeNode? { var node = node if node == nil { diff --git a/codes/typescript/chapter_array_and_linkedlist/my_list.ts b/codes/typescript/chapter_array_and_linkedlist/my_list.ts index 0b7c0865f..fb06031bd 100644 --- a/codes/typescript/chapter_array_and_linkedlist/my_list.ts +++ b/codes/typescript/chapter_array_and_linkedlist/my_list.ts @@ -11,7 +11,7 @@ class MyList { private _size: number = 0; // 列表长度(即当前元素数量) private extendRatio: number = 2; // 每次列表扩容的倍数 - /* 构造函数 */ + /* 构造方法 */ constructor() { this.nums = new Array(this._capacity); } diff --git a/codes/typescript/chapter_graph/graph_adjacency_list.ts b/codes/typescript/chapter_graph/graph_adjacency_list.ts index 6ce41f8f8..66fc8de9c 100644 --- a/codes/typescript/chapter_graph/graph_adjacency_list.ts +++ b/codes/typescript/chapter_graph/graph_adjacency_list.ts @@ -15,7 +15,7 @@ class Vertex { /* 基于邻接表实现的无向图类 */ class GraphAdjList { adjList: Map>; - /* 构造函数 */ + /* 构造方法 */ constructor(edges: Vertex[][]) { this.adjList = new Map(); // 添加所有顶点和边 diff --git a/codes/typescript/chapter_heap/my_heap.ts b/codes/typescript/chapter_heap/my_heap.ts index 03c388c29..c13b1323b 100644 --- a/codes/typescript/chapter_heap/my_heap.ts +++ b/codes/typescript/chapter_heap/my_heap.ts @@ -9,7 +9,7 @@ import { printHeap } from "../module/PrintUtil"; /* 最大堆类 */ class MaxHeap { private maxHeap: number[]; - /* 构造函数,建立空堆或根据输入列表建堆 */ + /* 构造方法,建立空堆或根据输入列表建堆 */ constructor(nums?: number[]) { // 将列表元素原封不动添加进堆 this.maxHeap = nums === undefined ? [] : [...nums]; diff --git a/codes/typescript/chapter_tree/avl_tree.ts b/codes/typescript/chapter_tree/avl_tree.ts index bc052aa1a..5c654fa53 100644 --- a/codes/typescript/chapter_tree/avl_tree.ts +++ b/codes/typescript/chapter_tree/avl_tree.ts @@ -10,7 +10,7 @@ import { printTree } from "../module/PrintUtil"; /* AVL 树*/ class AVLTree { root: TreeNode; - /*构造函数*/ + /*构造方法*/ constructor() { this.root = null; //根结点 } @@ -99,7 +99,7 @@ class AVLTree { return this.root; } - /* 递归插入结点(辅助函数) */ + /* 递归插入结点(辅助方法) */ insertHelper(node: TreeNode, val: number): TreeNode { if (node === null) return new TreeNode(val); /* 1. 查找插入位置,并插入结点 */ @@ -123,7 +123,7 @@ class AVLTree { return this.root; } - /* 递归删除结点(辅助函数) */ + /* 递归删除结点(辅助方法) */ removeHelper(node: TreeNode, val: number): TreeNode { if (node === null) return null; /* 1. 查找结点,并删除之 */ diff --git a/codes/zig/chapter_array_and_linkedlist/my_list.zig b/codes/zig/chapter_array_and_linkedlist/my_list.zig index 4b2fa5388..75cef2aca 100644 --- a/codes/zig/chapter_array_and_linkedlist/my_list.zig +++ b/codes/zig/chapter_array_and_linkedlist/my_list.zig @@ -17,7 +17,7 @@ pub fn MyList(comptime T: type) type { mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 - // 构造函数(分配内存+初始化列表) + // 构造方法(分配内存+初始化列表) pub fn init(self: *Self, allocator: std.mem.Allocator) !void { if (self.mem_arena == null) { self.mem_arena = std.heap.ArenaAllocator.init(allocator); @@ -27,7 +27,7 @@ pub fn MyList(comptime T: type) type { std.mem.set(T, self.nums, @as(T, 0)); } - // 析构函数(释放内存) + // 析构方法(释放内存) pub fn deinit(self: *Self) void { if (self.mem_arena == null) return; self.mem_arena.?.deinit(); diff --git a/codes/zig/chapter_hashing/array_hash_map.zig b/codes/zig/chapter_hashing/array_hash_map.zig index 44cf02bd7..033502de3 100644 --- a/codes/zig/chapter_hashing/array_hash_map.zig +++ b/codes/zig/chapter_hashing/array_hash_map.zig @@ -26,7 +26,7 @@ pub fn ArrayHashMap(comptime T: type) type { const Self = @This(); - // 构造函数 + // 构造方法 pub fn init(self: *Self, allocator: std.mem.Allocator) !void { self.mem_allocator = allocator; // 初始化一个长度为 100 的桶(数组) @@ -37,7 +37,7 @@ pub fn ArrayHashMap(comptime T: type) type { } } - // 析构函数 + // 析构方法 pub fn deinit(self: *Self) void { if (self.bucket != null) self.bucket.?.deinit(); } diff --git a/codes/zig/chapter_heap/my_heap.zig b/codes/zig/chapter_heap/my_heap.zig index ff5c38477..060e6b1c0 100644 --- a/codes/zig/chapter_heap/my_heap.zig +++ b/codes/zig/chapter_heap/my_heap.zig @@ -12,7 +12,7 @@ pub fn MaxHeap(comptime T: type) type { maxHeap: ?std.ArrayList(T) = null, // 使用列表而非数组,这样无需考虑扩容问题 - // 构造函数,根据输入列表建堆 + // 构造方法,根据输入列表建堆 pub fn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void { if (self.maxHeap != null) return; self.maxHeap = std.ArrayList(T).init(allocator); @@ -25,7 +25,7 @@ pub fn MaxHeap(comptime T: type) type { } } - // 析构函数,释放内存 + // 析构方法,释放内存 pub fn deinit(self: *Self) void { if (self.maxHeap != null) self.maxHeap.?.deinit(); } diff --git a/codes/zig/chapter_stack_and_queue/array_queue.zig b/codes/zig/chapter_stack_and_queue/array_queue.zig index 79f7116a1..5b92bec67 100644 --- a/codes/zig/chapter_stack_and_queue/array_queue.zig +++ b/codes/zig/chapter_stack_and_queue/array_queue.zig @@ -17,7 +17,7 @@ pub fn ArrayQueue(comptime T: type) type { mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 - // 构造函数(分配内存+初始化数组) + // 构造方法(分配内存+初始化数组) pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void { if (self.mem_arena == null) { self.mem_arena = std.heap.ArenaAllocator.init(allocator); @@ -28,7 +28,7 @@ pub fn ArrayQueue(comptime T: type) type { std.mem.set(T, self.nums, @as(T, 0)); } - // 析构函数(释放内存) + // 析构方法(释放内存) pub fn deinit(self: *Self) void { if (self.mem_arena == null) return; self.mem_arena.?.deinit(); diff --git a/codes/zig/chapter_stack_and_queue/array_stack.zig b/codes/zig/chapter_stack_and_queue/array_stack.zig index 77f230a50..b42b48ed5 100644 --- a/codes/zig/chapter_stack_and_queue/array_stack.zig +++ b/codes/zig/chapter_stack_and_queue/array_stack.zig @@ -12,14 +12,14 @@ pub fn ArrayStack(comptime T: type) type { stack: ?std.ArrayList(T) = null, - // 构造函数(分配内存+初始化栈) + // 构造方法(分配内存+初始化栈) pub fn init(self: *Self, allocator: std.mem.Allocator) void { if (self.stack == null) { self.stack = std.ArrayList(T).init(allocator); } } - // 析构函数(释放内存) + // 析构方法(释放内存) pub fn deinit(self: *Self) void { if (self.stack == null) return; self.stack.?.deinit(); diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig b/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig index b86e0c793..18d08004d 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig @@ -34,7 +34,7 @@ pub fn LinkedListDeque(comptime T: type) type { mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 - // 构造函数(分配内存+初始化队列) + // 构造方法(分配内存+初始化队列) pub fn init(self: *Self, allocator: std.mem.Allocator) !void { if (self.mem_arena == null) { self.mem_arena = std.heap.ArenaAllocator.init(allocator); @@ -45,7 +45,7 @@ pub fn LinkedListDeque(comptime T: type) type { self.deqSize = 0; } - // 析构函数(释放内存) + // 析构方法(释放内存) pub fn deinit(self: *Self) void { if (self.mem_arena == null) return; self.mem_arena.?.deinit(); diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig b/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig index a5b66d0b3..db2622d0a 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig @@ -16,7 +16,7 @@ pub fn LinkedListQueue(comptime T: type) type { mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 - // 构造函数(分配内存+初始化队列) + // 构造方法(分配内存+初始化队列) pub fn init(self: *Self, allocator: std.mem.Allocator) !void { if (self.mem_arena == null) { self.mem_arena = std.heap.ArenaAllocator.init(allocator); @@ -27,7 +27,7 @@ pub fn LinkedListQueue(comptime T: type) type { self.queSize = 0; } - // 析构函数(释放内存) + // 析构方法(释放内存) pub fn deinit(self: *Self) void { if (self.mem_arena == null) return; self.mem_arena.?.deinit(); diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig b/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig index 8fd8f783f..55ce994a3 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig @@ -15,7 +15,7 @@ pub fn LinkedListStack(comptime T: type) type { mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 - // 构造函数(分配内存+初始化栈) + // 构造方法(分配内存+初始化栈) pub fn init(self: *Self, allocator: std.mem.Allocator) !void { if (self.mem_arena == null) { self.mem_arena = std.heap.ArenaAllocator.init(allocator); @@ -25,7 +25,7 @@ pub fn LinkedListStack(comptime T: type) type { self.stkSize = 0; } - // 析构函数(释放内存) + // 析构方法(释放内存) pub fn deinit(self: *Self) void { if (self.mem_arena == null) return; self.mem_arena.?.deinit(); diff --git a/codes/zig/chapter_tree/avl_tree.zig b/codes/zig/chapter_tree/avl_tree.zig index 5ff24b90a..dec13c181 100644 --- a/codes/zig/chapter_tree/avl_tree.zig +++ b/codes/zig/chapter_tree/avl_tree.zig @@ -14,7 +14,7 @@ pub fn AVLTree(comptime T: type) type { mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 - // 构造函数 + // 构造方法 pub fn init(self: *Self, allocator: std.mem.Allocator) void { if (self.mem_arena == null) { self.mem_arena = std.heap.ArenaAllocator.init(allocator); @@ -22,7 +22,7 @@ pub fn AVLTree(comptime T: type) type { } } - // 析构函数 + // 析构方法 pub fn deinit(self: *Self) void { if (self.mem_arena == null) return; self.mem_arena.?.deinit(); @@ -113,7 +113,7 @@ pub fn AVLTree(comptime T: type) type { return self.root; } - // 递归插入结点(辅助函数) + // 递归插入结点(辅助方法) fn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) { var node = node_; if (node == null) { @@ -142,7 +142,7 @@ pub fn AVLTree(comptime T: type) type { return self.root; } - // 递归删除结点(辅助函数) + // 递归删除结点(辅助方法) fn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) { var node = node_; if (node == null) return null; diff --git a/codes/zig/chapter_tree/binary_search_tree.zig b/codes/zig/chapter_tree/binary_search_tree.zig index e34feb33e..f4d08ddc6 100644 --- a/codes/zig/chapter_tree/binary_search_tree.zig +++ b/codes/zig/chapter_tree/binary_search_tree.zig @@ -14,7 +14,7 @@ pub fn BinarySearchTree(comptime T: type) type { mem_arena: ?std.heap.ArenaAllocator = null, mem_allocator: std.mem.Allocator = undefined, // 内存分配器 - // 构造函数 + // 构造方法 pub fn init(self: *Self, allocator: std.mem.Allocator, nums: []T) !void { if (self.mem_arena == null) { self.mem_arena = std.heap.ArenaAllocator.init(allocator); @@ -24,7 +24,7 @@ pub fn BinarySearchTree(comptime T: type) type { self.root = try self.buildTree(nums, 0, nums.len - 1); // 构建二叉搜索树 } - // 析构函数 + // 析构方法 pub fn deinit(self: *Self) void { if (self.mem_arena == null) return; self.mem_arena.?.deinit(); diff --git a/docs/chapter_graph/graph_operations.md b/docs/chapter_graph/graph_operations.md index e153ba57d..a9f0c5c02 100644 --- a/docs/chapter_graph/graph_operations.md +++ b/docs/chapter_graph/graph_operations.md @@ -41,120 +41,37 @@ comments: true === "C++" ```cpp title="graph_adjacency_matrix.cpp" - + [class]{GraphAdjMat}-[func]{} ``` === "Python" ```python title="graph_adjacency_matrix.py" - + [class]{GraphAdjMat}-[func]{} ``` === "Go" ```go title="graph_adjacency_matrix.go" - /* 基于邻接矩阵实现的无向图类 */ - type graphAdjMat struct { - // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” - vertices []int - // 邻接矩阵,行列索引对应“顶点索引” - adjMat [][]int - } - - func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat { - // 添加顶点 - n := len(vertices) - adjMat := make([][]int, n) - for i := range adjMat { - adjMat[i] = make([]int, n) - } - // 初始化图 - g := &graphAdjMat{ - vertices: vertices, - adjMat: adjMat, - } - // 添加边 - // 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 - for i := range edges { - g.addEdge(edges[i][0], edges[i][1]) - } - return g - } - - /* 获取顶点数量 */ - func (g *graphAdjMat) size() int { - return len(g.vertices) - } - - /* 添加顶点 */ - func (g *graphAdjMat) addVertex(val int) { - n := g.size() - // 向顶点列表中添加新顶点的值 - g.vertices = append(g.vertices, val) - // 在邻接矩阵中添加一行 - newRow := make([]int, n) - g.adjMat = append(g.adjMat, newRow) - // 在邻接矩阵中添加一列 - for i := range g.adjMat { - g.adjMat[i] = append(g.adjMat[i], 0) - } - } - - /* 删除顶点 */ - func (g *graphAdjMat) removeVertex(index int) { - if index >= g.size() { - return - } - // 在顶点列表中移除索引 index 的顶点 - g.vertices = append(g.vertices[:index], g.vertices[index+1:]...) - // 在邻接矩阵中删除索引 index 的行 - g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...) - // 在邻接矩阵中删除索引 index 的列 - for i := range g.adjMat { - g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...) - } - } - - /* 添加边 */ - // 参数 i, j 对应 vertices 元素索引 - func (g *graphAdjMat) addEdge(i, j int) { - // 索引越界与相等处理 - if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j { - fmt.Errorf("%s", "Index Out Of Bounds Exception") - } - // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i) - g.adjMat[i][j] = 1 - g.adjMat[j][i] = 1 - } - - /* 删除边 */ - // 参数 i, j 对应 vertices 元素索引 - func (g *graphAdjMat) removeEdge(i, j int) { - // 索引越界与相等处理 - if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j { - fmt.Errorf("%s", "Index Out Of Bounds Exception") - } - g.adjMat[i][j] = 0 - g.adjMat[j][i] = 0 - } + [class]{graphAdjMat}-[func]{} ``` === "JavaScript" ```javascript title="graph_adjacency_matrix.js" - + [class]{GraphAdjMat}-[func]{} ``` === "TypeScript" ```typescript title="graph_adjacency_matrix.ts" - + [class]{GraphAdjMat}-[func]{} ``` === "C" ```c title="graph_adjacency_matrix.c" - + [class]{graphAdjMat}-[func]{} ``` === "C#" @@ -166,83 +83,7 @@ comments: true === "Swift" ```swift title="graph_adjacency_matrix.swift" - /* 基于邻接矩阵实现的无向图类 */ - class GraphAdjMat { - private var vertices: [Int] // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” - private var adjMat: [[Int]] // 邻接矩阵,行列索引对应“顶点索引” - - /* 构造函数 */ - init(vertices: [Int], edges: [[Int]]) { - self.vertices = [] - adjMat = [] - // 添加顶点 - for val in vertices { - addVertex(val: val) - } - // 添加边 - // 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 - for e in edges { - addEdge(i: e[0], j: e[1]) - } - } - - /* 获取顶点数量 */ - func size() -> Int { - vertices.count - } - - /* 添加顶点 */ - func addVertex(val: Int) { - let n = size() - // 向顶点列表中添加新顶点的值 - vertices.append(val) - // 在邻接矩阵中添加一行 - let newRow = Array(repeating: 0, count: n) - adjMat.append(newRow) - // 在邻接矩阵中添加一列 - for i in adjMat.indices { - adjMat[i].append(0) - } - } - - /* 删除顶点 */ - func removeVertex(index: Int) { - if index >= size() { - fatalError("越界") - } - // 在顶点列表中移除索引 index 的顶点 - vertices.remove(at: index) - // 在邻接矩阵中删除索引 index 的行 - adjMat.remove(at: index) - // 在邻接矩阵中删除索引 index 的列 - for i in adjMat.indices { - adjMat[i].remove(at: index) - } - } - - /* 添加边 */ - // 参数 i, j 对应 vertices 元素索引 - func addEdge(i: Int, j: Int) { - // 索引越界与相等处理 - if i < 0 || j < 0 || i >= size() || j >= size() || i == j { - fatalError("越界") - } - // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i) - adjMat[i][j] = 1 - adjMat[j][i] = 1 - } - - /* 删除边 */ - // 参数 i, j 对应 vertices 元素索引 - func removeEdge(i: Int, j: Int) { - // 索引越界与相等处理 - if i < 0 || j < 0 || i >= size() || j >= size() || i == j { - fatalError("越界") - } - adjMat[i][j] = 0 - adjMat[j][i] = 0 - } - } + [class]{GraphAdjMat}-[func]{} ``` === "Zig" @@ -289,121 +130,49 @@ comments: true === "C++" ```cpp title="graph_adjacency_list.cpp" + [class]{Vertex}-[func]{} + [class]{GraphAdjList}-[func]{} ``` === "Python" ```python title="graph_adjacency_list.py" + [class]{Vertex}-[func]{} + [class]{GraphAdjList}-[func]{} ``` === "Go" ```go title="graph_adjacency_list.go" - /* 顶点类 */ - type vertex struct { - val int - } - - func newVertex(val int) vertex { - return vertex{ - val: val, - } - } - - /* 基于邻接表实现的无向图类 */ - type graphAdjList struct { - // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象 - // 邻接表(使用哈希表实现), 使用哈希表模拟集合 - adjList map[vertex]map[vertex]struct{} - } - - /* 构造函数 */ - func newGraphAdjList(edges [][]vertex) *graphAdjList { - g := &graphAdjList{ - adjList: make(map[vertex]map[vertex]struct{}), - } - // 添加所有顶点和边 - for _, edge := range edges { - g.addVertex(edge[0]) - g.addVertex(edge[1]) - g.addEdge(edge[0], edge[1]) - } - return g - } - - /* 获取顶点数量 */ - func (g *graphAdjList) size() int { - return len(g.adjList) - } - - /* 添加边 */ - func (g *graphAdjList) addEdge(vet1 vertex, vet2 vertex) { - _, ok1 := g.adjList[vet1] - _, ok2 := g.adjList[vet2] - if !ok1 || !ok2 || vet1 == vet2 { - panic("error") - } - // 添加边 vet1 - vet2, 添加匿名 struct{}, - g.adjList[vet1][vet2] = struct{}{} - g.adjList[vet2][vet1] = struct{}{} - } - - /* 删除边 */ - func (g *graphAdjList) removeEdge(vet1 vertex, vet2 vertex) { - _, ok1 := g.adjList[vet1] - _, ok2 := g.adjList[vet2] - if !ok1 || !ok2 || vet1 == vet2 { - panic("error") - } - // 删除边 vet1 - vet2, 借助 delete 来删除 map 中的键 - delete(g.adjList[vet1], vet2) - delete(g.adjList[vet2], vet1) - } - - /* 添加顶点 */ - func (g *graphAdjList) addVertex(vet vertex) { - _, ok := g.adjList[vet] - if ok { - return - } - // 在邻接表中添加一个新链表(即 set) - g.adjList[vet] = make(map[vertex]struct{}) - } - - /* 删除顶点 */ - func (g *graphAdjList) removeVertex(vet vertex) { - _, ok := g.adjList[vet] - if !ok { - panic("error") - } - // 在邻接表中删除顶点 vet 对应的链表 - delete(g.adjList, vet) - // 遍历其它顶点的链表(即 Set),删除所有包含 vet 的边 - for _, set := range g.adjList { - // 操作 - delete(set, vet) - } - } + [class]{vertex}-[func]{} + + [class]{graphAdjList}-[func]{} ``` === "JavaScript" ```javascript title="graph_adjacency_list.js" + [class]{Vertex}-[func]{} + [class]{GraphAdjList}-[func]{} ``` === "TypeScript" ```typescript title="graph_adjacency_list.ts" + [class]{Vertex}-[func]{} + [class]{GraphAdjList}-[func]{} ``` === "C" ```c title="graph_adjacency_list.c" + [class]{vertex}-[func]{} + [class]{graphAdjList}-[func]{} ``` === "C#" @@ -417,91 +186,17 @@ comments: true === "Swift" ```swift title="graph_adjacency_list.swift" - /* 顶点类 */ - class Vertex: Hashable { - var val: Int - - init(val: Int) { - self.val = val - } - - static func == (lhs: Vertex, rhs: Vertex) -> Bool { - lhs.val == rhs.val - } - - func hash(into hasher: inout Hasher) { - hasher.combine(val) - } - } - - /* 基于邻接表实现的无向图类 */ - class GraphAdjList { - // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象 - private var adjList: [Vertex: Set] // 邻接表(使用哈希表实现) - - init(edges: [[Vertex]]) { - adjList = [:] - // 添加所有顶点和边 - for edge in edges { - addVertex(vet: edge[0]) - addVertex(vet: edge[1]) - addEdge(vet1: edge[0], vet2: edge[1]) - } - } - - /* 获取顶点数量 */ - func size() -> Int { - adjList.count - } - - /* 添加边 */ - func addEdge(vet1: Vertex, vet2: Vertex) { - if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 { - fatalError("参数错误") - } - // 添加边 vet1 - vet2 - adjList[vet1]?.insert(vet2) - adjList[vet2]?.insert(vet1) - } - - /* 删除边 */ - func removeEdge(vet1: Vertex, vet2: Vertex) { - if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 { - fatalError("参数错误") - } - // 删除边 vet1 - vet2 - adjList[vet1]?.remove(vet2) - adjList[vet2]?.remove(vet1) - } - - /* 添加顶点 */ - func addVertex(vet: Vertex) { - if adjList[vet] != nil { - return - } - // 在邻接表中添加一个新链表(即 HashSet) - adjList[vet] = [] - } - - /* 删除顶点 */ - func removeVertex(vet: Vertex) { - if adjList[vet] == nil { - fatalError("参数错误") - } - // 在邻接表中删除顶点 vet 对应的链表(即 HashSet) - adjList.removeValue(forKey: vet) - // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边 - for key in adjList.keys { - adjList[key]?.remove(vet) - } - } - } + [class]{Vertex}-[func]{} + + [class]{GraphAdjList}-[func]{} ``` === "Zig" ```zig title="graph_adjacency_list.zig" + [class]{Vertex}-[func]{} + [class]{GraphAdjList}-[func]{} ``` ## 9.2.3. 效率对比 diff --git a/docs/utils/extract_code_go.py b/docs/utils/extract_code_go.py index 194dfbf39..a51450b9e 100644 --- a/docs/utils/extract_code_go.py +++ b/docs/utils/extract_code_go.py @@ -66,7 +66,7 @@ class ExtractCodeBlocksGo(ExtractCodeBlocksJava): def check_func_blong_to_class(label): class_label_pattern = re.compile(f".*\*{label}\).*") - func_return_pattern = re.compile(f".*\*{label}.*") + func_return_pattern = re.compile(f".*{label}.*") constructor_pattern = re.compile(f".*new.*") class_label_match = class_label_pattern.match(f"{func_cls_label}") @@ -172,7 +172,7 @@ class ExtractCodeBlocksGo(ExtractCodeBlocksJava): replace_tabs(func) -# for code_path in glob.glob("codes/go/chapter_*/array_hash_map.go"): -# ext = ExtractCodeBlocksGo() -# res = ext.extract(code_path) -# pass +for code_path in glob.glob("codes/*/chapter_*/graph_adjacency_matrix.go"): + ext = ExtractCodeBlocksGo() + res = ext.extract(code_path) + pass