You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/zh-hant/codes/zig/chapter_tree/binary_tree_bfs.zig

57 lines
2.0 KiB

// File: binary_tree_bfs.zig
// Created Time: 2023-01-15
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// 層序走訪
fn levelOrder(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 levelOrder(i32, mem_allocator, root.?);
defer list.deinit();
std.debug.print("\n層序走訪的節點列印序列 = ", .{});
inc.PrintUtil.printList(i32, list);
_ = try std.io.getStdIn().reader().readByte();
}