【问题标题】:Building a Node class with PHP使用 PHP 构建 Node 类
【发布时间】:2013-03-14 05:43:04
【问题描述】:

我需要构建一个 Node 类,它代表一棵节点树,所有这些节点都保存在一个数组中。我知道伪代码和 C++ 的结构,但我认为 PHP 中最好的结构?

类似这样的:

class Node 
{
 //number of nodes from 0 to n
}

【问题讨论】:

  • 为什么不简单地使用数组?
  • 您应该稍微扩展您的帖子。你知道的 C++ 结构是什么?为什么不能将其转换为 PHP?会出现什么问题?
  • 是的,简而言之。在尝试写任何愚蠢的东西之前,请检查 spl:php.net/manual/en/book.spl.php
  • 如何使用数组制作它?

标签: php class tree nodes


【解决方案1】:

我认为你应该考虑Ardent 它有很好的Binary Tree 实现并且还支持BinarySearchTree

示例:

    $root = new BinaryTree(7);
    $root->setLeft(new BinaryTree(5));
    $root->setRight(new BinaryTree(8));
    $root->getLeft()->setLeft(new BinaryTree(1));
    $root->getLeft()->setRight(new BinaryTree(6));
    $root->getRight()->setRight(new BinaryTree(9));

您还可以拥有扩展 BinarySearchTree 的 AvlTree

$object = new AvlTree();
    //          5
    //        /    \
    //       2      9
    //     /  \    / \
    //    1    4  8  11
    //        /
    //       3
    $object->add(5);
    $object->add(2);
    $object->add(9);
    $object->add(1);
    $object->add(4);
    $object->add(8);
    $object->add(11);
    $object->add(3);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2015-11-20
    • 2016-03-16
    • 1970-01-01
    • 2013-11-10
    • 2014-03-20
    • 2012-05-11
    相关资源
    最近更新 更多