11const std = @import("std");
22const print = std.debug.print;
33const ArrayList = std.ArrayList;
4+ const Allocator = std.mem.Allocator;
45const testing = std.testing;
56
67// Returns a binary search tree instance.
@@ -22,7 +23,7 @@ pub fn BinarySearchTree(comptime T: type) type {
2223 left: ?*node = null,
2324 };
2425
25- allocator: *std.mem. Allocator,
26+ allocator: *Allocator,
2627 root: ?*node = null,
2728 size: usize = 0,
2829
@@ -56,27 +57,27 @@ pub fn BinarySearchTree(comptime T: type) type {
5657 }
5758
5859 // Function that performs inorder traversal of the tree
59- pub fn inorder(self: *Self, path: *ArrayList(T)) !void {
60+ pub fn inorder(self: *Self, allocator: Allocator, path: *ArrayList(T)) !void {
6061 if (self.root == null) {
6162 return;
6263 }
63- try self._inorder(self.root, path);
64+ try self._inorder(allocator, self.root, path);
6465 }
6566
6667 // Function that performs preorder traversal of the tree
67- pub fn preorder(self: *Self, path: *ArrayList(T)) !void {
68+ pub fn preorder(self: *Self, allocator: Allocator, path: *ArrayList(T)) !void {
6869 if (self.root == null) {
6970 return;
7071 }
71- try self._preorder(self.root, path);
72+ try self._preorder(allocator, self.root, path);
7273 }
7374
7475 // Function that performs postorder traversal of the tree
75- pub fn postorder(self: *Self, path: *ArrayList(T)) !void {
76+ pub fn postorder(self: *Self, allocator: Allocator, path: *ArrayList(T)) !void {
7677 if (self.root == null) {
7778 return;
7879 }
79- try self._postorder(self.root, path);
80+ try self._postorder(allocator, self.root, path);
8081 }
8182
8283 // Function that destroys the allocated memory of the whole tree
@@ -159,27 +160,27 @@ pub fn BinarySearchTree(comptime T: type) type {
159160 return false;
160161 }
161162
162- fn _inorder(self: *Self, root: ?*node, path: *ArrayList(T)) !void {
163+ fn _inorder(self: *Self, allocator: Allocator, root: ?*node, path: *ArrayList(T)) !void {
163164 if (root != null) {
164- try self._inorder(root.?.left, path);
165- try path.append(root.?.info);
166- try self._inorder(root.?.right, path);
165+ try self._inorder(allocator, root.?.left, path);
166+ try path.append(allocator, root.?.info);
167+ try self._inorder(allocator, root.?.right, path);
167168 }
168169 }
169170
170- fn _preorder(self: *Self, root: ?*node, path: *ArrayList(T)) !void {
171+ fn _preorder(self: *Self, allocator: Allocator, root: ?*node, path: *ArrayList(T)) !void {
171172 if (root != null) {
172- try path.append(root.?.info);
173- try self._preorder(root.?.left, path);
174- try self._preorder(root.?.right, path);
173+ try path.append(allocator, root.?.info);
174+ try self._preorder(allocator, root.?.left, path);
175+ try self._preorder(allocator, root.?.right, path);
175176 }
176177 }
177178
178- fn _postorder(self: *Self, root: ?*node, path: *ArrayList(T)) !void {
179+ fn _postorder(self: *Self, allocator: Allocator, root: ?*node, path: *ArrayList(T)) !void {
179180 if (root != null) {
180- try self._postorder(root.?.left, path);
181- try self._postorder(root.?.right, path);
182- try path.append(root.?.info);
181+ try self._postorder(allocator, root.?.left, path);
182+ try self._postorder(allocator, root.?.right, path);
183+ try path.append(allocator, root.?.info);
183184 }
184185 }
185186
@@ -244,26 +245,26 @@ test "Testing traversal methods" {
244245 try t.insert(12);
245246 try t.insert(15);
246247
247- var ino = ArrayList(i32).init(allocator) ;
248- defer ino.deinit();
248+ var ino: ArrayList(i32) = .empty ;
249+ defer ino.deinit(allocator );
249250
250251 const check_ino = [_]i32{ 3, 5, 12, 15, 25 };
251- try t.inorder(&ino);
252+ try t.inorder(allocator, &ino);
252253 try testing.expect(std.mem.eql(i32, ino.items, &check_ino));
253254
254- var pre = ArrayList(i32).init(allocator) ;
255- defer pre.deinit();
255+ var pre: ArrayList(i32) = .empty ;
256+ defer pre.deinit(allocator );
256257
257258 const check_pre = [_]i32{ 5, 3, 25, 12, 15 };
258- try t.preorder(&pre);
259+ try t.preorder(allocator, &pre);
259260
260261 try testing.expect(std.mem.eql(i32, pre.items, &check_pre));
261262
262- var post = ArrayList(i32).init(allocator) ;
263- defer post.deinit();
263+ var post: ArrayList(i32) = .empty ;
264+ defer post.deinit(allocator );
264265
265266 const check_post = [_]i32{ 3, 15, 12, 25, 5 };
266- try t.postorder(&post);
267+ try t.postorder(allocator, &post);
267268
268269 try testing.expect(std.mem.eql(i32, post.items, &check_post));
269270}
0 commit comments