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