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

This commit is contained in:
sjinzh
2023-01-15 20:39:46 +08:00
parent bf907e1715
commit 5e17778f69
3 changed files with 64 additions and 24 deletions

View File

@ -59,13 +59,16 @@ pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHas
}
// print a heap (PriorityQueue)
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype) !void {
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype, queue_flag: bool) !void {
var arr = queue.items;
var len = queue.len;
std.debug.print("堆的数组表示:", .{});
printArray(T, arr[0..len]);
std.debug.print("\n堆的树状表示:\n", .{});
var root = try TreeUtil.arrToTree(T, mem_allocator, arr[0..len]);
var root = if (queue_flag)
try TreeUtil.arrQueToTree(T, mem_allocator, arr[0..len]) // through TailQueue
else
try TreeUtil.arrListToTree(T, mem_allocator, arr[0..len]); // through ArrayList to work as queue
try printTree(root, null, false);
}