我正在尝试在PHP(nested dataset structure)中实现another description of the structure的面向对象版本。我已经创建了一个节点实现:
class Node
{
private $parent;
private $nodes = [];
private $level = 1;
private $left = 1;
private $right = 2;
/**
* @return self|null
*/
public function getParent()
{
return $this->parent;
}
private function setParent(self $parent = null)
{
$this->parent = $parent;
}
public function getLevel(): int
{
return $this->level;
}
private function setLevel(int $level)
{
$this->level = $level;
}
public function getLeft(): int
{
return $this->left;
}
private function setLeft(int $left)
{
$this->left = $left;
}
public function getRight(): int
{
return $this->right;
}
private function setRight(int $right)
{
$this->right = $right;
}
/**
* @return static[]
*/
public function getNodes(): array
{
return $this->nodes;
}
public function addNode(Node $new)
{
$new->setLevel($this->getLevel() + 1);
$this->nodes[] = $new;
// @todo
}
}
但是我需要在实现上实现帮助 addNode
方法,该方法应在当前节点上添加一个新节点并更新整个树,尤其是新添加的节点,父节点,子节点节点等。
为了使一切变得容易,我创建了一个简单的测试用例,该用例将检查是否正确实现了所有内容:
$country = new Node();
$state = new Node();
$city = new Node();
$country->addNode($state);
$state->addNode($city);
assert($country->getLeft() === 1);
assert($country->getRight() === 6);
assert($country->getLevel() === 1);
assert($state->getLeft() === 2);
assert($state->getRight() === 5);
assert($state->getLevel() === 2);
assert($city->getLeft() === 3);
assert($city->getRight() === 4);
assert($city->getLevel() === 3);
答案 0 :(得分:0)
您不应该定义父级或对其的引用,因为您可能会陷入引用循环中,这在代码中不是一件坏事,但在设计中却可能最终使您的代码无法理解出问题了。
对于addNode
函数,您似乎处在正确的轨道上,
public function addNode(Node $new)
{
//no need for getters and setters from inside the class
$new->level = $this->level + 1;
$this->nodes[] = $new;
return $this;
}
我不确定您要使用left
和right
做什么。但是我认为最上层的父级始终位于左侧,最下层的子级始终位于右侧。在那种情况下,我只是将其与水平区分开来。
我也不会使用private而是使用protected,我在这里主要是这样做的,因此我可以一路向上走,而无需使用getter和setter来使代码紧凑,但是无论如何,您都会感觉到:
class Node
{
/**
* @var static
*/
protected $parent;
/**
* @var static[]
*/
protected $nodes = [];
/**
* @var int
*/
protected $level = 1;
/**
* @var int
*/
protected $left = 0;
/**
* @var int
*/
protected $right = 0;
public function addNode(Node $new)
{
$new->level = $this->level + 1;
$new->parent = $this;
$new->left = $new->level - 1;
if (empty($this->nodes)) {
$this->right = 1;
}
$curr = $this;
while (null !== $curr->parent) {
//walking up to the current parent and telling it there is a new level added
$curr->parent->right++;
//setting the current level 1 up
$curr = $curr->parent;
}
$this->nodes[] = $new;
return $this;
}
}
由于我返回了$this
,因此我可以链接所有内容,或者像这样嵌套addNode调用
$country = new Node();
$state = new Node();
$city = new Node();
$country->addNode($state->addNode($city));
答案 1 :(得分:0)
休息一天后喝了一杯咖啡,终于设法完成了。这并不像看起来那样容易,因为添加新节点时必须重写整个树:
public function addChild(Node $new): void
{
$this->nodes[] = $new;
$new->setParent($this);
$new->setLevel($this->getLevel() + 1);
$new->setLeft($this->getLeft() + 1);
$new->setRight($this->getLeft() + 2);
$rootNode = $this;
while (!empty($rootNode->getParent())) {
$rootNode = $this->getParent();
}
$rootNode->setLeft(1);
$this->updateTree($rootNode);
}
private function updateTree(Node $node): void
{
$startIndex = $node->getLeft();
foreach ($node->getChildren() as $child) {
$child->setLeft(++$startIndex);
$child->setRight(++$startIndex);
if (count($child->getChildren())) {
$this->updateTree($child);
$startIndex = $this->getLastChild($child)->getRight();
}
}
$node->setRight($this->getLastChild($node)->getRight() + 1);
}
private function getLastChild(Node $node): Node
{
return $node->getChildren()[count($node->getChildren()) - 1];
}
该课程即将在GitHub上发布。