@ 2015-11-29T15:04:35Z <?php
class CategoryTree
{
protected $roots = [];
public function addCategory($category, $parent)
{
if ($this->categoryExists($this->roots, $category) || !$this->categoryExists($this->roots, $parent)) {
throw new InvalidArgumentException();
}
if ($parent === null) {
$this->tree[$category] = []
}
$node = $findNode($parent, $this->root);
if(!isset($node)) {
throw new InvalidArgumentException();
}
$node[$category] = [];
}
public function getChildren($parent)
{
$node = $findNode($parent, $this->root);
if(!isset($node)) {
throw new InvalidArgumentException();
}
return array_keys($node);
}
public function findNode($node, &$root) {
if (in_array($node, array_keys($root))) {
return $root[$node];
}
foreach ($root as $n) {
return $this->findNode($node, $n);
}
}
public function categoryExists($nodes, $category) {
$exists = false;
foreach($node as $node) {
if (in_array($category, $this->getChildren($node))) {
return true;
}
$exists = $this->categoryExists($node, $category);
}
return $exists;
}
public function add(&$node, $category, $parent) {
if (in_array($parent, $node)) {
return
}
}
}
// For testing purposes (do not submit uncommented):
$c = new CategoryTree;
$c->addCategory('A', null);
$c->addCategory('B', 'A');
$c->addCategory('C', 'A');
//echo implode(',', $c->getChildren('A'));
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.6.16 0.010 0.077 20.52 5.6.15 0.000 0.087 18.18 5.6.14 0.003 0.040 18.15 5.6.13 0.013 0.033 18.24 5.6.12 0.013 0.040 20.97 5.6.11 0.020 0.060 21.12 5.6.10 0.020 0.073 20.96 5.6.9 0.023 0.073 21.04 5.6.8 0.007 0.073 20.44 5.5.30 0.010 0.077 17.95 5.5.29 0.003 0.047 18.07 5.5.28 0.000 0.043 20.77 5.5.27 0.000 0.043 20.80 5.5.26 0.000 0.053 20.80 5.5.25 0.013 0.047 20.77 5.5.24 0.013 0.063 20.35
preferences:dark mode live preview
144.85 ms | 1394 KiB | 7 Q