Repository navigation
Expand file tree
/
Copy pathBSTNode.php
More file actions
63 lines (49 loc) · 1.5 KB
/
Copy pathBSTNode.php
File metadata and controls
63 lines (49 loc) · 1.5 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
<?php
declare(strict_types=1);
/*
* 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;
class BSTNode
{
public ?BSTNode $left = null;
public ?BSTNode $right = null;
public ?BSTNode $parent = null;
/**
* @param int $key The key of the node.
* @param mixed $value The associated value.
*/
public function __construct(public int $key, public mixed $value)
{
}
public function isRoot(): bool
{
return !$this->parent instanceof \DataStructures\BinarySearchTree\BSTNode;
}
public function isLeaf(): bool
{
return !$this->left instanceof \DataStructures\BinarySearchTree\BSTNode && !$this->right instanceof \DataStructures\BinarySearchTree\BSTNode;
}
public function getChildren(): array
{
if ($this->isLeaf()) {
return [];
}
$children = [];
if ($this->left instanceof \DataStructures\BinarySearchTree\BSTNode) {
$children['left'] = $this->left;
}
if ($this->right instanceof \DataStructures\BinarySearchTree\BSTNode) {
$children['right'] = $this->right;
}
return $children;
}
public function getChildrenCount(): int
{
return count($this->getChildren());
}
}