-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.php
More file actions
275 lines (222 loc) · 8.4 KB
/
BinarySearchTree.php
File metadata and controls
275 lines (222 loc) · 8.4 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace Zubs\Dsa\BinarySearchTree;
class BinarySearchTree
{
private BinarySearchNode | null $root = null;
/**
* Add a new node to the tree
* @param int $data Number to add to the tree
* @return bool True or False expressing success
*/
public function insert(int $data): bool
{
$node = new BinarySearchNode($data);
if ($this->isEmpty()) {
$this->root = $node;
return true;
} else {
return $this->insertNode($node, $this->root);
}
}
/**
* Check if the tree has any node(s) in it
* @return bool True when empty, False when not empty
*/
public function isEmpty(): bool
{
return is_null($this->root);
}
/**
* Adds a new node to the tree by attaching it to a parent
* @param BinarySearchNode $new_node New node to be added to the tree
* @param BinarySearchNode $current_node The parent of the new node
* @return bool True or False expressing success
*/
private function insertNode(BinarySearchNode $new_node, BinarySearchNode $current_node): bool
{
if ($new_node->data === $current_node->data) {
return false;
}
if ($new_node->data < $current_node->data) {
if (is_null($current_node->left)) {
return $current_node->addChildren($new_node, $current_node->right);
} else {
$current_node = $current_node->left;
return $this->insertNode($new_node, $current_node);
}
} else if ($new_node->data > $current_node->data) {
if (is_null($current_node->right)) {
return $current_node->addChildren($current_node->left, $new_node);
} else {
$current_node = $current_node->right;
return $this->insertNode($new_node, $current_node);
}
}
return true;
}
/**
* Search for Node or value in tree
* @param BinarySearchNode|int $node Node to search for
* @return bool True when node is found, False when otherwise
*/
public function search(BinarySearchNode | int $node): bool
{
if ($this->isEmpty()) return false;
if (gettype($node) !== "object") {
$node = new BinarySearchNode($node);
}
$current_node = $this->root;
if ($node->data === $current_node->data) return true;
else {
return $this->retrieveNode($node, $current_node);
}
}
/**
* @param BinarySearchNode $node
* @param BinarySearchNode $current_node
* @return bool True or False to represent success
*/
private function retrieveNode(BinarySearchNode $node, BinarySearchNode $current_node): bool
{
if ($node->data < $current_node->data) {
if (is_null($current_node->left)) return false;
if ($current_node->left->data === $node->data) return true;
return $this->retrieveNode($node, $current_node->left);
} else if ($node->data > $current_node->data) {
if (is_null($current_node->right)) return false;
if ($current_node->right->data === $node->data) return true;
return $this->retrieveNode($node, $current_node->right);
} else {
return false;
}
}
/**
* Remove a value or Node from the tree
* @param BinarySearchNode|int $node Node or value to remove from the tree
* @return bool True or False to represent status
*/
public function remove(BinarySearchNode | int $node): bool
{
if ($this->isEmpty()) return false;
if (!$this->search($node)) return false;
if (gettype($node) !== "object") {
$node = new BinarySearchNode($node);
}
if ($node->data === $this->root->data) {
return $this->removeRoot();
}
$previous_node = null;
$relationship = null;
$current_node = $this->root;
while (!is_null($current_node)) {
if ($node->data > $current_node->data) {
$previous_node = $current_node;
$relationship = 'right';
$current_node = $current_node->right;
} else if ($node->data < $current_node->data) {
$previous_node = $current_node;
$relationship = 'left';
$current_node = $current_node->left;
} else {
// If the node to be deleted has no children
if (is_null($current_node->left) && is_null($current_node->right)) {
if ($relationship === 'left') {
$previous_node->left = null;
return true;
} else {
$previous_node->right = null;
return true;
}
// If the current node has only a right node
} else if (!is_null($current_node->right) && is_null($current_node->left)) {
if ($relationship === 'left') {
$previous_node->left = $current_node->right;
return true;
} else {
$previous_node->right = $current_node->right;
return true;
}
// If the current node has only a left node
} else if (is_null($current_node->right) && !is_null($current_node->left)) {
if ($relationship === 'left') {
$previous_node->left = $current_node->left;
return true;
} else {
$previous_node->right = $current_node->left;
return true;
}
// If the current node has both left and right nodes
} else {
$successor = $this->getSuccessor($current_node);
$former_left = $current_node->left;
$former_right = $current_node->right;
if ($relationship === 'left') {
$previous_node->left = $successor;
} else {
$previous_node->right = $successor;
}
if ($former_right->data !== $successor->data) {
$successor->right = $former_right;
}
if ($former_left->data !== $successor->data) {
$successor->left = $former_left;
}
}
}
}
return true;
}
private function removeRoot(): true
{
$root = $this->root;
// If the root has no children
if (is_null($root->left) && is_null($root->right)) {
$this->root = null;
return true;
// If the root has only a right node
} else if (is_null($root->left) && !is_null($root->right)) {
$this->root = $root->right;
return true;
// If the root has only a left node
} else if (is_null($root->right) && !is_null($root->left)) {
$this->root = $root->left;
return true;
// If the root has both left and right nodes
} else {
$successor = $this->getSuccessor($root);
$former_left = $root->left;
$former_right = $root->right;
$this->root = $successor;
$this->root->left = $former_left;
$this->root->right = $former_right;
return true;
}
}
private function getSuccessor(BinarySearchNode $current_node): ?BinarySearchNode
{
$successor_parent = $current_node;
$successor = $current_node->right;
while (!is_null($successor->left)) {
$successor_parent = $successor;
$successor = $successor->left;
}
$successor_parent->left = null;
if (!is_null($successor->right)) {
$successor_parent->left = $successor->right;
}
return $successor;
}
public function print(BinarySearchNode $node = null): void
{
if (is_null($node)) {
$node = $this->root;
}
if (!is_null($node->left)) {
$this->print($node->left);
}
echo $node->data . "\n";
if (!is_null($node->right)) {
$this->print($node->right);
}
}
}