upgrade zig codes to 0.11.0-dev.3379+629f0d23b (#563)

* upgrade zig codes to 0.11.0-dev.3379+629f0d23b

* upgrade zig codes to 0.11.0-dev.3379+629f0d23b
This commit is contained in:
sjinzh
2023-06-25 20:59:00 +08:00
committed by GitHub
parent 62e8f0df50
commit 41b7b229a3
22 changed files with 264 additions and 460 deletions

View File

@@ -20,7 +20,7 @@ pub fn BinarySearchTree(comptime T: type) type {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
std.sort.sort(T, nums, {}, comptime std.sort.asc(T)); // 排序数组
std.mem.sort(T, nums, {}, comptime std.sort.asc(T)); // 排序数组
self.root = try self.buildTree(nums, 0, nums.len - 1); // 构建二叉搜索树
}
@@ -98,7 +98,7 @@ pub fn BinarySearchTree(comptime T: type) type {
}
// 删除节点
fn remove(self: *Self, num: T) !void {
fn remove(self: *Self, num: T) void {
// 若树为空,直接提前返回
if (self.root == null) return;
var cur = self.root;
@@ -135,11 +135,11 @@ pub fn BinarySearchTree(comptime T: type) type {
while (tmp.?.left != null) {
tmp = tmp.?.left;
}
var tmpVal = tmp.?.val;
var tmp_val = tmp.?.val;
// 递归删除节点 tmp
_ = self.remove(tmp.?.val);
self.remove(tmp.?.val);
// 用 tmp 覆盖 cur
cur.?.val = tmpVal;
cur.?.val = tmp_val;
}
}
};
@@ -160,18 +160,18 @@ pub fn main() !void {
std.debug.print("\n查找到的节点对象为 {any},节点值 = {}\n", .{node, node.?.val});
// 插入节点
node = try bst.insert(16);
try bst.insert(16);
std.debug.print("\n插入节点 16 后,二叉树为\n", .{});
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
// 删除节点
_ = bst.remove(1);
bst.remove(1);
std.debug.print("\n删除节点 1 后,二叉树为\n", .{});
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
_ = bst.remove(2);
bst.remove(2);
std.debug.print("\n删除节点 2 后,二叉树为\n", .{});
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
_ = bst.remove(4);
bst.remove(4);
std.debug.print("\n删除节点 4 后,二叉树为\n", .{});
try inc.PrintUtil.printTree(bst.getRoot(), null, false);