0

私のルート化された有向非巡回ツリーは、論理ゲート(および、または、xor ...) とノードで構成されます。

ノードゲート、それぞれがオブジェクトです。

ゲートだけが親になることができます。

ゲートオブジェクトには、 publicとしてchildrenプロパティがあります。childrenは、オブジェクトの配列または空の配列にすることができます。

以下は私の場合の有効なツリーです。(g:ゲート、n:ノード)

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

私の目的

オブジェクトの配列の形式でゲートの子孫を取得します。(例: print_r($gate1->getDescendants()))

私の質問

これは私の最初の oop 体験です。仕事に関連する小さなアプリを作成しようとしています。以下の私のコードでは、問題のある部分が次のとおりであることを理解しています。$this->descendants[] = $obj;

この行を に変更して$descendantsも、変数のスコープの問題により、出力は依然として不適切です。

どうすれば$gate1->getDescendants()正常に動作するようになりますか?

適切な出力が期待される

以下のツリーの 7 つの子オブジェクトの 1 次元配列。(g:ゲート、n:ノード)

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());
4

1 に答える 1

1

1 つのマイナーな調整:

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

あなたが呼び出すとき、あなた$obj->getDescendants()は戻り値を使用していません。

7 要素の配列応答を要求しているため、子孫変数にマージすることを想定しています。

于 2018-02-27T06:54:08.883 に答える