update zig codes for Section 'Heap' (heap.zig)

This commit is contained in:
sjinzh
2023-01-16 00:05:56 +08:00
parent d8289580a5
commit 73121c2cb3
3 changed files with 5 additions and 39 deletions

View File

@@ -18,13 +18,13 @@ fn testPush(comptime T: type, mem_allocator: std.mem.Allocator, heap_push: anyty
var heap = heap_push;
try heap.add(val); //元素入堆
std.debug.print("\n元素 {} 入堆后\n", .{val});
try inc.PrintUtil.printHeap(T, mem_allocator, heap, true);
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(); //堆顶元素出堆
std.debug.print("\n堆顶元素 {} 出堆后\n", .{val});
try inc.PrintUtil.printHeap(T, mem_allocator, heap_pop, true);
try inc.PrintUtil.printHeap(T, mem_allocator, heap_pop);
}
// Driver Code
@@ -73,10 +73,9 @@ pub fn main() !void {
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
// 输入列表并建堆
// 时间复杂度为 O(n) ,而非 O(nlogn)
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap, true);
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);
const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;