-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathBSTree.php
378 lines (331 loc) · 10.7 KB
/
BSTree.php
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #174
* https://github.com/TheAlgorithms/PHP/pull/174
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures\BinarySearchTree;
require_once 'BinaryTreeTraversal.php';
class BSTree extends BinaryTreeTraversal
{
private ?BSTNode $root = null;
private int $counter = 0;
/**
* Optionally provide an array of key-value pairs to build the binary search tree from.
* Optionally set the traversal algorithm for the Iterator interface.
* Otherwise, with manual insert() and setTraversalType() after initialization.
*/
public function __construct(array $initialData = [], string $traversalType = parent::IN_ORDER)
{
foreach ($initialData as $key => $value) {
$this->insert($key, $value); // Build the tree from an array of key-value pairs
}
parent::setTraversalType($traversalType);
}
/**
* Get the root of the Splay Tree.
*
* @return BSTNode|NULL The root node.
*/
public function getRoot(): ?BSTNode
{
return $this->root;
}
public function size(): int
{
return $this->counter;
}
public function isEmpty(): bool
{
return $this->root === null;
}
/**
* Inserts a new node into binary search tree and updates parent reference.
*
* Time complexity: O(log n) for binary search tree insertion.
*/
public function insert(int $key, $value): ?BSTNode
{
$this->insertNode($this->root, $key, $value);
$this->counter++;
return $this->root;
}
private function insertNode(?BSTNode &$rootPtr, int $key, $value): void
{
if ($rootPtr === null) {
$rootPtr = new BSTNode($key, $value);
return;
}
if ($key < $rootPtr->key) {
$this->insertNode($rootPtr->left, $key, $value);
$rootPtr->left->parent = $rootPtr;
} elseif ($key > $rootPtr->key) {
$this->insertNode($rootPtr->right, $key, $value);
$rootPtr->right->parent = $rootPtr;
} else {
throw new DuplicateKeyException($key);
}
}
/**
* Removes and isolates an existing node from the BST and update structure references.
*
* Time complexity: O(log n) for binary search tree node removal.
*/
public function remove(int $key): ?BSTNode
{
$discardedNode = $this->removeNode($this->root, $key);
if ($discardedNode !== null) {
$this->counter--;
}
return $discardedNode;
}
private function removeNode(?BSTNode &$rootPtr, int $key): ?BSTNode
{
if ($rootPtr === null) {
return null;
}
if ($key < $rootPtr->key) {
$discardedNode = $this->removeNode($rootPtr->left, $key);
} elseif ($key > $rootPtr->key) {
$discardedNode = $this->removeNode($rootPtr->right, $key);
} else {
// Key found, proceed to delete
$discardedNode = $rootPtr->getChildrenCount() === 2
? $this->handleNodeWithTwoChildren($rootPtr)
: $this->handleNodeWithSingleOrZeroChild($rootPtr);
}
return $discardedNode;
}
/**
* handle deletion when found node has 2 children.
*/
private function handleNodeWithTwoChildren(BSTNode $rootPtr): ?BSTNode
{
$minRightNode = $this->minNode($rootPtr->right);
$discarded = clone $rootPtr;
$rootPtr->key = $minRightNode->key;
$rootPtr->value = $minRightNode->value;
$this->removeNode($rootPtr->right, $minRightNode->key);
// Isolate the target node
$discarded->left = null;
$discarded->right = null;
$discarded->parent = null;
return $discarded;
}
/**
* Handle deletion when found node has 1 or 0 child.
*/
private function handleNodeWithSingleOrZeroChild(BSTNode &$rootPtr): ?BSTNode
{
$discard = $rootPtr;
if ($discard->isLeaf()) { // Case 1: Node to be removed is a leaf node
$rootPtr = null;
} elseif ($rootPtr->left != null) { // Case 2: Node has only a left child
$rootPtr = $rootPtr->left;
$discard->left = null;
} else { // Case 3: Node has only a right child
$rootPtr = $rootPtr->right;
$discard->right = null;
}
// Update the parent reference for the new child node
if ($rootPtr !== null) {
$rootPtr->parent = $discard->parent;
}
// Unlink the discarded node from its parent
$discard->parent = null;
return $discard;
}
/**
* Return the minimum node in the BST.
*/
public function minNode(?BSTNode $node): ?BSTNode
{
if ($node === null) {
return null;
}
return $node->left === null
? $node
: $this->minNode($node->left);
}
/**
* Search for a node by its key and return the node, otherwise Null.
*
* Time complexity: O(log n) for the splay operation.
*/
public function search(int $key): ?BSTNode
{
if ($this->isEmpty()) {
return null;
}
return $this->searchNode($this->root, $key);
}
private function searchNode(?BSTNode $node, int $key): ?BSTNode
{
if ($node === null) {
return null;
}
if ($key === $node->key) {
return $node;
} elseif ($key < $node->key) {
return $this->searchNode($node->left, $key);
} else {
return $this->searchNode($node->right, $key);
}
}
/**
* Check if a node with the given key exists in the BST.
*
* Time complexity: O(log n) for the splay operation.
*
* @param BSTNode|null $node The root of the (sub)tree to start propagating from.
* @param int $key The key of the node being searched.
*/
public function isFound(?BSTNode $node, int $key): bool
{
if ($node === null) {
return false;
}
if ($key === $node->key) {
return true;
} elseif ($key < $node->key) {
return $this->isFound($node->left, $key);
} else {
return $this->isFound($node->right, $key);
}
}
/**
* Get the depth of the given node relative to the root of the tree.
* Traverses from the given node up to the root of the tree by recursively following the parent references
*
* Time complexity: O(d_p) where d_p: depth of node p in tree
*
* @param BSTNode $node The node whose depth is to be calculated.
* @return int The depth of the node relative to the root.
*/
public function getDepth(BSTNode $node): int
{
return $node->parent === null
? 0
: 1 + $this->getDepth($node->parent);
}
/**
* Get the height of the given node relative to the farthest leaf node.
* Recursively visits all children of the given node determining the maximum height of its subtrees
*
* Time Complexity: O(n)
*
* @param BSTNode $node The node whose height is to be calculated.
* @return int The height of the node relative to its farthest descendant.
*/
public function getHeight(BSTNode $node): int
{
if ($node->isLeaf()) {
return 0;
}
$height = 0;
$childrenList = $node->getChildren();
foreach ($childrenList as $childNode) {
$height = max($height, $this->getHeight($childNode));
}
return 1 + $height;
}
/**
* Returns a flat associative array structure for inOrder Traversal
*/
public function inOrderTraversal(): array
{
return $this->inOrder($this->root);
}
/**
* Returns a flat associative array structure for preOrder Traversal
*/
public function preOrderTraversal(): array
{
return $this->preOrder($this->root);
}
/**
* Returns a flat associative array structure for postOrder Traversal
*/
public function postOrderTraversal(): array
{
return $this->postOrder($this->root);
}
/**
* Returns a flat associative array structure for Breadth-first Traversal
*/
public function breadthFirstTraversal(): array
{
return $this->breadthFirst($this->root);
}
/**
* Serializes the BST into a JSON string.
*
* @return string The BST as a JSON string.
*/
public function serialize(): string
{
return json_encode($this->serializeTree($this->root));
}
/**
* Recursively serializes the Binary Search Tree.
*
* @param BSTNode|null $node The current node to serialize from.
* @return array The serialized representation of the node.
*/
private function serializeTree(?BSTNode $node): array
{
if ($node === null) {
return [];
}
return [
'key' => $node->key,
'value' => $node->value,
'left' => $this->serializeTree($node->left),
'right' => $this->serializeTree($node->right),
];
}
/**
* Deserializes a JSON string into a BST object.
*
* @param string $data The JSON string to deserialize.
* @return BSTree The deserialized Binary Search Tree.
*/
public function deserialize(string $data): BSTree
{
$arrayRepresentation = json_decode($data, true);
$binarySearchTree = new self();
$binarySearchTree->root = $binarySearchTree->deserializeTree($arrayRepresentation, null);
$binarySearchTree->updateNodeCount($binarySearchTree->root);
return $binarySearchTree;
}
/**
* Recursively deserializes a BST from an array representation.
*
* @param array $data The serialized data for the node.
* @return BSTNode|null The deserialized node.
*/
private function deserializeTree(array $data, ?BSTNode $parent): ?BSTNode
{
if (empty($data)) {
return null;
}
$node = new BSTNode($data['key'], $data['value']);
$node->parent = $parent;
$node->left = $this->deserializeTree($data['left'], $node);
$node->right = $this->deserializeTree($data['right'], $node);
return $node;
}
/**
* Recursively updates the BST size after deserialization.
*/
private function updateNodeCount(?BSTNode $node): void
{
if ($node !== null) {
$this->counter++;
$this->updateNodeCount($node->left);
$this->updateNodeCount($node->right);
}
}
}