@ 2013-05-07T05:45:18Z <?php
class Node
{
public $color;
public $key;
public $value;
public $left;
public $right;
public function __construct($key, $value, $color)
{
$this->key = $key;
$this->value = $value;
$this->color = $color;
}
public function compare($key)
{
if ($this->key < $key) return -1;
elseif ($this->key > $key) return 1;
else return 0;
}
}
class RBTree
{
const RED = true;
const BLACK = false;
private $root;
public function isEmpty()
{
return $this->root == null;
}
public function put($key, $value)
{
$this->root = $this->insert($this->root, (int)$key, $value);
$this->root->color = self::BLACK;
}
public function get($key)
{
$x = $this->search($this->root, (int)$key);
return $x ? $x->value : null;
}
private function isRed($h)
{
return $h ? $h->color : false;
}
private function insert($x, $key, $value)
{
if (!$x) return new Node($key, $value, self::RED);
$cmp = $x->compare($key);
if ($cmp > 0) $x->right = insert($x->right, $key, $value);
elseif ($cmp < 0) $x->left = insert($x->left, $key, $value);
else $x->value = $value;
if ($this->isRed($x->right) && !$this->isRed($x->left)) $x = $this->rotateLeft($x);
if ($this->isRed($x->left) && $this->isRed($x->left->left)) $x = $this->rotateRight($x);
if ($this->isRed($x->left) && $this->isRed($x->right)) $this->flipColors($x);
return $x;
}
private function search($x, $key)
{
while ($x) {
$cmp = $x->compare($key);
if ($cmp > 0) $x = $x->right;
elseif ($cmp < 0) $x = $x->left;
else return $x;
}
return null;
}
private function flipColors($h)
{
$h->color = !$h.color;
$h->left->color = !h->left->color;
$h->right->color = $h->right->color;
}
private function rotateLeft($h)
{
$x = $h->right;
$h->right = $x->left;
$x->left = $h;
$x->color = $x->left->color;
$x->left->color = self::RED;
return $x;
}
private function rotateRight($h)
{
$x = $h->left;
$h->left = $x->right;
$x->right = $h;
$x->color = $x->right->color;
$x->right->color = self::RED;
return $x;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.14 0.010 0.067 16.35 5.4.13 0.010 0.050 16.44 5.4.12 0.010 0.057 16.34 5.4.11 0.013 0.063 16.54 5.4.10 0.007 0.033 16.37 5.4.9 0.003 0.067 16.43 5.4.8 0.013 0.067 16.44 5.4.7 0.007 0.073 16.44 5.4.6 0.003 0.080 16.42 5.4.5 0.007 0.053 16.38 5.4.4 0.007 0.077 16.43 5.4.3 0.007 0.073 16.46 5.4.2 0.007 0.070 16.36 5.4.1 0.007 0.040 16.39 5.4.0 0.003 0.067 15.74 5.3.24 0.003 0.040 14.63 5.3.23 0.007 0.040 14.73 5.3.22 0.007 0.073 14.45 5.3.21 0.007 0.063 14.65 5.3.20 0.003 0.040 14.45 5.3.19 0.007 0.050 14.56 5.3.18 0.003 0.047 14.67 5.3.17 0.013 0.070 14.82 5.3.16 0.003 0.067 14.60 5.3.15 0.003 0.077 14.67 5.3.14 0.013 0.070 14.64 5.3.13 0.007 0.077 14.66 5.3.12 0.003 0.057 14.63 5.3.11 0.010 0.050 14.80 5.3.10 0.007 0.077 14.11 5.3.9 0.013 0.057 14.27 5.3.8 0.013 0.073 13.87 5.3.7 0.003 0.060 14.09 5.3.6 0.007 0.070 14.06 5.3.5 0.003 0.047 14.01 5.3.4 0.000 0.067 14.09 5.3.3 0.007 0.057 14.16 5.3.2 0.000 0.047 13.74 5.3.1 0.013 0.063 13.69 5.3.0 0.010 0.063 13.71
preferences:dark mode live preview
135 ms | 1394 KiB | 7 Q