From 58d5c117f8ee0067e0709e25241e5443afbadc49 Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Mon, 16 Jan 2023 05:30:46 +0800 Subject: [PATCH] update zig codes for Section 'Heap' (heap.zig) --- codes/zig/chapter_heap/heap.zig | 9 ++++----- codes/zig/include/PrintUtil.zig | 2 +- codes/zig/include/TreeNode.zig | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/codes/zig/chapter_heap/heap.zig b/codes/zig/chapter_heap/heap.zig index a375cf3cc..e2f215d0b 100644 --- a/codes/zig/chapter_heap/heap.zig +++ b/codes/zig/chapter_heap/heap.zig @@ -14,17 +14,16 @@ fn greaterThan(context: void, a: i32, b: i32) std.math.Order { return lessThan(context, a, b).invert(); } -fn testPush(comptime T: type, mem_allocator: std.mem.Allocator, heap_push: anytype, val: T) !void { - var heap = heap_push; +fn testPush(comptime T: type, mem_allocator: std.mem.Allocator, heap: anytype, val: T) !void { try heap.add(val); //元素入堆 std.debug.print("\n元素 {} 入堆后\n", .{val}); try inc.PrintUtil.printHeap(T, mem_allocator, heap); } -fn testPop(comptime T: type, mem_allocator: std.mem.Allocator, heap_pop: anytype) !void { - var val = heap_pop.remove(); //堆顶元素出堆 +fn testPop(comptime T: type, mem_allocator: std.mem.Allocator, heap: anytype) !void { + var val = heap.remove(); //堆顶元素出堆 std.debug.print("\n堆顶元素 {} 出堆后\n", .{val}); - try inc.PrintUtil.printHeap(T, mem_allocator, heap_pop); + try inc.PrintUtil.printHeap(T, mem_allocator, heap); } // Driver Code diff --git a/codes/zig/include/PrintUtil.zig b/codes/zig/include/PrintUtil.zig index 6e46c0dee..6cfeaa6ad 100644 --- a/codes/zig/include/PrintUtil.zig +++ b/codes/zig/include/PrintUtil.zig @@ -65,7 +65,7 @@ pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anyt std.debug.print("堆的数组表示:", .{}); printArray(T, arr[0..len]); std.debug.print("\n堆的树状表示:\n", .{}); - var root = try TreeUtil.arrQueToTree(T, mem_allocator, arr[0..len]); // through TailQueue + var root = try TreeUtil.arrToTree(T, mem_allocator, arr[0..len]); // through TailQueue try printTree(root, null, false); } diff --git a/codes/zig/include/TreeNode.zig b/codes/zig/include/TreeNode.zig index 173421864..0e4f5b151 100644 --- a/codes/zig/include/TreeNode.zig +++ b/codes/zig/include/TreeNode.zig @@ -23,8 +23,8 @@ pub fn TreeNode(comptime T: type) type { }; } -// Generate a binary tree with an array (through TailQueue) -pub fn arrQueToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) { +// Generate a binary tree with an array +pub fn arrToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) { if (arr.len == 0) return null; var root = try mem_allocator.create(TreeNode(T)); root.init(arr[0]);