From 7b7e1b7c8b3cfe2ae8dedc74c91cd4dbc7dccd71 Mon Sep 17 00:00:00 2001 From: krahets Date: Sun, 29 Oct 2023 20:01:26 +0800 Subject: [PATCH] build --- docs/chapter_graph/graph_operations.md | 438 ++++++++++++------------- docs/chapter_graph/graph_traversal.md | 142 ++++---- docs/chapter_hashing/hash_collision.md | 8 +- docs/chapter_heap/top_k.md | 50 ++- 4 files changed, 326 insertions(+), 312 deletions(-) diff --git a/docs/chapter_graph/graph_operations.md b/docs/chapter_graph/graph_operations.md index ca198faa9..945b91445 100644 --- a/docs/chapter_graph/graph_operations.md +++ b/docs/chapter_graph/graph_operations.md @@ -40,14 +40,11 @@ comments: true class GraphAdjMat: """基于邻接矩阵实现的无向图类""" - # 顶点列表,元素代表“顶点值”,索引代表“顶点索引” - vertices: list[int] = [] - # 邻接矩阵,行列索引对应“顶点索引” - adj_mat: list[list[int]] = [] - def __init__(self, vertices: list[int], edges: list[list[int]]): """构造方法""" + # 顶点列表,元素代表“顶点值”,索引代表“顶点索引” self.vertices: list[int] = [] + # 邻接矩阵,行列索引对应“顶点索引” self.adj_mat: list[list[int]] = [] # 添加顶点 for val in vertices: @@ -945,24 +942,72 @@ comments: true === "C" ```c title="graph_adjacency_matrix.c" - /* 基于邻接矩阵实现的无向图类结构 */ + /* 基于邻接矩阵实现的无向图结构体 */ typedef struct { - int *vertices; // 顶点列表 - int **adjMat; // 邻接矩阵,元素代表“边”,索引代表“顶点索引” - int size; // 顶点数量 - int capacity; // 图容量 + int vertices[MAX_SIZE]; + int adjMat[MAX_SIZE][MAX_SIZE]; + int size; } GraphAdjMat; + /* 构造函数 */ + GraphAdjMat *newGraphAdjMat() { + GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat)); + graph->size = 0; + for (int i = 0; i < MAX_SIZE; i++) { + for (int j = 0; j < MAX_SIZE; j++) { + graph->adjMat[i][j] = 0; + } + } + return graph; + } + + /* 添加顶点 */ + void addVertex(GraphAdjMat *graph, int val) { + if (graph->size == MAX_SIZE) { + fprintf(stderr, "图的顶点数量已达最大值\n"); + return; + } + // 添加第 n 个顶点,并将第 n 行和列置零 + int n = graph->size; + graph->vertices[n] = val; + for (int i = 0; i <= n; i++) { + graph->adjMat[n][i] = graph->adjMat[i][n] = 0; + } + graph->size++; + } + + /* 删除顶点 */ + void removeVertex(GraphAdjMat *graph, int index) { + if (index < 0 || index >= graph->size) { + fprintf(stderr, "顶点索引越界\n"); + return; + } + // 在顶点列表中移除索引 index 的顶点 + for (int i = index; i < graph->size - 1; i++) { + graph->vertices[i] = graph->vertices[i + 1]; + } + // 在邻接矩阵中删除索引 index 的行 + for (int i = index; i < graph->size - 1; i++) { + for (int j = 0; j < graph->size; j++) { + graph->adjMat[i][j] = graph->adjMat[i + 1][j]; + } + } + // 在邻接矩阵中删除索引 index 的列 + for (int i = 0; i < graph->size; i++) { + for (int j = index; j < graph->size - 1; j++) { + graph->adjMat[i][j] = graph->adjMat[i][j + 1]; + } + } + graph->size--; + } + /* 添加边 */ // 参数 i, j 对应 vertices 元素索引 void addEdge(GraphAdjMat *graph, int i, int j) { - // 越界检查 if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) { - printf("Out of range in %s:%d\n", __FILE__, __LINE__); - exit(1); + fprintf(stderr, "边索引越界或相等\n"); + return; } - // 添加边 - // 参数 i, j 对应 vertices 元素索引 graph->adjMat[i][j] = 1; graph->adjMat[j][i] = 1; } @@ -970,137 +1015,22 @@ comments: true /* 删除边 */ // 参数 i, j 对应 vertices 元素索引 void removeEdge(GraphAdjMat *graph, int i, int j) { - // 越界检查 if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) { - printf("Out of range in %s:%d\n", __FILE__, __LINE__); - exit(1); + fprintf(stderr, "边索引越界或相等\n"); + return; } - // 删除边 - // 参数 i, j 对应 vertices 元素索引 graph->adjMat[i][j] = 0; graph->adjMat[j][i] = 0; } - /* 添加顶点 */ - void addVertex(GraphAdjMat *graph, int val) { - // 如果实际使用不大于预设空间,则直接初始化新空间 - if (graph->size < graph->capacity) { - graph->vertices[graph->size] = val; // 初始化新顶点值 - for (int i = 0; i < graph->size; i++) { - graph->adjMat[i][graph->size] = 0; // 邻接矩新列阵置0 - } - memset(graph->adjMat[graph->size], 0, sizeof(int) * (graph->size + 1)); // 将新增行置 0 - graph->size++; - return; - } - // 扩容,申请新的顶点数组 - int *temp = (int *)malloc(sizeof(int) * (graph->size * 2)); - memcpy(temp, graph->vertices, sizeof(int) * graph->size); - temp[graph->size] = val; - // 释放原数组 - free(graph->vertices); - graph->vertices = temp; - // 扩容,申请新的二维数组 - int **tempMat = (int **)malloc(sizeof(int *) * graph->size * 2); - int *tempMatLine = (int *)malloc(sizeof(int) * (graph->size * 2) * (graph->size * 2)); - memset(tempMatLine, 0, sizeof(int) * (graph->size * 2) * (graph->size * 2)); - for (int k = 0; k < graph->size * 2; k++) { - tempMat[k] = tempMatLine + k * (graph->size * 2); - } + /* 打印邻接矩阵 */ + void printGraphAdjMat(GraphAdjMat *graph) { + printf("顶点列表 = "); + printArray(graph->vertices, graph->size); + printf("邻接矩阵 =\n"); for (int i = 0; i < graph->size; i++) { - memcpy(tempMat[i], graph->adjMat[i], sizeof(int) * graph->size); // 原数据复制到新数组 + printArray(graph->adjMat[i], graph->size); } - for (int i = 0; i < graph->size; i++) { - tempMat[i][graph->size] = 0; // 将新增列置 0 - } - memset(tempMat[graph->size], 0, sizeof(int) * (graph->size + 1)); // 将新增行置 0 - // 释放原数组 - free(graph->adjMat[0]); - free(graph->adjMat); - // 扩容后,指向新地址 - graph->adjMat = tempMat; // 指向新的邻接矩阵地址 - graph->capacity = graph->size * 2; - graph->size++; - } - - /* 删除顶点 */ - void removeVertex(GraphAdjMat *graph, int index) { - // 越界检查 - if (index < 0 || index >= graph->size) { - printf("Out of range in %s:%d\n", __FILE__, __LINE__); - exit(1); - } - for (int i = index; i < graph->size - 1; i++) { - graph->vertices[i] = graph->vertices[i + 1]; // 清除删除的顶点,并将其后所有顶点前移 - } - graph->vertices[graph->size - 1] = 0; // 将被前移的最后一个顶点置 0 - // 清除邻接矩阵中删除的列 - for (int i = 0; i < graph->size - 1; i++) { - if (i < index) { - for (int j = index; j < graph->size - 1; j++) { - graph->adjMat[i][j] = graph->adjMat[i][j + 1]; // 被删除列后的所有列前移 - } - } else { - memcpy(graph->adjMat[i], graph->adjMat[i + 1], sizeof(int) * graph->size); // 被删除行的下方所有行上移 - for (int j = index; j < graph->size; j++) { - graph->adjMat[i][j] = graph->adjMat[i][j + 1]; // 被删除列后的所有列前移 - } - } - } - graph->size--; - } - - /* 打印顶点与邻接矩阵 */ - void printGraph(GraphAdjMat *graph) { - if (graph->size == 0) { - printf("graph is empty\n"); - return; - } - printf("顶点列表 = ["); - for (int i = 0; i < graph->size; i++) { - if (i != graph->size - 1) { - printf("%d, ", graph->vertices[i]); - } else { - printf("%d", graph->vertices[i]); - } - } - printf("]\n"); - printf("邻接矩阵 =\n[\n"); - for (int i = 0; i < graph->size; i++) { - printf(" ["); - for (int j = 0; j < graph->size; j++) { - if (j != graph->size - 1) { - printf("%u, ", graph->adjMat[i][j]); - } else { - printf("%u", graph->adjMat[i][j]); - } - } - printf("],\n"); - } - printf("]\n"); - } - - /* 构造函数 */ - GraphAdjMat *newGraphAjdMat(int numberVertices, int *vertices, int **adjMat) { - // 申请内存 - GraphAdjMat *newGraph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat)); // 为图分配内存 - newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存 - newGraph->adjMat = (int **)malloc(sizeof(int *) * numberVertices * 2); // 为邻接矩阵分配二维内存 - int *temp = (int *)malloc(sizeof(int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存 - newGraph->size = numberVertices; // 初始化顶点数量 - newGraph->capacity = numberVertices * 2; // 初始化图容量 - // 配置二维数组 - for (int i = 0; i < numberVertices * 2; i++) { - newGraph->adjMat[i] = temp + i * numberVertices * 2; // 将二维指针指向一维数组 - } - // 赋值 - memcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices); - for (int i = 0; i < numberVertices; i++) { - memcpy(newGraph->adjMat[i], adjMat[i], - sizeof(int) * numberVertices); // 将传入的邻接矩阵赋值给结构体内邻接矩阵 - } - // 返回结构体指针 - return newGraph; } ``` @@ -1967,120 +1897,160 @@ comments: true === "C" ```c title="graph_adjacency_list.c" - /* 基于邻接链表实现的无向图类结构 */ + /* 节点结构体 */ + typedef struct AdjListNode { + Vertex *vertex; // 顶点 + struct AdjListNode *next; // 后继节点 + } AdjListNode; + + /* 查找顶点对应的节点 */ + AdjListNode *findNode(GraphAdjList *graph, Vertex *vet) { + for (int i = 0; i < graph->size; i++) { + if (graph->heads[i]->vertex == vet) { + return graph->heads[i]; + } + } + return NULL; + } + + /* 添加边辅助函数 */ + void addEdgeHelper(AdjListNode *head, Vertex *vet) { + AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode)); + node->vertex = vet; + // 头插法 + node->next = head->next; + head->next = node; + } + + /* 删除边辅助函数 */ + void removeEdgeHelper(AdjListNode *head, Vertex *vet) { + AdjListNode *pre = head; + AdjListNode *cur = head->next; + // 在链表中搜索 vet 对应节点 + while (cur != NULL && cur->vertex != vet) { + pre = cur; + cur = cur->next; + } + if (cur == NULL) + return; + // 将 vet 对应节点从链表中删除 + pre->next = cur->next; + // 释放内存 + free(cur); + } + + /* 基于邻接表实现的无向图类 */ typedef struct { - Vertex **vertices; // 邻接表 - unsigned int size; // 顶点数量 - unsigned int capacity; // 顶点容量 + AdjListNode *heads[MAX_SIZE]; // 节点数组 + int size; // 节点数量 } GraphAdjList; - /* 添加边 */ - void addEdge(GraphAdjList *graph, int i, int j) { - // 越界检查 - if (i < 0 || j < 0 || i == j || i >= graph->size || j >= graph->size) { - printf("Out of range in %s:%d\n", __FILE__, __LINE__); - return; + /* 构造函数 */ + GraphAdjList *newGraphAdjList() { + GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList)); + if (!graph) { + return NULL; } - // 查找欲添加边的顶点 vet1 - vet2 - Vertex *vet1 = graph->vertices[i]; - Vertex *vet2 = graph->vertices[j]; - // 连接顶点 vet1 - vet2 - pushBack(vet1->list, vet2); - pushBack(vet2->list, vet1); + graph->size = 0; + for (int i = 0; i < MAX_SIZE; i++) { + graph->heads[i] = NULL; + } + return graph; + } + + /* 析构函数 */ + void delGraphAdjList(GraphAdjList *graph) { + for (int i = 0; i < graph->size; i++) { + AdjListNode *cur = graph->heads[i]; + while (cur != NULL) { + AdjListNode *next = cur->next; + if (cur != graph->heads[i]) { + free(cur); + } + cur = next; + } + free(graph->heads[i]->vertex); + free(graph->heads[i]); + } + free(graph); + } + + /* 查找顶点对应的节点 */ + AdjListNode *findNode(GraphAdjList *graph, Vertex *vet) { + for (int i = 0; i < graph->size; i++) { + if (graph->heads[i]->vertex == vet) { + return graph->heads[i]; + } + } + return NULL; + } + + /* 添加边 */ + void addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { + AdjListNode *head1 = findNode(graph, vet1); + AdjListNode *head2 = findNode(graph, vet2); + assert(head1 != NULL && head2 != NULL && head1 != head2); + // 添加边 vet1 - vet2 + addEdgeHelper(head1, vet2); + addEdgeHelper(head2, vet1); } /* 删除边 */ - void removeEdge(GraphAdjList *graph, int i, int j) { - // 越界检查 - if (i < 0 || j < 0 || i == j || i >= graph->size || j >= graph->size) { - printf("Out of range in %s:%d\n", __FILE__, __LINE__); - return; - } - // 查找欲删除边的顶点 vet1 - vet2 - Vertex *vet1 = graph->vertices[i]; - Vertex *vet2 = graph->vertices[j]; - // 移除待删除边 vet1 - vet2 - removeLink(vet1->list, vet2); - removeLink(vet2->list, vet1); + void removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) { + AdjListNode *head1 = findNode(graph, vet1); + AdjListNode *head2 = findNode(graph, vet2); + assert(head1 != NULL && head2 != NULL); + // 删除边 vet1 - vet2 + removeEdgeHelper(head1, head2->vertex); + removeEdgeHelper(head2, head1->vertex); } /* 添加顶点 */ - void addVertex(GraphAdjList *graph, int val) { - // 若大小超过容量,则扩容 - if (graph->size >= graph->capacity) { - Vertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * graph->capacity); - memcpy(tempList, graph->vertices, sizeof(Vertex *) * graph->size); - free(graph->vertices); // 释放原邻接表内存 - graph->vertices = tempList; // 指向新邻接表 - graph->capacity = graph->capacity * 2; // 容量扩大至2倍 - } - // 申请新顶点内存并将新顶点地址存入顶点列表 - Vertex *newV = newVertex(val); // 建立新顶点 - newV->pos = graph->size; // 为新顶点标记下标 - newV->list = newLinklist(newV); // 为新顶点建立链表 - graph->vertices[graph->size] = newV; // 将新顶点加入邻接表 - graph->size++; + void addVertex(GraphAdjList *graph, Vertex *vet) { + assert(graph != NULL && graph->size < MAX_SIZE); + AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode)); + head->vertex = vet; + head->next = NULL; + // 在邻接表中添加一个新链表 + graph->heads[graph->size++] = head; } /* 删除顶点 */ - void removeVertex(GraphAdjList *graph, unsigned int index) { - // 越界检查 - if (index < 0 || index >= graph->size) { - printf("Out of range in %s:%d\n", __FILE__, __LINE__); - exit(1); + void removeVertex(GraphAdjList *graph, Vertex *vet) { + AdjListNode *node = findNode(graph, vet); + assert(node != NULL); + // 在邻接表中删除顶点 vet 对应的链表 + AdjListNode *cur = node, *pre = NULL; + while (cur) { + pre = cur; + cur = cur->next; + free(pre); } - Vertex *vet = graph->vertices[index]; // 查找待删节点 - if (vet == 0) { // 若不存在该节点,则返回 - printf("index is:%d\n", index); - printf("Out of range in %s:%d\n", __FILE__, __LINE__); - return; - } - // 遍历待删除顶点的链表,将所有与待删除节点有关的边删除 - Node *temp = vet->list->head->next; - while (temp != 0) { - removeLink(temp->val->list, vet); // 删除与该顶点有关的边 - temp = temp->next; - } - // 将顶点前移 - for (int i = index; i < graph->size - 1; i++) { - graph->vertices[i] = graph->vertices[i + 1]; // 顶点前移 - graph->vertices[i]->pos--; // 所有前移的顶点索引值减 1 - } - graph->vertices[graph->size - 1] = 0; - graph->size--; - // 释放内存 - freeVertex(vet); - } - - /* 打印顶点与邻接矩阵 */ - void printGraph(GraphAdjList *graph) { - printf("邻接表 =\n"); + // 遍历其他顶点的链表,删除所有包含 vet 的边 for (int i = 0; i < graph->size; i++) { - Node *n = graph->vertices[i]->list->head->next; - printf("%d: [", graph->vertices[i]->val); - while (n != 0) { - if (n->next != 0) { - printf("%d, ", n->val->val); - } else { - printf("%d", n->val->val); + cur = graph->heads[i]; + pre = NULL; + while (cur) { + pre = cur; + cur = cur->next; + if (cur && cur->vertex == vet) { + pre->next = cur->next; + free(cur); + break; } - n = n->next; } - printf("]\n"); } - } - - /* 构造函数 */ - GraphAdjList *newGraphAdjList(unsigned int verticesCapacity) { - // 申请内存 - GraphAdjList *newGraph = (GraphAdjList *)malloc(sizeof(GraphAdjList)); - // 建立顶点表并分配内存 - newGraph->vertices = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // 为顶点列表分配内存 - memset(newGraph->vertices, 0, sizeof(Vertex *) * verticesCapacity); // 顶点列表置 0 - newGraph->size = 0; // 初始化顶点数量 - newGraph->capacity = verticesCapacity; // 初始化顶点容量 - // 返回图指针 - return newGraph; + // 将该顶点之后的顶点向前移动,以填补空缺 + int i; + for (i = 0; i < graph->size; i++) { + if (graph->heads[i] == node) + break; + } + for (int j = i; j < graph->size - 1; j++) { + graph->heads[j] = graph->heads[j + 1]; + } + graph->size--; + free(vet); } ``` diff --git a/docs/chapter_graph/graph_traversal.md b/docs/chapter_graph/graph_traversal.md index 624ce06e2..f7d81635c 100644 --- a/docs/chapter_graph/graph_traversal.md +++ b/docs/chapter_graph/graph_traversal.md @@ -342,43 +342,73 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这 === "C" ```c title="graph_bfs.c" - /* 广度优先遍历 */ + /* 节点队列结构体 */ + typedef struct { + Vertex *vertices[MAX_SIZE]; + int front, rear, size; + } Queue; + + /* 构造函数 */ + Queue *newQueue() { + Queue *q = (Queue *)malloc(sizeof(Queue)); + q->front = q->rear = q->size = 0; + return q; + } + + /* 判断队列是否为空 */ + int isEmpty(Queue *q) { + return q->size == 0; + } + + /* 入队操作 */ + void enqueue(Queue *q, Vertex *vet) { + q->vertices[q->rear] = vet; + q->rear = (q->rear + 1) % MAX_SIZE; + q->size++; + } + + /* 出队操作 */ + Vertex *dequeue(Queue *q) { + Vertex *vet = q->vertices[q->front]; + q->front = (q->front + 1) % MAX_SIZE; + q->size--; + return vet; + } + + /* 检查顶点是否已被访问 */ + int isVisited(Vertex **visited, int size, Vertex *vet) { + // 遍历查找节点,使用 O(n) 时间 + for (int i = 0; i < size; i++) { + if (visited[i] == vet) + return 1; + } + return 0; + } + + /* 广度优先遍历 BFS */ // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 - Vertex **graphBFS(GraphAdjList *t, Vertex *startVet) { - // 顶点遍历序列 - Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * t->size); - memset(res, 0, sizeof(Vertex *) * t->size); + void graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) { // 队列用于实现 BFS - Queue *que = newQueue(t->size); - // 哈希表,用于记录已被访问过的顶点 - HashTable *visited = newHash(t->size); - int resIndex = 0; - queuePush(que, startVet); // 将第一个元素入队 - hashMark(visited, startVet->pos); // 标记第一个入队的顶点 + Queue *queue = newQueue(); + enqueue(queue, startVet); + visited[(*visitedSize)++] = startVet; // 以顶点 vet 为起点,循环直至访问完所有顶点 - while (que->head < que->tail) { - // 遍历该顶点的边链表,将所有与该顶点有连接的,并且未被标记的顶点入队 - Node *n = queueTop(que)->list->head->next; - while (n != 0) { - // 查询哈希表,若该索引的顶点已入队,则跳过,否则入队并标记 - if (hashQuery(visited, n->val->pos) == 1) { - n = n->next; - continue; // 跳过已被访问过的顶点 + while (!isEmpty(queue)) { + Vertex *vet = dequeue(queue); // 队首顶点出队 + res[(*resSize)++] = vet; // 记录访问顶点 + // 遍历该顶点的所有邻接顶点 + AdjListNode *node = findNode(graph, vet); + while (node != NULL) { + // 跳过已被访问过的顶点 + if (!isVisited(visited, *visitedSize, node->vertex)) { + enqueue(queue, node->vertex); // 只入队未访问的顶点 + visited[(*visitedSize)++] = node->vertex; // 标记该顶点已被访问 } - queuePush(que, n->val); // 只入队未访问的顶点 - hashMark(visited, n->val->pos); // 标记该顶点已被访问 + node = node->next; } - // 队首元素存入数组 - res[resIndex] = queueTop(que); // 队首顶点加入顶点遍历序列 - resIndex++; - queuePop(que); // 队首元素出队 } // 释放内存 - freeQueue(que); - freeHash(visited); - resIndex = 0; - // 返回顶点遍历序列 - return res; + free(queue); } ``` @@ -749,39 +779,37 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这 === "C" ```c title="graph_dfs.c" + /* 检查顶点是否已被访问 */ + int isVisited(Vertex **res, int size, Vertex *vet) { + // 遍历查找节点,使用 O(n) 时间 + for (int i = 0; i < size; i++) { + if (res[i] == vet) { + return 1; + } + } + return 0; + } + /* 深度优先遍历 DFS 辅助函数 */ - int resIndex = 0; - void dfs(GraphAdjList *graph, HashTable *visited, Vertex *vet, Vertex **res) { - if (hashQuery(visited, vet->pos) == 1) { - return; // 跳过已被访问过的顶点 + void dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) { + // 记录访问顶点 + res[(*resSize)++] = vet; + // 遍历该顶点的所有邻接顶点 + AdjListNode *node = findNode(graph, vet); + while (node != NULL) { + // 跳过已被访问过的顶点 + if (!isVisited(res, *resSize, node->vertex)) { + // 递归访问邻接顶点 + dfs(graph, res, resSize, node->vertex); + } + node = node->next; } - hashMark(visited, vet->pos); // 标记顶点并将顶点存入数组 - res[resIndex] = vet; // 将顶点存入数组 - resIndex++; - // 遍历该顶点链表 - Node *n = vet->list->head->next; - while (n != 0) { - // 递归访问邻接顶点 - dfs(graph, visited, n->val, res); - n = n->next; - } - return; } /* 深度优先遍历 DFS */ // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 - Vertex **graphDFS(GraphAdjList *graph, Vertex *startVet) { - // 顶点遍历序列 - Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph->size); - memset(res, 0, sizeof(Vertex *) * graph->size); - // 哈希表,用于记录已被访问过的顶点 - HashTable *visited = newHash(graph->size); - dfs(graph, visited, startVet, res); - // 释放哈希表内存并将数组索引归零 - freeHash(visited); - resIndex = 0; - // 返回遍历数组 - return res; + void graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) { + dfs(graph, res, resSize, startVet); } ``` diff --git a/docs/chapter_hashing/hash_collision.md b/docs/chapter_hashing/hash_collision.md index dc8898f52..2441696b8 100644 --- a/docs/chapter_hashing/hash_collision.md +++ b/docs/chapter_hashing/hash_collision.md @@ -1164,7 +1164,7 @@ comments: true Node **buckets; // 桶数组 } HashMapChaining; - /* 构造方法 */ + /* 构造函数 */ HashMapChaining *initHashMapChaining() { HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining)); hashMap->size = 0; @@ -1178,7 +1178,7 @@ comments: true return hashMap; } - /* 析构方法 */ + /* 析构函数 */ void freeHashMapChaining(HashMapChaining *hashMap) { for (int i = 0; i < hashMap->capacity; i++) { Node *cur = hashMap->buckets[i]; @@ -2682,7 +2682,7 @@ comments: true Pair *TOMBSTONE; // 删除标记 } HashMapOpenAddressing; - /* 构造方法 */ + /* 构造函数 */ HashMapOpenAddressing *newHashMapOpenAddressing() { HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing)); hashMap->size = 0; @@ -2697,7 +2697,7 @@ comments: true return hashMap; } - /* 析构方法 */ + /* 析构函数 */ void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) { for (int i = 0; i < hashMap->capacity; i++) { Pair *pair = hashMap->buckets[i]; diff --git a/docs/chapter_heap/top_k.md b/docs/chapter_heap/top_k.md index 033ca4575..33dba431b 100644 --- a/docs/chapter_heap/top_k.md +++ b/docs/chapter_heap/top_k.md @@ -259,33 +259,49 @@ comments: true === "TS" ```typescript title="top_k.ts" - [class]{}-[func]{pushMinHeap} + /* 元素入堆 */ + function pushMinHeap(maxHeap: MaxHeap, val: number): void { + // 元素取反 + maxHeap.push(-val); + } - [class]{}-[func]{popMinHeap} + /* 元素出堆 */ + function popMinHeap(maxHeap: MaxHeap): number { + // 元素取反 + return -maxHeap.pop(); + } - [class]{}-[func]{peekMinHeap} + /* 访问堆顶元素 */ + function peekMinHeap(maxHeap: MaxHeap): number { + // 元素取反 + return -maxHeap.peek(); + } - [class]{}-[func]{getMinHeap} + /* 取出堆中元素 */ + function getMinHeap(maxHeap: MaxHeap): number[] { + // 元素取反 + return maxHeap.getMaxHeap().map((num: number) => -num); + } /* 基于堆查找数组中最大的 k 个元素 */ function topKHeap(nums: number[], k: number): number[] { - // 将堆中所有元素取反,从而用大顶堆来模拟小顶堆 - const invertedNums = nums.map((num) => -num); + // 初始化小顶堆 + // 请注意:我们将堆中所有元素取反,从而用大顶堆来模拟小顶堆 + const maxHeap = new MaxHeap([]); // 将数组的前 k 个元素入堆 - const heap = new MaxHeap(invertedNums.slice(0, k)); + for (let i = 0; i < k; i++) { + pushMinHeap(maxHeap, nums[i]); + } // 从第 k+1 个元素开始,保持堆的长度为 k - for (let i = k; i < invertedNums.length; i++) { - // 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆 - if (invertedNums[i] < heap.peek()) { - heap.pop(); - heap.push(invertedNums[i]); + for (let i = k; i < nums.length; i++) { + // 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆 + if (nums[i] > peekMinHeap(maxHeap)) { + popMinHeap(maxHeap); + pushMinHeap(maxHeap, nums[i]); } } - // 取出堆中元素 - const maxHeap = heap.getMaxHeap(); - // 对堆中元素取相反数 - const invertedMaxHeap = maxHeap.map((num) => -num); - return invertedMaxHeap; + // 返回堆中元素 + return getMinHeap(maxHeap); } ```