<?php
namespace DataStructures\BinarySearchTree;
class BSTNode
{
public int $key;
public $value;
public ?BSTNode $left;
public ?BSTNode $right;
public ?BSTNode $parent;
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());
}
}