Merge pull request #249 from sjinzh/master

add zig codes for Section 'Hash Map', 'Binary Tree' and 'Linear Search'
This commit is contained in:
Yudong Jin 2023-01-14 19:55:22 +08:00 committed by GitHub
commit a5affe6113
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 278 additions and 7 deletions

View file

@ -160,6 +160,48 @@ pub fn build(b: *std.build.Builder) void {
const run_step_array_stack = b.step("run_array_stack", "Run array_stack");
run_step_array_stack.dependOn(&run_cmd_array_stack.step);
// Section: "Hash Map"
// Source File: "chapter_hashing/hash_map.zig"
// Run Command: zig build run_hash_map
const exe_hash_map = b.addExecutable("hash_map", "chapter_hashing/hash_map.zig");
exe_hash_map.addPackagePath("include", "include/include.zig");
exe_hash_map.setTarget(target);
exe_hash_map.setBuildMode(mode);
exe_hash_map.install();
const run_cmd_hash_map = exe_hash_map.run();
run_cmd_hash_map.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_hash_map.addArgs(args);
const run_step_hash_map= b.step("run_hash_map", "Run hash_map");
run_step_hash_map.dependOn(&run_cmd_hash_map.step);
// Section: "Binary Tree"
// Source File: "chapter_tree/binary_tree.zig"
// Run Command: zig build run_binary_tree
const exe_binary_tree = b.addExecutable("hash_map", "chapter_tree/binary_tree.zig");
exe_binary_tree.addPackagePath("include", "include/include.zig");
exe_binary_tree.setTarget(target);
exe_binary_tree.setBuildMode(mode);
exe_binary_tree.install();
const run_cmd_binary_tree = exe_binary_tree.run();
run_cmd_binary_tree.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_binary_tree.addArgs(args);
const run_step_binary_tree= b.step("run_binary_tree", "Run binary_tree");
run_step_binary_tree.dependOn(&run_cmd_binary_tree.step);
// Section: "Linear Search"
// Source File: "chapter_searching/linear_search.zig"
// Run Command: zig build run_linear_search
const exe_linear_search = b.addExecutable("linear_search", "chapter_searching/linear_search.zig");
exe_linear_search.addPackagePath("include", "include/include.zig");
exe_linear_search.setTarget(target);
exe_linear_search.setBuildMode(mode);
exe_linear_search.install();
const run_cmd_linear_search = exe_linear_search.run();
run_cmd_linear_search.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_linear_search.addArgs(args);
const run_step_linear_search= b.step("run_linear_search", "Run linear_search");
run_step_linear_search.dependOn(&run_cmd_linear_search.step);
// Section: "Bubble Sort"
// Source File: "chapter_sorting/bubble_sort.zig"
// Run Command: zig build run_bubble_sort

View file

@ -0,0 +1,55 @@
// File: hash_map.zig
// Created Time: 2023-01-13
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
//
var map = std.AutoHashMap(i32, []const u8).init(std.heap.page_allocator);
//
defer map.deinit();
//
// (key, value)
try map.put(12836, "小哈");
try map.put(15937, "小啰");
try map.put(16750, "小算");
try map.put(13276, "小法");
try map.put(10583, "小鸭");
std.debug.print("\n添加完成后,哈希表为\nKey -> Value\n", .{});
inc.PrintUtil.printHashMap(i32, []const u8, map);
//
// key value
var name = map.get(15937).?;
std.debug.print("\n输入学号 15937 ,查询到姓名 {s}\n", .{name});
//
// (key, value)
_ = map.remove(10583);
std.debug.print("\n删除 10583 后,哈希表为\nKey -> Value\n", .{});
inc.PrintUtil.printHashMap(i32, []const u8, map);
//
std.debug.print("\n遍历键值对 Key->Value\n", .{});
inc.PrintUtil.printHashMap(i32, []const u8, map);
std.debug.print("\n单独遍历键 Key\n", .{});
var it = map.iterator();
while (it.next()) |kv| {
std.debug.print("{}\n", .{kv.key_ptr.*});
}
std.debug.print("\n单独遍历值 value\n", .{});
it = map.iterator();
while (it.next()) |kv| {
std.debug.print("{s}\n", .{kv.value_ptr.*});
}
const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
}

View file

@ -0,0 +1,56 @@
// File: linear_search.zig
// Created Time: 2023-01-13
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// 线
fn linearSearchList(comptime T: type, nums: std.ArrayList(T), target: T) T {
//
for (nums.items) |num, i| {
//
if (num == target) {
return @intCast(T, i);
}
}
// -1
return -1;
}
// 线
pub fn linearSearchLinkedList(comptime T: type, node: ?*inc.ListNode(T), target: T) ?*inc.ListNode(T) {
var head = node;
//
while (head != null) {
//
if (head.?.val == target) return head;
head = head.?.next;
}
return null;
}
// Driver Code
pub fn main() !void {
var target: i32 = 3;
// 线
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 });
var index = linearSearchList(i32, nums, target);
std.debug.print("目标元素 3 的索引 = {}\n", .{index});
// 线
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer mem_arena.deinit();
const mem_allocator = mem_arena.allocator();
var head = try inc.ListUtil.listToLinkedList(i32, mem_allocator, nums);
var node = linearSearchLinkedList(i32, head, target);
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
try inc.PrintUtil.printLinkedList(i32, node);
const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
}

