2023-01-09 19:31:45 +08:00
|
|
|
// 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();
|
|
|
|
|
2023-01-28 00:47:58 +08:00
|
|
|
val: T = undefined, // 结点值
|
|
|
|
height: i32 = undefined, // 结点高度
|
|
|
|
left: ?*Self = null, // 左子结点指针
|
|
|
|
right: ?*Self = null, // 右子结点指针
|
2023-01-09 19:31:45 +08:00
|
|
|
|
|
|
|
// Initialize a tree node with specific value
|
|
|
|
pub fn init(self: *Self, x: i32) void {
|
|
|
|
self.val = x;
|
2023-01-28 00:47:58 +08:00
|
|
|
self.height = 0;
|
2023-01-13 23:40:37 +08:00
|
|
|
self.left = null;
|
|
|
|
self.right = null;
|
2023-01-09 19:31:45 +08:00
|
|
|
}
|
|
|
|
};
|
2023-01-14 08:11:54 +08:00
|
|
|
}
|
|
|
|
|
2023-01-16 05:30:46 +08:00
|
|
|
// Generate a binary tree with an array
|
|
|
|
pub fn arrToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) {
|
2023-01-15 20:39:46 +08:00
|
|
|
if (arr.len == 0) return null;
|
|
|
|
var root = try mem_allocator.create(TreeNode(T));
|
|
|
|
root.init(arr[0]);
|
|
|
|
const L = std.TailQueue(*TreeNode(T));
|
|
|
|
var que = L{};
|
|
|
|
var root_node = try mem_allocator.create(L.Node);
|
|
|
|
root_node.data = root;
|
|
|
|
que.append(root_node);
|
|
|
|
var index: usize = 0;
|
|
|
|
while (que.len > 0) {
|
|
|
|
var que_node = que.popFirst().?;
|
|
|
|
var node = que_node.data;
|
|
|
|
index += 1;
|
|
|
|
if (index >= arr.len) break;
|
|
|
|
if (index < arr.len) {
|
|
|
|
var tmp = try mem_allocator.create(TreeNode(T));
|
|
|
|
tmp.init(arr[index]);
|
|
|
|
node.left = tmp;
|
|
|
|
var tmp_node = try mem_allocator.create(L.Node);
|
|
|
|
tmp_node.data = node.left.?;
|
|
|
|
que.append(tmp_node);
|
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
if (index >= arr.len) break;
|
|
|
|
if (index < arr.len) {
|
|
|
|
var tmp = try mem_allocator.create(TreeNode(T));
|
|
|
|
tmp.init(arr[index]);
|
|
|
|
node.right = tmp;
|
|
|
|
var tmp_node = try mem_allocator.create(L.Node);
|
|
|
|
tmp_node.data = node.right.?;
|
|
|
|
que.append(tmp_node);
|
2023-01-14 08:11:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
2023-01-09 19:31:45 +08:00
|
|
|
}
|