开发者

Returning an instance of a node itself in a php tree class

So I'm trying to create a tree structure in PHP. I don't know if that's possible and I'm not all that great with PHP so this is difficult for me.

The code I have so far is (important stuff only, extra code has been cut out):

abstract class tree_node {
    protected $_child_refs = array();

    abstract public function add_child($arg);

    public function count() {
        return count($this->_child_refs);
    }

    public function get_deepest_children() {
        if ($this->count() === 0) {
            return $this;
        } else {
            foreach ($this->_child_refs as $child_ref) {
                $deepest[] = $child_ref->get_deepest_children();
            }
        }
    }

    abstract public function __construct();
}

class data_node extends tree_node {
    private $_data = "";

    public function add_child($data) {
        $new_child = new data_node($data);
        $this->_child_refs[] = $new_child;
    }

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

$foo = new data_node("foo");
$foo->add_child("bar");

var_dump($foo->get_deepest_children());

This code should return a data_node with "bar" as the data but instead I get NULL. What's wrong with "return $this"? Is that not the proper way to return an instance of a class itself?

Als开发者_开发技巧o, feel free to critique this code/tell me I'm doing this completely wrong. I want to keep tree functions separate from functions specific to the data stored in the tree, which is why I split it up into two classes, but if you think that's a bad idea tell me.


This:

public function get_deepest_children() {
    if ($this->count() === 0) {
        return $this;
    } else {
        foreach ($this->_child_refs as $child_ref) {
            $deepest[] = $child_ref->get_deepest_children();
        }
    }
}

Should be something like this:

public function get_deepest_children() {
    if ($this->count() === 0) {
        return array($this);
    }
    $deepest = array();
    foreach ($this->_child_refs as $child_ref) {
        $deepest = array_merge($deepest,$child_ref->get_deepest_children());
    }
    return $deepest;
}


Your get_deepest_children isn't always returning the same type of value. In the base case it will return a node object, in the other cases it extends the $deepest list but doesn't return any value.

<?php
public function get_deepest_children() {
    if ($this->count() === 0) {
        return $this;
    } else {
        foreach ($this->_child_refs as $child_ref) {
            $deepest[] = $child_ref->get_deepest_children();
        }
    }
}

I would modify it like this:

<?php
public function get_deepest_children() {
    if ($this->count() === 0) {
        return Array($this);
    } else {
        $deepest = Array();

        foreach ($this->_child_refs as $child_ref) {
            $deepest = array_merge($deepest, $child_ref->get_deepest_children());
        }

        return $deepest;
    }
}

You should be able to see how it now always it returns an array of all of the deepest nodes (usually called leaf nodes) below/including the current one. This way we can array_merge the consecutive results to get the final ones.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