forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTNode.php
66 lines (58 loc) · 1.44 KB
/
BSTNode.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
<?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;
class BSTNode
{
public int $key;
/**
* @var mixed
*/
public $value;
public ?BSTNode $left;
public ?BSTNode $right;
public ?BSTNode $parent;
/**
* @param int $key The key of the node.
* @param mixed $value The associated value.
*/
public function __construct(int $key, $value)
{
$this->key = $key;
$this->value = $value;
$this->left = null;
$this->right = null;
$this->parent = null;
}
public function isRoot(): bool
{
return $this->parent === null;
}
public function isLeaf(): bool
{
return $this->left === null && $this->right === null;
}
public function getChildren(): array
{
if ($this->isLeaf()) {
return [];
}
$children = [];
if ($this->left !== null) {
$children['left'] = $this->left;
}
if ($this->right !== null) {
$children['right'] = $this->right;
}
return $children;
}
public function getChildrenCount(): int
{
return count($this->getChildren());
}
}