add zig codes for Section 'Binary Tree' (#292)

* add zig codes for Section 'Binary Tree'

* add zig codes for Section 'Binary Tree'
pull/295/head
sjinzh 2 years ago committed by GitHub
parent d9f8c53e4a
commit fc8bbb7d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -268,6 +268,19 @@ pub fn build(b: *std.build.Builder) void {
const run_step_binary_tree= b.step("run_binary_tree", "Run binary_tree"); const run_step_binary_tree= b.step("run_binary_tree", "Run binary_tree");
run_step_binary_tree.dependOn(&run_cmd_binary_tree.step); run_step_binary_tree.dependOn(&run_cmd_binary_tree.step);
// Source File: "chapter_tree/binary_tree_bfs.zig"
// Run Command: zig build run_binary_tree_bfs
const exe_binary_tree_bfs = b.addExecutable("binary_tree_bfs", "chapter_tree/binary_tree_bfs.zig");
exe_binary_tree_bfs.addPackagePath("include", "include/include.zig");
exe_binary_tree_bfs.setTarget(target);
exe_binary_tree_bfs.setBuildMode(mode);
exe_binary_tree_bfs.install();
const run_cmd_binary_tree_bfs = exe_binary_tree_bfs.run();
run_cmd_binary_tree_bfs.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_binary_tree_bfs.addArgs(args);
const run_step_binary_tree_bfs = b.step("run_binary_tree_bfs", "Run binary_tree_bfs");
run_step_binary_tree_bfs.dependOn(&run_cmd_binary_tree_bfs.step);
// Section: "Heap" // Section: "Heap"
// Source File: "chapter_heap/heap.zig" // Source File: "chapter_heap/heap.zig"
// Run Command: zig build run_heap // Run Command: zig build run_heap

@ -0,0 +1,58 @@
// File: binary_tree_bfs.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
//
fn hierOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {
//
const L = std.TailQueue(*inc.TreeNode(T));
var queue = L{};
var root_node = try mem_allocator.create(L.Node);
root_node.data = root;
queue.append(root_node);
//
var list = std.ArrayList(T).init(std.heap.page_allocator);
while (queue.len > 0) {
var queue_node = queue.popFirst().?; //
var node = queue_node.data;
try list.append(node.val); //
if (node.left != null) {
var tmp_node = try mem_allocator.create(L.Node);
tmp_node.data = node.left.?;
queue.append(tmp_node); //
}
if (node.right != null) {
var tmp_node = try mem_allocator.create(L.Node);
tmp_node.data = node.right.?;
queue.append(tmp_node); //
}
}
return list;
}
// Driver Code
pub fn main() !void {
//
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer mem_arena.deinit();
const mem_allocator = mem_arena.allocator();
//
//
var nums = [_]i32{1, 2, 3, 4, 5, 6, 7};
var root = try inc.TreeUtil.arrToTree(i32, mem_allocator, &nums);
std.debug.print("初始化二叉树\n", .{});
try inc.PrintUtil.printTree(root, null, false);
//
var list = try hierOrder(i32, mem_allocator, root.?);
defer list.deinit();
std.debug.print("\n层序遍历的结点打印序列 = ", .{});
inc.PrintUtil.printList(i32, list);
const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
}
Loading…
Cancel
Save