forked from booniepepper/zig-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.zig
More file actions
164 lines (135 loc) · 4.76 KB
/
Copy pathbinary_tree.zig
File metadata and controls
164 lines (135 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const std = @import("std");
const Allocator = std.mem.Allocator;
const debug = std.debug;
const assert = debug.assert;
const testing = std.testing;
/// A very simple tree where every node is a tree.
/// This is primarily intended to be a backing data structure for more
/// sophisticated tree-based data structures with tuned insertion logic,
/// balancing, etc.
pub fn BinaryTree(comptime T: type) type {
return struct {
lhs: ?*Self = null,
rhs: ?*Self = null,
data: T,
const Self = @This();
/// Iterate over each child node, returning the count of all nodes including itself.
/// This operation is O(N).
pub fn count(self: *const Self) usize {
var n: usize = 1;
if (self.lhs) |lhs| n += lhs.count();
if (self.rhs) |rhs| n += rhs.count();
return n;
}
/// Reverse the tree starting from this node in-place. Why would anyone
/// need this? Why does it come up in tech interviews?
/// This operation is O(N).
pub fn reverse(self: *Self) void {
const temp = self.lhs;
self.lhs = self.rhs;
self.rhs = temp;
if (self.lhs) |lhs| lhs.reverse();
if (self.rhs) |rhs| rhs.reverse();
}
/// Sets both lhs and rhs to null.
pub fn prune(self: *Self) void {
self.lhs = null;
self.rhs = null;
}
};
}
// TESTING
// Naive Math values to simulate an AST.
const Math = union(enum) {
plus: void,
minus: void,
n: i32,
// TODO: Add a simple parser?
// Assumes that all branches are operators and their lhs/rhs are present. Assumes all leaves are numbers.
fn resolve(tree: *BinaryTree(Math)) i32 {
return switch (tree.data) {
.plus => Math.resolve(tree.lhs.?) + Math.resolve(tree.rhs.?),
.minus => Math.resolve(tree.lhs.?) - Math.resolve(tree.rhs.?),
.n => |n| n,
};
}
};
test "1 + 2 = 3" {
const T = BinaryTree(Math);
var plus = T{ .data = .plus };
try testing.expectEqual(@as(usize, 1), plus.count());
var one = T{ .data = Math{ .n = 1 } };
var two = T{ .data = Math{ .n = 2 } };
plus.lhs = &one;
plus.rhs = &two;
try testing.expectEqual(@as(i32, 3), Math.resolve(&plus));
try testing.expectEqual(@as(usize, 3), plus.count());
}
test "((5 - 4) + (0 + 2)) + ((6 - 2) + (2 + 3))" {
const T = BinaryTree(Math);
// We'll build up this:
// -
// / \
// + +
// / | | \
// - + - +
// / \ / \ / \ / \
// 5 4 0 2 6 2 2 3
var _one_lhs = T{ .data = .{ .n = 5 } };
var _one_rhs = T{ .data = .{ .n = 4 } };
var one = T{
.data = .minus,
.lhs = &_one_lhs,
.rhs = &_one_rhs,
};
try testing.expectEqual(@as(i32, 1), Math.resolve(&one));
try testing.expectEqual(@as(usize, 3), one.count());
var _two_lhs = T{ .data = .{ .n = 0 } };
var _two_rhs = T{ .data = .{ .n = 2 } };
var two = T{
.data = .plus,
.lhs = &_two_lhs,
.rhs = &_two_rhs,
};
try testing.expectEqual(@as(i32, 2), Math.resolve(&two));
try testing.expectEqual(@as(usize, 3), two.count());
var three = T{ .data = .plus, .lhs = &one, .rhs = &two };
try testing.expectEqual(@as(i32, 3), Math.resolve(&three));
try testing.expectEqual(@as(usize, 7), three.count());
var _four_lhs = T{ .data = .{ .n = 6 } };
var _four_rhs = T{ .data = .{ .n = 2 } };
var four = T{
.data = .minus,
.lhs = &_four_lhs,
.rhs = &_four_rhs,
};
try testing.expectEqual(@as(i32, 4), Math.resolve(&four));
try testing.expectEqual(@as(usize, 3), four.count());
var _five_lhs = T{ .data = .{ .n = 2 } };
var _five_rhs = T{ .data = .{ .n = 3 } };
var five = T{
.data = .plus,
.lhs = &_five_lhs,
.rhs = &_five_rhs,
};
try testing.expectEqual(@as(i32, 5), Math.resolve(&five));
try testing.expectEqual(@as(usize, 3), five.count());
var nine = T{ .data = .plus, .lhs = &four, .rhs = &five };
try testing.expectEqual(@as(i32, 9), Math.resolve(&nine));
try testing.expectEqual(@as(usize, 7), nine.count());
var negativeSix = T{ .data = .minus, .lhs = &three, .rhs = &nine };
try testing.expectEqual(@as(i32, -6), Math.resolve(&negativeSix));
try testing.expectEqual(@as(usize, 15), negativeSix.count());
// Deep reversal will flip the subtraction operations.
// -
// / \
// + +
// / | | \
// + - + -
// / \ / \ / \ / \
// 3 2 2 6 2 0 4 5
negativeSix.reverse();
var zero = negativeSix;
try testing.expectEqual(@as(i32, 0), Math.resolve(&zero));
try testing.expectEqual(@as(usize, 15), zero.count());
}