douzhiji2020 2018-02-27 06:34
浏览 47
已采纳

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

my rooted directed acyclic trees will consist of logic gates (and,or,xor ...) and nodes.

a node & a gate, each is an object.

only a gate may be a parent.

each gate object has children property as public. children can be array of objects or an empty array.

below is a valid tree for my cases. (g:gate, n:node)

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

my aim

get descendants of a gate in form of array of objects. (ex: print_r($gate1->getDescendants()))

my question

This is my first oop experience, I try to make a small app related to my job. In my code below, I understand that problematic part is: $this->descendants[] = $obj;

if I change this line to $descendants, then the output is still improper because of scope of variable issue.

How can I get $gate1->getDescendants() working properly?

proper output expected

1-dim array of 7 children objects for tree below. (g:gate, n:node)

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

improper output I got

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] => 
        )

)

code: class Node, class Gate, try.php

class Node
{
    public $id;

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

class Gate

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());
  • 写回答

1条回答 默认 最新

  • doujingke4981 2018-02-27 06:54
    关注

    One minor adjustment:

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

    When you call $obj->getDescendants() you aren't using the returned value.

    I am assuming that you want it merged into the descendants variable, as you are requesting a 7-element array response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)