View file

@ -1,4 +1,4 @@
// File: time_complexity.zig
// File: bubble_sort.zig
// Created Time: 2023-01-08
// Author: sjinzh (sjinzh@gmail.com)

View file

@ -1,4 +1,4 @@
// File: time_complexity.zig
// File: insertion_sort.zig
// Created Time: 2023-01-08
// Author: sjinzh (sjinzh@gmail.com)

View file

@ -1,4 +1,4 @@
// File: stack.zig
// File: array_stack.zig
// Created Time: 2023-01-08
// Author: sjinzh (sjinzh@gmail.com)

View file

@ -0,0 +1,40 @@
// File: binary_tree.zig
// Created Time: 2023-01-14
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
//
//
var n1 = inc.TreeNode(i32){ .val = 1 };
var n2 = inc.TreeNode(i32){ .val = 2 };
var n3 = inc.TreeNode(i32){ .val = 3 };
var n4 = inc.TreeNode(i32){ .val = 4 };
var n5 = inc.TreeNode(i32){ .val = 5 };
//
n1.left = &n2;
n1.right = &n3;
n2.left = &n4;
n2.right = &n5;
std.debug.print("初始化二叉树\n", .{});
try inc.PrintUtil.printTree(&n1, null, false);
//
var p = inc.TreeNode(i32){ .val = 0 };
// n1 -> n2 P
n1.left = &p;
p.left = &n2;
std.debug.print("插入结点 P 后\n", .{});
try inc.PrintUtil.printTree(&n1, null, false);
//
n1.left = &n2;
std.debug.print("删除结点 P 后\n", .{});
try inc.PrintUtil.printTree(&n1, null, false);
const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
}

View file

@ -16,6 +16,21 @@ pub fn ListNode(comptime T: type) type {
// Initialize a list node with specific value
pub fn init(self: *Self, x: i32) void {
self.val = x;
self.next = null;
}
};
}
// Generate a linked list with a list
pub fn listToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, list: std.ArrayList(T)) !?*ListNode(T) {
var dum = try mem_allocator.create(ListNode(T));
dum.init(0);
var head = dum;
for (list.items) |val| {
var tmp = try mem_allocator.create(ListNode(T));
tmp.init(val);
head.next = tmp;
head = head.next.?;
}
return dum.next;
}

View file

@ -3,8 +3,10 @@
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const ListNode = @import("ListNode.zig").ListNode;
const TreeNode = @import("TreeNode.zig").TreeNode;
pub const ListUtil = @import("ListNode.zig");
pub const ListNode = ListUtil.ListNode;
pub const TreeUtil = @import("TreeNode.zig");
pub const TreeNode = TreeUtil.TreeNode;
// Print an array
pub fn printArray(comptime T: type, nums: []T) void {
@ -46,6 +48,27 @@ pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void {
}
}
// Print a HashMap
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});
}
}
// print a heap (PriorityQueue)
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);
}
// This tree printer is borrowed from TECHIE DELIGHT
// https://www.techiedelight.com/c-program-print-binary-tree/
const Trunk = struct {

View file

@ -17,6 +17,44 @@ pub fn TreeNode(comptime T: type) type {
// Initialize a tree node with specific value
pub fn init(self: *Self, x: i32) void {
self.val = x;
self.left = null;
self.right = null;
}
};
}
// Generate a binary tree with an array
pub fn arrToTree(comptime T: type, mem_allocator: std.mem.Allocator, list: []T) !?*TreeNode(T) {
if (list.len == 0) return null;
var root = try mem_allocator.create(TreeNode(T));
root.init(list[0]);
const TailQueue = std.TailQueue(?*TreeNode(T));
const TailQueueNode = std.TailQueue(?*TreeNode(T)).Node;
var que = TailQueue{};
var node_root = TailQueueNode{ .data = root };
que.append(&node_root);
var index: usize = 0;
while (que.len > 0) {
var node = que.popFirst();
index += 1;
if (index >= list.len) break;
if (index < list.len) {
var tmp = try mem_allocator.create(TreeNode(T));
tmp.init(list[index]);
node.?.data.?.left = tmp;
var a = TailQueueNode{ .data = node.?.data.?.left };
que.append(&a);
}
index += 1;
if (index >= list.len) break;
if (index < list.len) {
var tmp = try mem_allocator.create(TreeNode(T));
tmp.init(list[index]);
node.?.data.?.right = tmp;
var a = TailQueueNode{ .data = node.?.data.?.right };
que.append(&a);
}
}
return root;
}

View file

@ -3,5 +3,7 @@
// Author: sjinzh (sjinzh@gmail.com)
pub const PrintUtil = @import("PrintUtil.zig");
pub const ListNode = @import("ListNode.zig").ListNode;
pub const TreeNode = @import("TreeNode.zig").TreeNode;
pub const ListUtil = @import("ListNode.zig");
pub const ListNode = ListUtil.ListNode;
pub const TreeUtil = @import("TreeNode.zig");
pub const TreeNode = TreeUtil.TreeNode;