/
binary_tree.zig
202 lines (164 loc) · 5.49 KB
/
binary_tree.zig
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const std = @import("std");
const testing = std.testing;
pub fn BinaryTree(comptime T: type) type {
return struct {
root: usize = 1,
items: NodeList,
count: usize,
const Self = @This();
pub const SearchResult = union(enum) {
found: Found,
not_found,
};
const Found = struct {
location: usize,
data: T,
};
const NodeList = std.ArrayList(?Node);
const Node = struct {
left: usize,
right: usize,
location: usize,
data: T,
pub fn init(data: T, location: usize) Node {
return Node{
.left = 2 * location,
.right = (2 * location) + 1,
.location = location,
.data = data,
};
}
};
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.items = NodeList.init(allocator),
.count = 0,
};
}
pub fn deinit(self: *Self) void {
self.items.deinit();
}
pub fn insert(self: *Self, data: T) !void {
var iter: usize = 0;
while (true) {
try self.items.ensureTotalCapacity(iter);
if (self.items.items[iter]) |node| {
if (data == node.data) {
break;
} else if (data > node.data) {
iter = node.right;
} else {
iter = node.left;
}
} else {
try self.items.insert(iter, Node.init(data, iter));
self.count += 1;
break;
}
}
}
pub fn map(self: *Self, comptime func: fn (T) T) void {
for (self.items.items) |*item| {
if (item.*) |node| {
item.*.?.data = func(node.data);
}
}
}
// TODO: fix the links might need to shift some nodes around
pub fn delete(self: *Self, data: T) !void {
switch (self.search(data)) {
.found => |result| {
try self.items.insert(result.location, null);
self.count -= 1;
},
.not_found => {},
}
}
pub fn search(self: *Self, data: T) SearchResult {
var iter: usize = 1;
while (true) {
if (iter > self.items.capacity) {
return .not_found;
}
if (self.items.items[iter]) |node| {
if (data == node.data) {
return .{ .found = .{ .location = node.location, .data = node.data } };
} else if (data > node.data) {
iter = node.right;
} else {
iter = node.left;
}
} else {
return .not_found;
}
}
}
};
}
test "BinaryTree.init" {
var tree = BinaryTree(i32).init(testing.allocator);
defer tree.deinit();
try testing.expectEqual(@as(usize, 0), tree.count);
}
test "BinaryTree.insert" {
var tree = BinaryTree(i32).init(testing.allocator);
defer tree.deinit();
try tree.insert(3);
try tree.insert(7);
try tree.insert(2);
try testing.expectEqual(@as(usize, 3), tree.count);
try testing.expectEqual(@as(i32, 3), tree.items.items[tree.root].?.data);
try testing.expectEqual(@as(i32, 7), tree.items.items[tree.items.items[tree.root].?.right].?.data);
try testing.expectEqual(@as(i32, 2), tree.items.items[tree.items.items[tree.root].?.left].?.data);
}
test "BinaryTree.map" {
var tree = BinaryTree(i32).init(testing.allocator);
defer tree.deinit();
try tree.insert(3);
try tree.insert(7);
try tree.insert(2);
const addOne = struct {
fn function(data: i32) i32 {
return data + 1;
}
}.function;
tree.map(addOne);
try testing.expectEqual(@as(usize, 3), tree.count);
try testing.expectEqual(@as(i32, 4), tree.items.items[tree.root].?.data);
try testing.expectEqual(@as(i32, 8), tree.items.items[tree.items.items[tree.root].?.right].?.data);
try testing.expectEqual(@as(i32, 3), tree.items.items[tree.items.items[tree.root].?.left].?.data);
}
test "BinaryTree.search" {
var tree = BinaryTree(i32).init(testing.allocator);
defer tree.deinit();
const SearchResult = BinaryTree(i32).SearchResult;
try testing.expectEqual(SearchResult.not_found, tree.search(7));
try tree.insert(5);
try tree.insert(7);
try tree.insert(4);
try testing.expectEqual(SearchResult{
.found = .{
.data = 7,
.location = 3,
},
}, tree.search(7));
try testing.expectEqual(SearchResult{
.found = .{
.data = 4,
.location = 2,
},
}, tree.search(4));
try testing.expectEqual(SearchResult.not_found, tree.search(8));
}
test "BinaryTree.delete" {
var tree = BinaryTree(i32).init(testing.allocator);
defer tree.deinit();
try tree.insert(4);
try tree.insert(7);
try tree.insert(1);
try tree.insert(3);
try tree.insert(6);
try testing.expectEqual(@as(usize, 5), tree.count);
try tree.delete(3);
try testing.expectEqual(@as(usize, 4), tree.count);
}