Skip to content

Commit

Permalink
Add code source blocks to the chapter Graph.
Browse files Browse the repository at this point in the history
Fix "函数" and "方法"
  • Loading branch information
krahets committed Feb 9, 2023
1 parent d37c71b commit 3000163
Show file tree
Hide file tree
Showing 47 changed files with 108 additions and 411 deletions.
4 changes: 2 additions & 2 deletions codes/c/chapter_array_and_linkedlist/my_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef struct myList myList;
/* 前置声明 */
void extendCapacity(myList *list);

/* 构造函数 */
/* 构造方法 */
myList *newMyList() {
myList *list = malloc(sizeof(myList));
list->capacity = 10;
Expand All @@ -29,7 +29,7 @@ myList *newMyList() {
return list;
}

/* 析构函数 */
/* 析构方法 */
void delMyList(myList *list) {
free(list->nums);
free(list);
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_heap/my_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_stack_and_queue/array_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct ArrayQueue {

typedef struct ArrayQueue ArrayQueue;

/* 构造函数 */
/* 构造方法 */
ArrayQueue *newArrayQueue(int capacity) {
ArrayQueue *queue = (ArrayQueue *) malloc(sizeof(ArrayQueue));
// 初始化数组
Expand All @@ -26,7 +26,7 @@ ArrayQueue *newArrayQueue(int capacity) {
return queue;
}

/* 析构函数 */
/* 析构方法 */
void delArrayQueue(ArrayQueue *queue) {
free(queue->nums);
queue->queCapacity = 0;
Expand Down
8 changes: 4 additions & 4 deletions codes/c/chapter_stack_and_queue/linkedlist_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ struct linkedListStack {

typedef struct linkedListStack linkedListStack;

/* 构造函数 */
/* 构造方法 */
linkedListStack *newLinkedListStack() {
linkedListStack *s = malloc(sizeof(linkedListStack));
s->top = NULL;
s->size = 0;
return s;
}

/* 析构函数 */
/* 析构方法 */
void delLinkedListStack(linkedListStack *s) {
while (s->top) {
ListNode *n = s->top->next;
Expand Down Expand Up @@ -80,7 +80,7 @@ int pop(linkedListStack *s) {
/* Driver Code */
int main() {
/* 初始化栈 */
// 构造函数
// 构造方法
linkedListStack *stack = newLinkedListStack();

/* 元素入栈 */
Expand Down Expand Up @@ -109,7 +109,7 @@ int main() {
bool empty = isEmpty(stack);
printf("栈是否为空 = %s\n", empty ? "true" : "false");

/* 析构函数 */
/* 析构方法 */
delLinkedListStack(stack);
return 0;
}
4 changes: 2 additions & 2 deletions codes/c/chapter_tree/avl_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ TreeNode *rotate(TreeNode *node) {
return node;
}

/* 递归插入结点(辅助函数*/
/* 递归插入结点(辅助方法*/
TreeNode *insertHelper(TreeNode *node, int val) {
if (node == NULL) {
return newTreeNode(val);
Expand Down Expand Up @@ -151,7 +151,7 @@ TreeNode *getInOrderNext(TreeNode *node) {
return node;
}

/* 递归删除结点(辅助函数*/
/* 递归删除结点(辅助方法*/
TreeNode *removeHelper(TreeNode *node, int val) {
TreeNode *child, *grandChild, *temp;
if (node == NULL) {
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_array_and_linkedlist/my_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class MyList {
int extendRatio = 2; // 每次列表扩容的倍数

public:
/* 构造函数 */
/* 构造方法 */
MyList() {
nums = new int[numsCapacity];
}

/* 析构函数 */
/* 析构方法 */
~MyList() {
delete[] nums;
}
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_heap/my_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MaxHeap {
}

public:
/* 构造函数,根据输入列表建堆 */
/* 构造方法,根据输入列表建堆 */
MaxHeap(vector<int> nums) {
// 将列表元素原封不动添加进堆
maxHeap = nums;
Expand Down
8 changes: 4 additions & 4 deletions codes/cpp/chapter_tree/avl_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AVLTree {
return node;
}

/* 递归插入结点(辅助函数*/
/* 递归插入结点(辅助方法*/
TreeNode* insertHelper(TreeNode* node, int val) {
if (node == nullptr) return new TreeNode(val);
/* 1. 查找插入位置,并插入结点 */
Expand All @@ -102,7 +102,7 @@ class AVLTree {
return node;
}

/* 递归删除结点(辅助函数*/
/* 递归删除结点(辅助方法*/
TreeNode* removeHelper(TreeNode* node, int val) {
if (node == nullptr) return nullptr;
/* 1. 查找结点,并删除之 */
Expand Down Expand Up @@ -183,10 +183,10 @@ class AVLTree {
return cur;
}

/*构造函数*/
/*构造方法*/
AVLTree() : root(nullptr) {}

/*析构函数*/
/*析构方法*/
~AVLTree() {
freeMemoryTree(root);
}
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_array_and_linkedlist/my_list.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MyList
private int numsSize = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数

/* 构造函数 */
/* 构造方法 */
public MyList()
{
nums = new int[numsCapacity];
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_tree/avl_tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public int balanceFactor(TreeNode? node)
return root;
}

/* 递归插入结点(辅助函数) */
/* 递归插入结点(辅助方法) */
private TreeNode? insertHelper(TreeNode? node, int val)
{
if (node == null) return new TreeNode(val);
Expand All @@ -138,7 +138,7 @@ public int balanceFactor(TreeNode? node)
return root;
}

/* 递归删除结点(辅助函数) */
/* 递归删除结点(辅助方法) */
private TreeNode? removeHelper(TreeNode? node, int val)
{
if (node == null) return null;
Expand Down
2 changes: 1 addition & 1 deletion codes/dart/chapter_array_and_linkedlist/my_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MyList {
int _size = 0; // 列表长度(即当前元素数量)
int _extendRatio = 2; // 每次列表扩容的倍数

/* 构造函数 */
/* 构造方法 */
MyList() {
_nums = List.filled(_capacity, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_array_and_linkedlist/my_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type myList struct {
extendRatio int
}

/* 构造函数 */
/* 构造方法 */
func newMyList() *myList {
return &myList{
numsCapacity: 10, // 列表容量
Expand Down
3 changes: 2 additions & 1 deletion codes/go/chapter_graph/graph_adjacency_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type vertex struct {
val int
}

/* 构造方法 */
func newVertex(val int) vertex {
return vertex{
val: val,
Expand All @@ -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{}),
Expand Down
1 change: 1 addition & 0 deletions codes/go/chapter_graph/graph_adjacency_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type graphAdjMat struct {
adjMat [][]int
}

/* 构造方法 */
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
// 添加顶点
n := len(vertices)
Expand Down
4 changes: 2 additions & 2 deletions codes/go/chapter_heap/my_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions codes/go/chapter_tree/avl_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_array_and_linkedlist/my_list.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MyList {
private int size = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数

/* 构造函数 */
/* 构造方法 */
public MyList() {
nums = new int[capacity];
}
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_adjacency_list.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GraphAdjList {
// 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
Map<Vertex, Set<Vertex>> adjList; // 邻接表(使用哈希表实现)

/* 构造函数 */
/* 构造方法 */
public GraphAdjList(Vertex[][] edges) {
this.adjList = new HashMap<>();
// 添加所有顶点和边
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_adjacency_matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GraphAdjMat {
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
List<List<Integer>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”

/* 构造函数 */
/* 构造方法 */
public GraphAdjMat(int[] vertices, int[][] edges) {
this.vertices = new ArrayList<>();
this.adjMat = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_heap/my_heap.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MaxHeap {
// 使用列表而非数组,这样无需考虑扩容问题
private List<Integer> maxHeap;

/* 构造函数,根据输入列表建堆 */
/* 构造方法,根据输入列表建堆 */
public MaxHeap(List<Integer> nums) {
// 将列表元素原封不动添加进堆
maxHeap = new ArrayList<>(nums);
Expand Down
4 changes: 2 additions & 2 deletions codes/java/chapter_tree/avl_tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public TreeNode insert(int val) {
return root;
}

/* 递归插入结点(辅助函数) */
/* 递归插入结点(辅助方法) */
private TreeNode insertHelper(TreeNode node, int val) {
if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入结点 */
Expand All @@ -119,7 +119,7 @@ public TreeNode remove(int val) {
return root;
}

/* 递归删除结点(辅助函数) */
/* 递归删除结点(辅助方法) */
private TreeNode removeHelper(TreeNode node, int val) {
if (node == null) return null;
/* 1. 查找结点,并删除之 */
Expand Down
2 changes: 1 addition & 1 deletion codes/javascript/chapter_array_and_linkedlist/my_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MyList {
#size = 0; // 列表长度(即当前元素数量)
#extendRatio = 2; // 每次列表扩容的倍数

/* 构造函数 */
/* 构造方法 */
constructor() {
this.#nums = new Array(this.#capacity);
}
Expand Down
2 changes: 1 addition & 1 deletion codes/javascript/chapter_graph/graph_adjacency_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Vertex {
/* 基于邻接表实现的无向图类 */
class GraphAdjList {
adjList;
/* 构造函数 */
/* 构造方法 */
constructor(edges) {
this.adjList = new Map();
// 添加所有顶点和边
Expand Down
2 changes: 1 addition & 1 deletion codes/javascript/chapter_heap/my_heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { printHeap } = require("../include/PrintUtil");
class MaxHeap {
#maxHeap;

/* 构造函数,建立空堆或根据输入列表建堆 */
/* 构造方法,建立空堆或根据输入列表建堆 */
constructor(nums) {
// 将列表元素原封不动添加进堆
this.#maxHeap = nums === undefined ? [] : [...nums];
Expand Down
6 changes: 3 additions & 3 deletions codes/javascript/chapter_tree/avl_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { printTree } = require("../include/PrintUtil");

/* AVL 树*/
class AVLTree {
/*构造函数*/
/*构造方法*/
constructor() {
this.root = null; //根结点
}
Expand Down Expand Up @@ -98,7 +98,7 @@ class AVLTree {
return this.root;
}

/* 递归插入结点(辅助函数) */
/* 递归插入结点(辅助方法) */
insertHelper(node, val) {
if (node === null) return new TreeNode(val);
/* 1. 查找插入位置,并插入结点 */
Expand All @@ -118,7 +118,7 @@ class AVLTree {
return this.root;
}

/* 递归删除结点(辅助函数) */
/* 递归删除结点(辅助方法) */
removeHelper(node, val) {
if (node === null) return null;
/* 1. 查找结点,并删除之 */
Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_array_and_linkedlist/my_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

""" 列表类简易实现 """
class MyList:
""" 构造函数 """
""" 构造方法 """
def __init__(self):
self.__capacity = 10 # 列表容量
self.__nums = [0] * self.__capacity # 数组(存储列表元素)
Expand Down
4 changes: 2 additions & 2 deletions codes/python/chapter_tree/avl_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def insert(self, val) -> TreeNode:
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)
Expand All @@ -111,7 +111,7 @@ def remove(self, val: int):
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
Expand Down
Loading

0 comments on commit 3000163

Please sign in to comment.