-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeSearch.zig
More file actions
145 lines (130 loc) · 3.96 KB
/
BinaryTreeSearch.zig
File metadata and controls
145 lines (130 loc) · 3.96 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
const std = @import("std");
const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator;
var allocator: std.mem.Allocator = undefined;
const Queue = struct {
const QueueNode = struct {
prev: ?*QueueNode,
next: ?*QueueNode,
value: *Node,
};
head: ?*QueueNode = null,
tail: ?*QueueNode = null,
size: usize = 0,
fn push(self: *Queue, node: *Node) void {
if (self.head != null) {
const newQueueNode: ?*QueueNode = createQueueNode();
if (newQueueNode) |n| {
n.prev = null;
n.next = null;
n.value = node;
self.tail = n;
}
} else {
const newQueueNode: ?*QueueNode = createQueueNode();
if (newQueueNode) |n| {
n.prev = self.tail;
n.next = null;
n.value = node;
self.head = n;
self.tail = n;
}
}
self.size += 1;
}
fn pop(self: *Queue) ?*Node {
const node = self.head.?.value;
self.head = self.head.?.next;
self.size -= 1;
return node;
}
fn createQueueNode() ?*Queue.QueueNode {
return allocator.create(Queue.QueueNode) catch |err| {
std.debug.print("Error creating queuenode: {}", .{err});
return null;
};
}
};
const Node = struct {
left: ?*Node = null,
right: ?*Node = null,
value: u8,
};
fn insert(node: *Node, value: u8) void {
var queue = Queue{};
var temp: *Node = node;
queue.push(temp);
while (queue.size > 0) {
temp = queue.pop().?;
if (temp.left == null and value < temp.value) {
temp.left = newNode(value);
break;
} else if (value < temp.value) {
queue.push(temp.left.?);
}
if (temp.right == null and value > temp.value) {
temp.right = newNode(value);
break;
} else if (value > temp.value) {
queue.push(temp.right.?);
}
}
}
fn newNode(value: u8) ?*Node {
const new_node = allocator.create(Node) catch {
std.debug.print("Erorr trying to insert value: {}", .{value});
return null;
};
new_node.right = null;
new_node.left = null;
new_node.value = value;
return new_node;
}
fn inOrderFormat(temp: ?*Node, depth: u8) void {
if (temp) |t| {
var nd = depth + 4;
inOrderFormat(t.right, nd);
for (0..depth) |_| {
std.debug.print(" ", .{});
}
std.debug.print("{}< \n", .{t.*.value});
inOrderFormat(t.left, nd);
}
}
fn search(node: ?*Node, value: u8) ?*Node {
if (node == null or node.?.*.value == value) return node;
if (node.?.*.value < value) return search(node.?.*.right, value);
if (node.?.*.value > value) return search(node.?.*.left, value);
unreachable;
}
fn printResult(result: ?*Node) void {
if (result) |r| {
std.debug.print("Value is present: \n", .{});
inOrderFormat(r, 0);
} else std.debug.print("Value not present in tree\n", .{});
}
pub fn main() !void {
var general_purpose_allocator = GeneralPurposeAllocator(.{}){};
const gpa = general_purpose_allocator.allocator();
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();
allocator = arena_instance.allocator();
var node = Node{
.value = 10,
};
node.left = newNode(7);
node.left.?.left = newNode(3);
node.right = newNode(15);
node.right.?.left = newNode(13);
node.right.?.right = newNode(17);
std.debug.print("Inorder traversal before insertion: \n", .{});
inOrderFormat(&node, 0);
insert(&node, 12);
insert(&node, 8);
insert(&node, 4);
insert(&node, 49);
std.debug.print("\nInorder traversal after insertion: \n", .{});
inOrderFormat(&node, 0);
printResult(search(&node, 12));
printResult(search(&node, 15));
printResult(search(&node, 20));
}