Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions DataStructures/RedBlackTree/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace DataStructures;

/**
* Internal Node class representing a single node in the Red-Black Tree
*
* @internal
*/
class Node
{
const RED = 'RED';
const BLACK = 'BLACK';

public $value;
public $color;
public $left;
public $right;
public $parent;

/**
* Node constructor
*
* @param mixed $value
* @param string $color
* @param Node|null $parent
*/
public function __construct($value, $color = self::RED, $parent = null)
{
$this->value = $value;
$this->color = $color;
$this->parent = $parent;
$this->left = null;
$this->right = null;
}

public function isRed(): bool
{
return $this->color === self::RED;
}

public function isBlack(): bool
{
return $this->color === self::BLACK;
}

public function sibling(): ?Node
{
if ($this->parent === null) return null;
return $this->parent->left === $this ? $this->parent->right : $this->parent->left;
}

public function isLeftChild(): bool
{
return $this->parent !== null && $this->parent->left === $this;
}

public function isRightChild(): bool
{
return $this->parent !== null && $this->parent->right === $this;
}

public function hasRedChild(): bool
{
return ($this->left !== null && $this->left->isRed()) ||
($this->right !== null && $this->right->isRed());
}
}
Loading