Php tree type instance

Source: Internet
Author: User
This article mainly introduces the php tree type, which involves the tree structure of data structures and algorithms. the instances are relatively simple and easy to understand. it has some reference value for learning data structures, for more information about php tree classes, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

The principle of this instance is simple. after learning the data structure, you can see what it means. However, when using the data, the subnode id (71) appears) less than the parent node id (104 ). as a result, the part of the node is not stored in the array. the instance code is modified as follows:

The code is as follows:

<? Php
Class tree
{
Var $ data = array ();
Var $ child = array (-1 => array ());
Var $ layer = array (-1 =>-1 );
Var $ parent = array ();
Var $ num = array ();

Function setnode ($ id, $ parent, $ value, $ num = 0)
{
$ Parent = $ parent? $ Parent: 0;

$ This-> data [$ id] = $ value;
$ This-> num [$ id] = $ num;
If (! Isset ($ this-> child [$ id]) $ this-> child [$ id] = array ();
$ This-> child [$ parent] [] = $ id;
$ This-> parent [$ id] = $ parent;

If (! Isset ($ this-> layer [$ parent]) & $ parent = 0)
{
$ This-> layer [$ id] = 0;
}
Else
{
$ This-> layer [$ id] = $ this-> layer [$ parent] + 1;
}
}

Function getlist (& $ tree, $ root = 0)
{
Foreach ($ this-> child [$ root] as $ key => $ id)
{
$ Tree [] = $ id;
If ($ this-> child [$ id]) $ this-> getlist ($ tree, $ id );
}
}

Function getvalue ($ id)
{
If ($ this-> layer [$ id] = 0)
{
Return $ this-> data [$ id];
}
Else
{
Return $ leftmar. $ this-> data [$ id];
}
}

Function getnum ($ id)
{
Return $ this-> num [$ id];
}

Function getbitvalue ($ id)
{
Return $ this-> data [$ id];
}

Function getlayer ($ id, $ space = false)
{
Return $ space? Str_repeat ($ space, $ this-> layer [$ id]): $ this-> layer [$ id];
}

Function getparent ($ id)
{
Return $ this-> parent [$ id];
}

Function getparents ($ id)
{
While ($ this-> parent [$ id]! =-1)
{
$ Id = $ parent [$ this-> layer [$ id] = $ this-> parent [$ id];
}

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

Return $ parent;
}

Function getchild ($ id)
{
Return $ this-> child [$ id];
}

Function getchilds ($ id = 0)
{
$ Child = array ($ id );
$ This-> getlist ($ child, $ id );

Return $ child;
}

Function printdata ()
{
Return $ this-> layer;
}
}
?>

I hope this article will help you with PHP programming.

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.