PHP Unlimited classification of tree Class code _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
by Lenush;
*/
Class Tree
{
var $data = array ();
var $child = array ( -1=>array ());
var $layer = array ( -1=>-1);
var $parent = array ();

function Tree ($value)
{
$this->setnode (0,-1, $value);
}//End Func

function Setnode ($id, $parent, $value)
{
$parent = $parent? $parent: 0;

$this->data[$id] = $value;
$this->child[$id] = array ();
$this->child[$parent] [] = $id;
$this->parent[$id] = $parent;

if (!isset ($this->layer[$parent]))
{
$this->layer[$id] = 0;
}
Else
{
$this->layer[$id] = $this->layer[$parent] + 1;
}
}//End Func

Function GetList (& $tree, $root = 0)
{
foreach ($this->child[$root] as $key + = $id)
{
$tree [] = $id;

if ($this->child[$id]) $this->getlist ($tree, $id);
}
}//End Func


function GetValue ($id)
{
return $this->data[$id];
}//End Func


function Getlayer ($id, $space = False)
{
Return $space str_repeat ($space, $this->layer[$id]): $this->layer[$id];
}//End Func

function GetParent ($id)
{
return $this->parent[$id];
}//End Func

function Getparents ($id)
{
while ($this->parent[$id]! =-1)
{
$id = $parent [$this->layer[$id]] = $this->parent[$id];
}

Ksort ($parent);
Reset ($parent);

return $parent;
}//End Func

function Getchild ($id)
{
return $this->child[$id];
}//End Func

function Getchilds ($id = 0)
{
$child = Array ($id);
$this->getlist ($child, $id);

return $child;
}//End Func
}//End Class


New Tree (the name of the root directory);
The ID of the root directory is automatically assigned as 0
$Tree = new Tree (' Directory navigation ');

Setnode (directory ID, ancestor ID, directory name);
$Tree->setnode (1, 0, ' directory 1 ');
$Tree->setnode (2, 1, ' Directory 2 ');
$Tree->setnode (3, 0, ' Directory 3 ');
$Tree->setnode (4, 3, ' directory 3.1 ');
$Tree->setnode (5, 3, ' Directory 3.2 ');
$Tree->setnode (6, 3, ' directory 3.3 ');
$Tree->setnode (7, 2, ' Directory 2.1 ');
$Tree->setnode (8, 2, ' directory 2.2 ');
$Tree->setnode (9, 2, ' directory 2.3 ');
$Tree->setnode (10, 6, ' Directory 3.3.1 ');
$Tree->setnode (11, 6, ' directory 3.3.2 ');
$Tree->setnode (12, 6, ' Directory 3.3.3 ');

Getchilds (specify directory ID);
Gets the specified directory subordinate directory. If no directory is specified, start with the root directory
$category = $Tree->getchilds ();

Traverse output
foreach ($category as $key = $id)
{
echo $Tree->getlayer ($id, ' | '). $Tree->getvalue ($id). "
\ n ";
}
?>

http://www.bkjia.com/PHPjc/320977.html www.bkjia.com true http://www.bkjia.com/PHPjc/320977.html techarticle Copy the code as follows: Php/** by Lenush; */class Tree {var $data = array (), var $child = array ( -1=array ()), var $layer = array ( -1=-1); var $parent = array (); function Tree ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.