mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
add zig codes for Section 'Space Complexity' and 'Space Time Tradeoff'
This commit is contained in:
20
codes/zig/include/ListNode.zig
Normal file
20
codes/zig/include/ListNode.zig
Normal file
@@ -0,0 +1,20 @@
|
||||
// File: ListNode.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// Definition for a singly-linked list node
|
||||
pub fn ListNode(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
val: T = 0,
|
||||
next: ?*Self = null,
|
||||
|
||||
// Initialize a list node with specific value
|
||||
pub fn init(self: *Self, x: i32) void {
|
||||
self.val = x;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,13 +1,72 @@
|
||||
// File: TreeNode.zig
|
||||
// File: PrintUtil.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
const ListNode = @import("ListNode.zig").ListNode;
|
||||
const TreeNode = @import("TreeNode.zig").TreeNode;
|
||||
|
||||
// Print an Array
|
||||
// Print an array
|
||||
pub fn printArray(comptime T: type, nums: []T) void {
|
||||
std.debug.print("[", .{});
|
||||
for (nums) |num, j| {
|
||||
std.debug.print("{}{s}", .{num, if (j == nums.len-1) "]\n" else ", " });
|
||||
}
|
||||
if (nums.len > 0) {
|
||||
for (nums) |num, j| {
|
||||
std.debug.print("{}{s}", .{num, if (j == nums.len-1) "]\n" else ", " });
|
||||
}
|
||||
} else {
|
||||
std.debug.print("]", .{});
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to print branches of the binary tree
|
||||
pub fn showTrunks(p: ?*Trunk) void {
|
||||
if (p == null) return;
|
||||
showTrunks(p.?.prev);
|
||||
std.debug.print("{s}", .{p.?.str});
|
||||
}
|
||||
|
||||
// The interface of the tree printer
|
||||
// Print a binary tree
|
||||
pub fn printTree(root: ?*TreeNode(i32), prev: ?*Trunk, isLeft: bool) !void {
|
||||
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 = "———";
|
||||
} else if (isLeft) {
|
||||
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);
|
||||
}
|
||||
21
codes/zig/include/TreeNode.zig
Normal file
21
codes/zig/include/TreeNode.zig
Normal file
@@ -0,0 +1,21 @@
|
||||
// File: TreeNode.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// Definition for a binary tree node
|
||||
pub fn TreeNode(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
val: T = undefined,
|
||||
left: ?*Self = null,
|
||||
right: ?*Self = null,
|
||||
|
||||
// Initialize a tree node with specific value
|
||||
pub fn init(self: *Self, x: i32) void {
|
||||
self.val = x;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
// File: include.zig
|
||||
// Created Time: 2023-01-04
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
pub const PrintUtil = @import("PrintUtil.zig");
|
||||
pub const PrintUtil = @import("PrintUtil.zig");
|
||||
pub const ListNode = @import("ListNode.zig").ListNode;
|
||||
pub const TreeNode = @import("TreeNode.zig").TreeNode;
|
||||
Reference in New Issue
Block a user