Skip to content

Commit e057c0c

Browse files
authored
Merge pull request booniepepper#14 from booniepepper/booniepepper/dev
BinaryTree: Strip out things that aren't needed
2 parents 8082bf8 + 212335f commit e057c0c

File tree

1 file changed

+8
-27
lines changed

1 file changed

+8
-27
lines changed

src/experimental/binary_tree.zig

+8-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const std = @import("std");
2+
const Allocator = std.mem.Allocator;
23
const debug = std.debug;
34
const assert = debug.assert;
45
const testing = std.testing;
56

6-
pub const Chirality = enum { lhs, rhs };
7-
87
/// A very simple tree where every node is a tree.
98
/// This is primarily intended to be a backing data structure for more
109
/// sophisticated tree-based data structures with tuned insertion logic,
@@ -31,39 +30,20 @@ pub fn BinaryTree(comptime T: type) type {
3130
/// Reverse the tree starting from this node in-place. Why would anyone
3231
/// need this? Why does it come up in tech interviews?
3332
/// This operation is O(N).
34-
///
35-
/// Returns a pointer to self.
36-
pub fn reverse(self: *Self) *Self {
33+
pub fn reverse(self: *Self) void {
3734
const temp = self.lhs;
3835

3936
self.lhs = self.rhs;
4037
self.rhs = temp;
4138

42-
if (self.lhs) |lhs| _ = lhs.reverse();
43-
if (self.rhs) |rhs| _ = rhs.reverse();
44-
45-
return self;
46-
}
47-
48-
/// Assigns a node to one of the sides of this node.
49-
///
50-
/// Returns a pointer to subtree.
51-
pub fn assign(self: *Self, side: Chirality, subtree: *Self) *Self {
52-
switch (side) {
53-
.lhs => self.lhs = subtree,
54-
.rhs => self.rhs = subtree,
55-
}
56-
57-
return subtree;
39+
if (self.lhs) |lhs| lhs.reverse();
40+
if (self.rhs) |rhs| rhs.reverse();
5841
}
5942

6043
/// Sets both lhs and rhs to null.
61-
///
62-
/// Returns a pointer to self.
63-
pub fn reset(self: *Self) *Self {
44+
pub fn prune(self: *Self) void {
6445
self.lhs = null;
6546
self.rhs = null;
66-
return self;
6747
}
6848
};
6949
}
@@ -177,7 +157,8 @@ test "((5 - 4) + (0 + 2)) + ((6 - 2) + (2 + 3))" {
177157
// / \ / \ / \ / \
178158
// 3 2 2 6 2 0 4 5
179159

180-
var zero = negativeSix.reverse();
181-
try testing.expectEqual(@as(i32, 0), Math.resolve(zero));
160+
negativeSix.reverse();
161+
var zero = negativeSix;
162+
try testing.expectEqual(@as(i32, 0), Math.resolve(&zero));
182163
try testing.expectEqual(@as(usize, 15), zero.count());
183164
}

0 commit comments

Comments
 (0)