mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
70 lines
2.3 KiB
Zig
70 lines
2.3 KiB
Zig
// File: binary_tree_dfs.zig
|
|
// Created Time: 2023-01-15
|
|
// Author: codingonion ([email protected])
|
|
|
|
const std = @import("std");
|
|
const inc = @import("include");
|
|
|
|
var list = std.ArrayList(i32).init(std.heap.page_allocator);
|
|
|
|
// 行きがけ順走査
|
|
fn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
|
|
if (root == null) return;
|
|
// 訪問順序:根ノード -> 左部分木 -> 右部分木
|
|
try list.append(root.?.val);
|
|
try preOrder(T, root.?.left);
|
|
try preOrder(T, root.?.right);
|
|
}
|
|
|
|
// 通りがけ順走査
|
|
fn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
|
|
if (root == null) return;
|
|
// 訪問順序:左部分木 -> 根ノード -> 右部分木
|
|
try inOrder(T, root.?.left);
|
|
try list.append(root.?.val);
|
|
try inOrder(T, root.?.right);
|
|
}
|
|
|
|
// 帰りがけ順走査
|
|
fn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
|
|
if (root == null) return;
|
|
// 訪問順序:左部分木 -> 右部分木 -> 根ノード
|
|
try postOrder(T, root.?.left);
|
|
try postOrder(T, root.?.right);
|
|
try list.append(root.?.val);
|
|
}
|
|
|
|
// 実行用コード
|
|
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);
|
|
|
|
// 行きがけ順走査
|
|
list.clearRetainingCapacity();
|
|
try preOrder(i32, root);
|
|
std.debug.print("\n行きがけ順走査で表示するノードの順序 = ", .{});
|
|
inc.PrintUtil.printList(i32, list);
|
|
|
|
// 通りがけ順走査
|
|
list.clearRetainingCapacity();
|
|
try inOrder(i32, root);
|
|
std.debug.print("\n通りがけ順走査で表示するノードの順序 = ", .{});
|
|
inc.PrintUtil.printList(i32, list);
|
|
|
|
// 帰りがけ順走査
|
|
list.clearRetainingCapacity();
|
|
try postOrder(i32, root);
|
|
std.debug.print("\n帰りがけ順走査で表示するノードの順序 = ", .{});
|
|
inc.PrintUtil.printList(i32, list);
|
|
|
|
_ = try std.io.getStdIn().reader().readByte();
|
|
} |