2023-01-09 19:31:45 +08:00
|
|
|
// File: PrintUtil.zig
|
2023-01-08 20:38:48 +08:00
|
|
|
// Created Time: 2023-01-07
|
2024-01-02 01:25:37 +08:00
|
|
|
// Author: codingonion (coderonion@gmail.com)
|
2023-01-08 20:38:48 +08:00
|
|
|
|
|
|
|
const std = @import("std");
|
2023-01-14 08:11:54 +08:00
|
|
|
pub const ListUtil = @import("ListNode.zig");
|
|
|
|
pub const ListNode = ListUtil.ListNode;
|
|
|
|
pub const TreeUtil = @import("TreeNode.zig");
|
|
|
|
pub const TreeNode = TreeUtil.TreeNode;
|
2023-01-08 20:38:48 +08:00
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印数组
|
2023-01-08 20:38:48 +08:00
|
|
|
pub fn printArray(comptime T: type, nums: []T) void {
|
|
|
|
std.debug.print("[", .{});
|
2023-01-09 19:31:45 +08:00
|
|
|
if (nums.len > 0) {
|
2023-06-25 20:59:00 +08:00
|
|
|
for (nums, 0..) |num, j| {
|
2023-01-11 17:39:28 +08:00
|
|
|
std.debug.print("{}{s}", .{num, if (j == nums.len-1) "]" else ", " });
|
2023-01-09 19:31:45 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std.debug.print("]", .{});
|
2023-01-11 17:39:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印列表
|
2023-01-11 17:39:28 +08:00
|
|
|
pub fn printList(comptime T: type, list: std.ArrayList(T)) void {
|
|
|
|
std.debug.print("[", .{});
|
|
|
|
if (list.items.len > 0) {
|
2023-06-25 20:59:00 +08:00
|
|
|
for (list.items, 0..) |value, i| {
|
2023-01-11 17:39:28 +08:00
|
|
|
std.debug.print("{}{s}", .{value, if (i == list.items.len-1) "]" else ", " });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std.debug.print("]", .{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印链表
|
2023-01-11 17:39:28 +08:00
|
|
|
pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void {
|
|
|
|
if (node == null) return;
|
2023-06-25 20:59:00 +08:00
|
|
|
var list = std.ArrayList(T).init(std.heap.page_allocator);
|
2023-01-11 17:39:28 +08:00
|
|
|
defer list.deinit();
|
|
|
|
var head = node;
|
|
|
|
while (head != null) {
|
|
|
|
try list.append(head.?.val);
|
|
|
|
head = head.?.next;
|
|
|
|
}
|
2023-06-25 20:59:00 +08:00
|
|
|
for (list.items, 0..) |value, i| {
|
2023-01-11 17:39:28 +08:00
|
|
|
std.debug.print("{}{s}", .{value, if (i == list.items.len-1) "\n" else "->" });
|
2023-01-09 19:31:45 +08:00
|
|
|
}
|
2023-01-08 20:38:48 +08:00
|
|
|
}
|
2023-01-09 19:31:45 +08:00
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印队列
|
2023-01-21 01:22:13 +08:00
|
|
|
pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void {
|
|
|
|
var node = queue.first;
|
|
|
|
std.debug.print("[", .{});
|
|
|
|
var i: i32 = 0;
|
|
|
|
while (node != null) : (i += 1) {
|
|
|
|
var data = node.?.data;
|
|
|
|
std.debug.print("{}{s}", .{data, if (i == queue.len - 1) "]" else ", " });
|
|
|
|
node = node.?.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印哈希表
|
2023-01-13 20:17:44 +08:00
|
|
|
pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void {
|
|
|
|
var it = map.iterator();
|
|
|
|
while (it.next()) |kv| {
|
|
|
|
var key = kv.key_ptr.*;
|
|
|
|
var value = kv.value_ptr.*;
|
|
|
|
std.debug.print("{} -> {s}\n", .{key, value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印堆
|
2023-01-14 08:11:54 +08:00
|
|
|
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype) !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]);
|
|
|
|
try printTree(root, null, false);
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印二叉树
|
2023-01-09 19:31:45 +08:00
|
|
|
// This tree printer is borrowed from TECHIE DELIGHT
|
|
|
|
// https://www.techiedelight.com/c-program-print-binary-tree/
|
|
|
|
const Trunk = struct {
|
|
|
|
prev: ?*Trunk = null,
|
|
|
|
str: []const u8 = undefined,
|
|
|
|
|
|
|
|
pub fn init(self: *Trunk, prev: ?*Trunk, str: []const u8) void {
|
|
|
|
self.prev = prev;
|
|
|
|
self.str = str;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn showTrunks(p: ?*Trunk) void {
|
|
|
|
if (p == null) return;
|
|
|
|
showTrunks(p.?.prev);
|
|
|
|
std.debug.print("{s}", .{p.?.str});
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
// 打印二叉树
|
2023-11-01 05:14:22 +08:00
|
|
|
pub fn printTree(root: ?*TreeNode(i32), prev: ?*Trunk, isRight: bool) !void {
|
2023-01-09 19:31:45 +08:00
|
|
|
if (root == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var prev_str = " ";
|
|
|
|
var trunk = Trunk{.prev = prev, .str = prev_str};
|
|
|
|
|
|
|
|
try printTree(root.?.right, &trunk, true);
|
|
|
|
|
|
|
|
if (prev == null) {
|
|
|
|
trunk.str = "———";
|
2023-11-01 05:14:22 +08:00
|
|
|
} else if (isRight) {
|
2023-01-09 19:31:45 +08:00
|
|
|
trunk.str = "/———";
|
|
|
|
prev_str = " |";
|
|
|
|
} else {
|
|
|
|
trunk.str = "\\———";
|
|
|
|
prev.?.str = prev_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
showTrunks(&trunk);
|
|
|
|
std.debug.print(" {}\n", .{root.?.val});
|
|
|
|
|
|
|
|
if (prev) |_| {
|
|
|
|
prev.?.str = prev_str;
|
|
|
|
}
|
|
|
|
trunk.str = " |";
|
|
|
|
|
|
|
|
try printTree(root.?.left, &trunk, false);
|
|
|
|
}
|