build
This commit is contained in:
parent
49d39ff871
commit
76dcc6cbd3
@ -723,7 +723,7 @@ comments: true
|
|||||||
private int size = 0; // 列表长度(即当前元素数量)
|
private int size = 0; // 列表长度(即当前元素数量)
|
||||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
public MyList() {
|
public MyList() {
|
||||||
nums = new int[capacity];
|
nums = new int[capacity];
|
||||||
}
|
}
|
||||||
@ -827,12 +827,12 @@ comments: true
|
|||||||
int extendRatio = 2; // 每次列表扩容的倍数
|
int extendRatio = 2; // 每次列表扩容的倍数
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
MyList() {
|
MyList() {
|
||||||
nums = new int[numsCapacity];
|
nums = new int[numsCapacity];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 析构函数 */
|
/* 析构方法 */
|
||||||
~MyList() {
|
~MyList() {
|
||||||
delete[] nums;
|
delete[] nums;
|
||||||
}
|
}
|
||||||
@ -935,7 +935,7 @@ comments: true
|
|||||||
```python title="my_list.py"
|
```python title="my_list.py"
|
||||||
""" 列表类简易实现 """
|
""" 列表类简易实现 """
|
||||||
class MyList:
|
class MyList:
|
||||||
""" 构造函数 """
|
""" 构造方法 """
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__capacity = 10 # 列表容量
|
self.__capacity = 10 # 列表容量
|
||||||
self.__nums = [0] * self.__capacity # 数组(存储列表元素)
|
self.__nums = [0] * self.__capacity # 数组(存储列表元素)
|
||||||
@ -1012,7 +1012,7 @@ comments: true
|
|||||||
extendRatio int
|
extendRatio int
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
func newMyList() *myList {
|
func newMyList() *myList {
|
||||||
return &myList{
|
return &myList{
|
||||||
numsCapacity: 10, // 列表容量
|
numsCapacity: 10, // 列表容量
|
||||||
@ -1119,7 +1119,7 @@ comments: true
|
|||||||
#size = 0; // 列表长度(即当前元素数量)
|
#size = 0; // 列表长度(即当前元素数量)
|
||||||
#extendRatio = 2; // 每次列表扩容的倍数
|
#extendRatio = 2; // 每次列表扩容的倍数
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
constructor() {
|
constructor() {
|
||||||
this.#nums = new Array(this.#capacity);
|
this.#nums = new Array(this.#capacity);
|
||||||
}
|
}
|
||||||
@ -1225,7 +1225,7 @@ comments: true
|
|||||||
private _size: number = 0; // 列表长度(即当前元素数量)
|
private _size: number = 0; // 列表长度(即当前元素数量)
|
||||||
private extendRatio: number = 2; // 每次列表扩容的倍数
|
private extendRatio: number = 2; // 每次列表扩容的倍数
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
constructor() {
|
constructor() {
|
||||||
this.nums = new Array(this._capacity);
|
this.nums = new Array(this._capacity);
|
||||||
}
|
}
|
||||||
@ -1337,7 +1337,7 @@ comments: true
|
|||||||
private int numsSize = 0; // 列表长度(即当前元素数量)
|
private int numsSize = 0; // 列表长度(即当前元素数量)
|
||||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
public MyList()
|
public MyList()
|
||||||
{
|
{
|
||||||
nums = new int[numsCapacity];
|
nums = new int[numsCapacity];
|
||||||
@ -1451,7 +1451,7 @@ comments: true
|
|||||||
private var _size = 0 // 列表长度(即当前元素数量)
|
private var _size = 0 // 列表长度(即当前元素数量)
|
||||||
private let extendRatio = 2 // 每次列表扩容的倍数
|
private let extendRatio = 2 // 每次列表扩容的倍数
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
init() {
|
init() {
|
||||||
nums = Array(repeating: 0, count: _capacity)
|
nums = Array(repeating: 0, count: _capacity)
|
||||||
}
|
}
|
||||||
@ -1563,7 +1563,7 @@ comments: true
|
|||||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||||
|
|
||||||
// 构造函数(分配内存+初始化列表)
|
// 构造方法(分配内存+初始化列表)
|
||||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||||
if (self.mem_arena == null) {
|
if (self.mem_arena == null) {
|
||||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
@ -1573,7 +1573,7 @@ comments: true
|
|||||||
std.mem.set(T, self.nums, @as(T, 0));
|
std.mem.set(T, self.nums, @as(T, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 析构函数(释放内存)
|
// 析构方法(释放内存)
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.mem_arena == null) return;
|
if (self.mem_arena == null) return;
|
||||||
self.mem_arena.?.deinit();
|
self.mem_arena.?.deinit();
|
||||||
|
|||||||
@ -40,7 +40,7 @@ comments: true
|
|||||||
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||||
List<List<Integer>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
List<List<Integer>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
public GraphAdjMat(int[] vertices, int[][] edges) {
|
public GraphAdjMat(int[] vertices, int[][] edges) {
|
||||||
this.vertices = new ArrayList<>();
|
this.vertices = new ArrayList<>();
|
||||||
this.adjMat = new ArrayList<>();
|
this.adjMat = new ArrayList<>();
|
||||||
@ -125,13 +125,13 @@ comments: true
|
|||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
||||||
```cpp title="graph_adjacency_matrix.cpp"
|
```cpp title="graph_adjacency_matrix.cpp"
|
||||||
|
[class]{GraphAdjMat}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Python"
|
=== "Python"
|
||||||
|
|
||||||
```python title="graph_adjacency_matrix.py"
|
```python title="graph_adjacency_matrix.py"
|
||||||
|
[class]{GraphAdjMat}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Go"
|
=== "Go"
|
||||||
@ -145,6 +145,7 @@ comments: true
|
|||||||
adjMat [][]int
|
adjMat [][]int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 构造方法 */
|
||||||
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
|
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
|
||||||
// 添加顶点
|
// 添加顶点
|
||||||
n := len(vertices)
|
n := len(vertices)
|
||||||
@ -221,24 +222,33 @@ comments: true
|
|||||||
g.adjMat[i][j] = 0
|
g.adjMat[i][j] = 0
|
||||||
g.adjMat[j][i] = 0
|
g.adjMat[j][i] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 打印邻接矩阵 */
|
||||||
|
func (g *graphAdjMat) print() {
|
||||||
|
fmt.Printf("\t顶点列表 = %v\n", g.vertices)
|
||||||
|
fmt.Printf("\t邻接矩阵 = \n")
|
||||||
|
for i := range g.adjMat {
|
||||||
|
fmt.Printf("\t\t\t%v\n", g.adjMat[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="graph_adjacency_matrix.js"
|
```javascript title="graph_adjacency_matrix.js"
|
||||||
|
[class]{GraphAdjMat}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="graph_adjacency_matrix.ts"
|
```typescript title="graph_adjacency_matrix.ts"
|
||||||
|
[class]{GraphAdjMat}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
|
||||||
```c title="graph_adjacency_matrix.c"
|
```c title="graph_adjacency_matrix.c"
|
||||||
|
[class]{graphAdjMat}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C#"
|
=== "C#"
|
||||||
@ -255,7 +265,7 @@ comments: true
|
|||||||
private var vertices: [Int] // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
private var vertices: [Int] // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||||
private var adjMat: [[Int]] // 邻接矩阵,行列索引对应“顶点索引”
|
private var adjMat: [[Int]] // 邻接矩阵,行列索引对应“顶点索引”
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
init(vertices: [Int], edges: [[Int]]) {
|
init(vertices: [Int], edges: [[Int]]) {
|
||||||
self.vertices = []
|
self.vertices = []
|
||||||
adjMat = []
|
adjMat = []
|
||||||
@ -326,6 +336,14 @@ comments: true
|
|||||||
adjMat[i][j] = 0
|
adjMat[i][j] = 0
|
||||||
adjMat[j][i] = 0
|
adjMat[j][i] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 打印邻接矩阵 */
|
||||||
|
func print() {
|
||||||
|
Swift.print("顶点列表 = ", terminator: "")
|
||||||
|
Swift.print(vertices)
|
||||||
|
Swift.print("邻接矩阵 =")
|
||||||
|
PrintUtil.printMatrix(matrix: adjMat)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -378,7 +396,7 @@ comments: true
|
|||||||
// 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
|
// 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
|
||||||
Map<Vertex, Set<Vertex>> adjList; // 邻接表(使用哈希表实现)
|
Map<Vertex, Set<Vertex>> adjList; // 邻接表(使用哈希表实现)
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
public GraphAdjList(Vertex[][] edges) {
|
public GraphAdjList(Vertex[][] edges) {
|
||||||
this.adjList = new HashMap<>();
|
this.adjList = new HashMap<>();
|
||||||
// 添加所有顶点和边
|
// 添加所有顶点和边
|
||||||
@ -448,13 +466,17 @@ comments: true
|
|||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
||||||
```cpp title="graph_adjacency_list.cpp"
|
```cpp title="graph_adjacency_list.cpp"
|
||||||
|
[class]{Vertex}-[func]{}
|
||||||
|
|
||||||
|
[class]{GraphAdjList}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Python"
|
=== "Python"
|
||||||
|
|
||||||
```python title="graph_adjacency_list.py"
|
```python title="graph_adjacency_list.py"
|
||||||
|
[class]{Vertex}-[func]{}
|
||||||
|
|
||||||
|
[class]{GraphAdjList}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Go"
|
=== "Go"
|
||||||
@ -465,6 +487,7 @@ comments: true
|
|||||||
val int
|
val int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 构造方法 */
|
||||||
func newVertex(val int) vertex {
|
func newVertex(val int) vertex {
|
||||||
return vertex{
|
return vertex{
|
||||||
val: val,
|
val: val,
|
||||||
@ -478,7 +501,7 @@ comments: true
|
|||||||
adjList map[vertex]map[vertex]struct{}
|
adjList map[vertex]map[vertex]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 构造函数 */
|
/* 构造方法 */
|
||||||
func newGraphAdjList(edges [][]vertex) *graphAdjList {
|
func newGraphAdjList(edges [][]vertex) *graphAdjList {
|
||||||
g := &graphAdjList{
|
g := &graphAdjList{
|
||||||
adjList: make(map[vertex]map[vertex]struct{}),
|
adjList: make(map[vertex]map[vertex]struct{}),
|
||||||
@ -545,24 +568,196 @@ comments: true
|
|||||||
delete(set, vet)
|
delete(set, vet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 打印邻接表 */
|
||||||
|
func (g *graphAdjList) print() {
|
||||||
|
var builder strings.Builder
|
||||||
|
fmt.Printf("邻接表 = \n")
|
||||||
|
for k, v := range g.adjList {
|
||||||
|
builder.WriteString("\t\t" + strconv.Itoa(k.val) + ": ")
|
||||||
|
for vet := range v {
|
||||||
|
builder.WriteString(strconv.Itoa(vet.val) + " ")
|
||||||
|
}
|
||||||
|
fmt.Println(builder.String())
|
||||||
|
builder.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="graph_adjacency_list.js"
|
```javascript title="graph_adjacency_list.js"
|
||||||
|
/* 顶点类 */
|
||||||
|
class Vertex {
|
||||||
|
val;
|
||||||
|
constructor(val) {
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 基于邻接表实现的无向图类 */
|
||||||
|
class GraphAdjList {
|
||||||
|
adjList;
|
||||||
|
/* 构造方法 */
|
||||||
|
constructor(edges) {
|
||||||
|
this.adjList = new Map();
|
||||||
|
// 添加所有顶点和边
|
||||||
|
for (const edge of edges) {
|
||||||
|
this.addVertex(edge[0]);
|
||||||
|
this.addVertex(edge[1]);
|
||||||
|
this.addEdge(edge[0], edge[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 获取顶点数量 */
|
||||||
|
size() {
|
||||||
|
return this.adjList.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加边 */
|
||||||
|
addEdge(vet1, vet2) {
|
||||||
|
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||||
|
throw new Error("Illegal Argument Exception");
|
||||||
|
}
|
||||||
|
// 添加边 vet1 - vet2
|
||||||
|
this.adjList.get(vet1).add(vet2);
|
||||||
|
this.adjList.get(vet2).add(vet1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除边 */
|
||||||
|
removeEdge(vet1, vet2) {
|
||||||
|
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||||
|
throw new Error("Illegal Argument Exception");
|
||||||
|
}
|
||||||
|
// 删除边 vet1 - vet2
|
||||||
|
this.adjList.get(vet1).delete(vet2);
|
||||||
|
this.adjList.get(vet2).delete(vet1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加顶点 */
|
||||||
|
addVertex(vet) {
|
||||||
|
if (this.adjList.has(vet)) return;
|
||||||
|
// 在邻接表中添加一个新链表(即 HashSet)
|
||||||
|
this.adjList.set(vet, new Set());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除顶点 */
|
||||||
|
removeVertex(vet) {
|
||||||
|
if (!this.adjList.has(vet)) {
|
||||||
|
throw new Error("Illegal Argument Exception");
|
||||||
|
}
|
||||||
|
// 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
|
||||||
|
this.adjList.delete(vet);
|
||||||
|
// 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
|
||||||
|
for (let set of this.adjList.values()) {
|
||||||
|
set.delete(vet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打印邻接表 */
|
||||||
|
print() {
|
||||||
|
console.log("邻接表 =");
|
||||||
|
for (const [key, value] of this.adjList) {
|
||||||
|
const tmp = [];
|
||||||
|
for (const vertex of value){
|
||||||
|
tmp.push(vertex.val);
|
||||||
|
}
|
||||||
|
console.log(key.val + ": " + tmp + ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="graph_adjacency_list.ts"
|
```typescript title="graph_adjacency_list.ts"
|
||||||
|
/* 顶点类 */
|
||||||
|
class Vertex {
|
||||||
|
val: number;
|
||||||
|
constructor(val: number) {
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 基于邻接表实现的无向图类 */
|
||||||
|
class GraphAdjList {
|
||||||
|
adjList: Map<Vertex, Set<Vertex>>;
|
||||||
|
/* 构造方法 */
|
||||||
|
constructor(edges: Vertex[][]) {
|
||||||
|
this.adjList = new Map();
|
||||||
|
// 添加所有顶点和边
|
||||||
|
for (const edge of edges) {
|
||||||
|
this.addVertex(edge[0]);
|
||||||
|
this.addVertex(edge[1]);
|
||||||
|
this.addEdge(edge[0], edge[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 获取顶点数量 */
|
||||||
|
size(): number {
|
||||||
|
return this.adjList.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加边 */
|
||||||
|
addEdge(vet1: Vertex, vet2: Vertex): void {
|
||||||
|
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||||
|
throw new Error("Illegal Argument Exception");
|
||||||
|
}
|
||||||
|
// 添加边 vet1 - vet2
|
||||||
|
this.adjList.get(vet1).add(vet2);
|
||||||
|
this.adjList.get(vet2).add(vet1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除边 */
|
||||||
|
removeEdge(vet1: Vertex, vet2: Vertex): void {
|
||||||
|
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||||
|
throw new Error("Illegal Argument Exception");
|
||||||
|
}
|
||||||
|
// 删除边 vet1 - vet2
|
||||||
|
this.adjList.get(vet1).delete(vet2);
|
||||||
|
this.adjList.get(vet2).delete(vet1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加顶点 */
|
||||||
|
addVertex(vet: Vertex): void {
|
||||||
|
if (this.adjList.has(vet)) return;
|
||||||
|
// 在邻接表中添加一个新链表(即 HashSet)
|
||||||
|
this.adjList.set(vet, new Set());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除顶点 */
|
||||||
|
removeVertex(vet: Vertex): void {
|
||||||
|
if (!this.adjList.has(vet)) {
|
||||||
|
throw new Error("Illegal Argument Exception");
|
||||||
|
}
|
||||||
|
// 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
|
||||||
|
this.adjList.delete(vet);
|
||||||
|
// 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
|
||||||
|
for (let set of this.adjList.values()) {
|
||||||
|
set.delete(vet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打印邻接表 */
|
||||||
|
print(): void {
|
||||||
|
console.log("邻接表 =");
|
||||||
|
for (const [key, value] of this.adjList.entries()) {
|
||||||
|
const tmp = [];
|
||||||
|
for (const vertex of value){
|
||||||
|
tmp.push(vertex.val);
|
||||||
|
}
|
||||||
|
console.log(key.val + ": " + tmp + ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
|
||||||
```c title="graph_adjacency_list.c"
|
```c title="graph_adjacency_list.c"
|
||||||
|
[class]{vertex}-[func]{}
|
||||||
|
|
||||||
|
[class]{graphAdjList}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C#"
|
=== "C#"
|
||||||
@ -576,22 +771,7 @@ comments: true
|
|||||||
=== "Swift"
|
=== "Swift"
|
||||||
|
|
||||||
```swift title="graph_adjacency_list.swift"
|
```swift title="graph_adjacency_list.swift"
|
||||||
/* 顶点类 */
|
[class]{Vertex}-[func]{}
|
||||||
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 {
|
class GraphAdjList {
|
||||||
@ -654,13 +834,27 @@ comments: true
|
|||||||
adjList[key]?.remove(vet)
|
adjList[key]?.remove(vet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 打印邻接表 */
|
||||||
|
func print() {
|
||||||
|
Swift.print("邻接表 =")
|
||||||
|
for entry in adjList {
|
||||||
|
var tmp: [Int] = []
|
||||||
|
for vertex in entry.value {
|
||||||
|
tmp.append(vertex.val)
|
||||||
|
}
|
||||||
|
Swift.print("\(entry.key.val): \(tmp),")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
|
||||||
```zig title="graph_adjacency_list.zig"
|
```zig title="graph_adjacency_list.zig"
|
||||||
|
[class]{Vertex}-[func]{}
|
||||||
|
|
||||||
|
[class]{GraphAdjList}-[func]{}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 9.2.3. 效率对比
|
## 9.2.3. 效率对比
|
||||||
|
|||||||
@ -1179,7 +1179,7 @@ $$
|
|||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
// 构造函数
|
// 构造方法
|
||||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||||
self.mem_allocator = allocator;
|
self.mem_allocator = allocator;
|
||||||
// 初始化一个长度为 100 的桶(数组)
|
// 初始化一个长度为 100 的桶(数组)
|
||||||
@ -1190,7 +1190,7 @@ $$
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 析构函数
|
// 析构方法
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.bucket != null) self.bucket.?.deinit();
|
if (self.bucket != null) self.bucket.?.deinit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1079,7 +1079,7 @@ comments: true
|
|||||||
=== "Java"
|
=== "Java"
|
||||||
|
|
||||||
```java title="my_heap.java"
|
```java title="my_heap.java"
|
||||||
/* 构造函数,根据输入列表建堆 */
|
/* 构造方法,根据输入列表建堆 */
|
||||||
MaxHeap(List<Integer> nums) {
|
MaxHeap(List<Integer> nums) {
|
||||||
// 将列表元素原封不动添加进堆
|
// 将列表元素原封不动添加进堆
|
||||||
maxHeap = new ArrayList<>(nums);
|
maxHeap = new ArrayList<>(nums);
|
||||||
@ -1093,7 +1093,7 @@ comments: true
|
|||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
||||||
```cpp title="my_heap.cpp"
|
```cpp title="my_heap.cpp"
|
||||||
/* 构造函数,根据输入列表建堆 */
|
/* 构造方法,根据输入列表建堆 */
|
||||||
MaxHeap(vector<int> nums) {
|
MaxHeap(vector<int> nums) {
|
||||||
// 将列表元素原封不动添加进堆
|
// 将列表元素原封不动添加进堆
|
||||||
maxHeap = nums;
|
maxHeap = nums;
|
||||||
@ -1113,7 +1113,7 @@ comments: true
|
|||||||
=== "Go"
|
=== "Go"
|
||||||
|
|
||||||
```go title="my_heap.go"
|
```go title="my_heap.go"
|
||||||
/* 构造函数,根据切片建堆 */
|
/* 构造方法,根据切片建堆 */
|
||||||
func newMaxHeap(nums []any) *maxHeap {
|
func newMaxHeap(nums []any) *maxHeap {
|
||||||
// 将列表元素原封不动添加进堆
|
// 将列表元素原封不动添加进堆
|
||||||
h := &maxHeap{data: nums}
|
h := &maxHeap{data: nums}
|
||||||
@ -1128,7 +1128,7 @@ comments: true
|
|||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="my_heap.js"
|
```javascript title="my_heap.js"
|
||||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
/* 构造方法,建立空堆或根据输入列表建堆 */
|
||||||
constructor(nums) {
|
constructor(nums) {
|
||||||
// 将列表元素原封不动添加进堆
|
// 将列表元素原封不动添加进堆
|
||||||
this.#maxHeap = nums === undefined ? [] : [...nums];
|
this.#maxHeap = nums === undefined ? [] : [...nums];
|
||||||
@ -1142,7 +1142,7 @@ comments: true
|
|||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="my_heap.ts"
|
```typescript title="my_heap.ts"
|
||||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
/* 构造方法,建立空堆或根据输入列表建堆 */
|
||||||
constructor(nums?: number[]) {
|
constructor(nums?: number[]) {
|
||||||
// 将列表元素原封不动添加进堆
|
// 将列表元素原封不动添加进堆
|
||||||
this.maxHeap = nums === undefined ? [] : [...nums];
|
this.maxHeap = nums === undefined ? [] : [...nums];
|
||||||
@ -1168,7 +1168,7 @@ comments: true
|
|||||||
=== "Swift"
|
=== "Swift"
|
||||||
|
|
||||||
```swift title="my_heap.swift"
|
```swift title="my_heap.swift"
|
||||||
/* 构造函数,根据输入列表建堆 */
|
/* 构造方法,根据输入列表建堆 */
|
||||||
init(nums: [Int]) {
|
init(nums: [Int]) {
|
||||||
// 将列表元素原封不动添加进堆
|
// 将列表元素原封不动添加进堆
|
||||||
maxHeap = nums
|
maxHeap = nums
|
||||||
@ -1182,7 +1182,7 @@ comments: true
|
|||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
|
||||||
```zig title="my_heap.zig"
|
```zig title="my_heap.zig"
|
||||||
// 构造函数,根据输入列表建堆
|
// 构造方法,根据输入列表建堆
|
||||||
fn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {
|
fn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {
|
||||||
if (self.maxHeap != null) return;
|
if (self.maxHeap != null) return;
|
||||||
self.maxHeap = std.ArrayList(T).init(allocator);
|
self.maxHeap = std.ArrayList(T).init(allocator);
|
||||||
|
|||||||
@ -853,7 +853,7 @@ comments: true
|
|||||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||||
|
|
||||||
// 构造函数(分配内存+初始化队列)
|
// 构造方法(分配内存+初始化队列)
|
||||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||||
if (self.mem_arena == null) {
|
if (self.mem_arena == null) {
|
||||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
@ -864,7 +864,7 @@ comments: true
|
|||||||
self.queSize = 0;
|
self.queSize = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 析构函数(释放内存)
|
// 析构方法(释放内存)
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.mem_arena == null) return;
|
if (self.mem_arena == null) return;
|
||||||
self.mem_arena.?.deinit();
|
self.mem_arena.?.deinit();
|
||||||
@ -1540,7 +1540,7 @@ comments: true
|
|||||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||||
|
|
||||||
// 构造函数(分配内存+初始化数组)
|
// 构造方法(分配内存+初始化数组)
|
||||||
pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {
|
pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {
|
||||||
if (self.mem_arena == null) {
|
if (self.mem_arena == null) {
|
||||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
@ -1551,7 +1551,7 @@ comments: true
|
|||||||
std.mem.set(T, self.nums, @as(T, 0));
|
std.mem.set(T, self.nums, @as(T, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 析构函数(释放内存)
|
// 析构方法(释放内存)
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.mem_arena == null) return;
|
if (self.mem_arena == null) return;
|
||||||
self.mem_arena.?.deinit();
|
self.mem_arena.?.deinit();
|
||||||
|
|||||||
@ -781,7 +781,7 @@ comments: true
|
|||||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||||
|
|
||||||
// 构造函数(分配内存+初始化栈)
|
// 构造方法(分配内存+初始化栈)
|
||||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||||
if (self.mem_arena == null) {
|
if (self.mem_arena == null) {
|
||||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
@ -791,7 +791,7 @@ comments: true
|
|||||||
self.stkSize = 0;
|
self.stkSize = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 析构函数(释放内存)
|
// 析构方法(释放内存)
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.mem_arena == null) return;
|
if (self.mem_arena == null) return;
|
||||||
self.mem_arena.?.deinit();
|
self.mem_arena.?.deinit();
|
||||||
@ -1257,14 +1257,14 @@ comments: true
|
|||||||
|
|
||||||
stack: ?std.ArrayList(T) = null,
|
stack: ?std.ArrayList(T) = null,
|
||||||
|
|
||||||
// 构造函数(分配内存+初始化栈)
|
// 构造方法(分配内存+初始化栈)
|
||||||
pub fn init(self: *Self, allocator: std.mem.Allocator) void {
|
pub fn init(self: *Self, allocator: std.mem.Allocator) void {
|
||||||
if (self.stack == null) {
|
if (self.stack == null) {
|
||||||
self.stack = std.ArrayList(T).init(allocator);
|
self.stack = std.ArrayList(T).init(allocator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 析构函数(释放内存)
|
// 析构方法(释放内存)
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.stack == null) return;
|
if (self.stack == null) return;
|
||||||
self.stack.?.deinit();
|
self.stack.?.deinit();
|
||||||
|
|||||||
@ -1187,7 +1187,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入结点(辅助函数) */
|
/* 递归插入结点(辅助方法) */
|
||||||
TreeNode insertHelper(TreeNode node, int val) {
|
TreeNode insertHelper(TreeNode node, int val) {
|
||||||
if (node == null) return new TreeNode(val);
|
if (node == null) return new TreeNode(val);
|
||||||
/* 1. 查找插入位置,并插入结点 */
|
/* 1. 查找插入位置,并插入结点 */
|
||||||
@ -1214,7 +1214,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入结点(辅助函数) */
|
/* 递归插入结点(辅助方法) */
|
||||||
TreeNode* insertHelper(TreeNode* node, int val) {
|
TreeNode* insertHelper(TreeNode* node, int val) {
|
||||||
if (node == nullptr) return new TreeNode(val);
|
if (node == nullptr) return new TreeNode(val);
|
||||||
/* 1. 查找插入位置,并插入结点 */
|
/* 1. 查找插入位置,并插入结点 */
|
||||||
@ -1240,7 +1240,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
self.root = self.__insert_helper(self.root, val)
|
self.root = self.__insert_helper(self.root, val)
|
||||||
return self.root
|
return self.root
|
||||||
|
|
||||||
""" 递归插入结点(辅助函数)"""
|
""" 递归插入结点(辅助方法)"""
|
||||||
def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode:
|
def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode:
|
||||||
if node is None:
|
if node is None:
|
||||||
return TreeNode(val)
|
return TreeNode(val)
|
||||||
@ -1267,7 +1267,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return t.root
|
return t.root
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入结点(辅助函数) */
|
/* 递归插入结点(辅助方法) */
|
||||||
func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
|
func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return NewTreeNode(val)
|
return NewTreeNode(val)
|
||||||
@ -1299,7 +1299,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return this.root;
|
return this.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入结点(辅助函数) */
|
/* 递归插入结点(辅助方法) */
|
||||||
insertHelper(node, val) {
|
insertHelper(node, val) {
|
||||||
if (node === null) return new TreeNode(val);
|
if (node === null) return new TreeNode(val);
|
||||||
/* 1. 查找插入位置,并插入结点 */
|
/* 1. 查找插入位置,并插入结点 */
|
||||||
@ -1323,7 +1323,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return this.root;
|
return this.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入结点(辅助函数) */
|
/* 递归插入结点(辅助方法) */
|
||||||
insertHelper(node: TreeNode, val: number): TreeNode {
|
insertHelper(node: TreeNode, val: number): TreeNode {
|
||||||
if (node === null) return new TreeNode(val);
|
if (node === null) return new TreeNode(val);
|
||||||
/* 1. 查找插入位置,并插入结点 */
|
/* 1. 查找插入位置,并插入结点 */
|
||||||
@ -1360,7 +1360,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入结点(辅助函数) */
|
/* 递归插入结点(辅助方法) */
|
||||||
TreeNode? insertHelper(TreeNode? node, int val)
|
TreeNode? insertHelper(TreeNode? node, int val)
|
||||||
{
|
{
|
||||||
if (node == null) return new TreeNode(val);
|
if (node == null) return new TreeNode(val);
|
||||||
@ -1389,7 +1389,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入结点(辅助函数) */
|
/* 递归插入结点(辅助方法) */
|
||||||
func insertHelper(node: TreeNode?, val: Int) -> TreeNode? {
|
func insertHelper(node: TreeNode?, val: Int) -> TreeNode? {
|
||||||
var node = node
|
var node = node
|
||||||
if node == nil {
|
if node == nil {
|
||||||
@ -1420,7 +1420,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return self.root;
|
return self.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 递归插入结点(辅助函数)
|
// 递归插入结点(辅助方法)
|
||||||
fn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {
|
fn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {
|
||||||
var node = node_;
|
var node = node_;
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
@ -1457,7 +1457,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除结点(辅助函数) */
|
/* 递归删除结点(辅助方法) */
|
||||||
TreeNode removeHelper(TreeNode node, int val) {
|
TreeNode removeHelper(TreeNode node, int val) {
|
||||||
if (node == null) return null;
|
if (node == null) return null;
|
||||||
/* 1. 查找结点,并删除之 */
|
/* 1. 查找结点,并删除之 */
|
||||||
@ -1508,7 +1508,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除结点(辅助函数) */
|
/* 递归删除结点(辅助方法) */
|
||||||
TreeNode* removeHelper(TreeNode* node, int val) {
|
TreeNode* removeHelper(TreeNode* node, int val) {
|
||||||
if (node == nullptr) return nullptr;
|
if (node == nullptr) return nullptr;
|
||||||
/* 1. 查找结点,并删除之 */
|
/* 1. 查找结点,并删除之 */
|
||||||
@ -1562,7 +1562,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
root = self.__remove_helper(self.root, val)
|
root = self.__remove_helper(self.root, val)
|
||||||
return root
|
return root
|
||||||
|
|
||||||
""" 递归删除结点(辅助函数) """
|
""" 递归删除结点(辅助方法) """
|
||||||
def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||||
if node is None:
|
if node is None:
|
||||||
return None
|
return None
|
||||||
@ -1608,7 +1608,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除结点(辅助函数) */
|
/* 递归删除结点(辅助方法) */
|
||||||
func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
|
func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return nil
|
return nil
|
||||||
@ -1668,7 +1668,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return this.root;
|
return this.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除结点(辅助函数) */
|
/* 递归删除结点(辅助方法) */
|
||||||
removeHelper(node, val) {
|
removeHelper(node, val) {
|
||||||
if (node === null) return null;
|
if (node === null) return null;
|
||||||
/* 1. 查找结点,并删除之 */
|
/* 1. 查找结点,并删除之 */
|
||||||
@ -1715,7 +1715,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return this.root;
|
return this.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除结点(辅助函数) */
|
/* 递归删除结点(辅助方法) */
|
||||||
removeHelper(node: TreeNode, val: number): TreeNode {
|
removeHelper(node: TreeNode, val: number): TreeNode {
|
||||||
if (node === null) return null;
|
if (node === null) return null;
|
||||||
/* 1. 查找结点,并删除之 */
|
/* 1. 查找结点,并删除之 */
|
||||||
@ -1774,7 +1774,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除结点(辅助函数) */
|
/* 递归删除结点(辅助方法) */
|
||||||
TreeNode? removeHelper(TreeNode? node, int val)
|
TreeNode? removeHelper(TreeNode? node, int val)
|
||||||
{
|
{
|
||||||
if (node == null) return null;
|
if (node == null) return null;
|
||||||
@ -1833,7 +1833,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除结点(辅助函数) */
|
/* 递归删除结点(辅助方法) */
|
||||||
func removeHelper(node: TreeNode?, val: Int) -> TreeNode? {
|
func removeHelper(node: TreeNode?, val: Int) -> TreeNode? {
|
||||||
var node = node
|
var node = node
|
||||||
if node == nil {
|
if node == nil {
|
||||||
@ -1892,7 +1892,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||||||
return self.root;
|
return self.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 递归删除结点(辅助函数)
|
// 递归删除结点(辅助方法)
|
||||||
fn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {
|
fn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {
|
||||||
var node = node_;
|
var node = node_;
|
||||||
if (node == null) return null;
|
if (node == null) return null;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user