将父门的后代作为有根有向无环树中的子对象(节点和门)的 1-dim 数组获取



我的扎根的无循环树将由逻辑 GATES> GATES (and,or,or,or,XOR ...(和 nodes组成

a 节点&a gate ,每个都是一个对象。

只有门可以是父母。

每个 gate 对象具有儿童 public 的属性。儿童可以是对象的数组或一个空数组。

以下是我的情况的有效树。(G:GATE,N:NODE(

g1
|---|---|---|---|
n1  n2  n3  g2  g3
            |
           n4 n5

我的目标

以对象数组的形式获得门的后代。(例如:print_r($gate1->getDescendants())(

我的问题

这是我的第一次OOP体验,我尝试制作一个与工作相关的小应用程序。在下面的代码中,我知道有问题的部分是:$this->descendants[] = $obj;

如果我将此行更改为$descendants,则由于可变问题的范围,输出仍然不当。

如何使$gate1->getDescendants()正常工作?

正确的输出预期

1个儿童对象的1 dim阵列下面的树。(G:GATE,N:NODE(

g1
|---|---|---|---|
n1  n2  n3  g2  g3
            |
           n4 n5

不当输出我得到

Array
(
    [0] => Node Object
        (
            [id] => 1
        )
    [1] => Node Object
        (
            [id] => 2
        )
    [2] => Node Object
        (
            [id] => 3
        )
    [3] => Gate Object
        (
            [id] => 2
            [type] => or
            [desc] => My First OR Gate
            [children] => Array
                (
                    [0] => Node Object
                        (
                            [id] => 4
                        )
                    [1] => Node Object
                        (
                            [id] => 5
                        )
                )
            [descendants] => Array
                (
                    [0] => Node Object
                        (
                            [id] => 4
                        )
                    [1] => Node Object
                        (
                            [id] => 5
                        )
                )
        )
    [4] => Gate Object
        (
            [id] => 3
            [type] => xor
            [desc] => My First XOR Gate
            [children] => Array
                (
                )
            [descendants] => 
        )
)

代码:类节点,类门,try.php

class Node
{
    public $id;
    public function __construct($id)
    {
        $this->id = $id;
    }
}

类门

class Gate
{
    public $id;
    public $type;
    public $desc;
    public $children = array();
    public $descendants;
    public function __construct($id, $type, $desc)
    {
        $this->id = $id;
        $this->type = $type;
        $this->desc = $desc;            
    }
    public function addChild($child)
    {
        if($child instanceof Node OR $child instanceof Gate)
        {
            $this->children[] = $child;
        }
        else
        {               
            throw new Exception('Child of Gate must be a Node or Gate object!');
        }
    }
    public function getDescendants()
    {           
        if(!empty($this->children))
        {
            $count_children = count($this->children);
            for ($i = 0; $i < $count_children; $i++) 
            {
                $obj = $this->children[$i];
                $this->descendants[] = $obj;
                // i tried also below
                // $descendants[] = $obj;
                if($obj instanceof Gate)
                {                           
                    $obj->getDescendants();                         
                }
            }
            return $this->descendants;
            // i tried also below
            //return $descendants;
        }
        else
        {
            return $this->children;
        }
    }
}

try.php

require_once('Node.php');
require_once('Gate.php');
$node1 = new Node(1);   
$node2 = new Node(2);   
$node3 = new Node(3);
$node4 = new Node(4);   
$node5 = new Node(5);   
$gate1 = new Gate(1,'and','My First AND Gate'); 
$gate2 = new Gate(2,'or','My First OR Gate');
$gate3 = new Gate(3,'xor','My First XOR Gate');
$gate1->addChild($node1);
$gate1->addChild($node2);
$gate1->addChild($node3);
$gate1->addChild($gate2);
$gate1->addChild($gate3);
$gate2->addChild($node4);
$gate2->addChild($node5);
function pa($var)
{
    echo '<pre>';print_r($var);echo '</pre>';
}
/**
 * get top gate's descandants
 * (not only 1st level,
 * but children @all levels)
 */
pa($gate1->getDescendants());

一个次要调整:

if($obj instanceof Gate)
{
    $this->descendants = array_merge($this->descendants, $obj->getDescendants());
}

当您致电$obj->getDescendants()时,您不使用返回的值。

我假设您希望它合并到后代变量中,因为您请求7元素响应。

相关内容

  • 没有找到相关文章

最新更新