PHP unordered Tree Implementation Method _php skill

Source: Internet
Author: User

This article illustrates the implementation method of the PHP unordered tree. Share to everyone for your reference. Specifically as follows:

The effect is as shown in the following illustration:

The PHP code is as follows:

<?php/* Write in PHP unordered tree/class unorderedtree{//Node ID counter protected $nodeId = 0;
 The depth of the tree is protected $depth = 0;
 Number of nodes in the tree, protected $nodesCount = 0;
 The degree @todo of the tree: make it play a role public $degree = "to be implent";
 root Node ID//Because the tree has a variety of operations from the root node, do not want to traverse the tree to the top to find root, with a variable always point to the root node protected $rootid =null; Child node collection, k-v for nodeid=> (stdclass) node//Some tree implementations are often implemented using nodes and trees of the same class, where nodes are used stdclass{data, parent, ID, childrenids}, because I  Think that nodes and trees should be two objects, and stdclass should be lighter than the tree class//Node format description: $this->nodes[nodeid] = new stdclass{ID, ParentID, childrenids, data}// ID: Node ID//PARENTID: Node Parent ID//Childrenids: child node ID do not want to traverse the tree to determine the hierarchy//Note: node, #只保存其自身数据和其子节点id的集合 #, child node data by $tree from the tree
 ->nodes[$node->childrenids[a_child_id]] Access//Data: A node contains attributes such as attribute data such as node names protected $nodes =array ();
 User-defined Access node protected $userVisitFunction =null; /* Grouping: basic function of class//@todo: Construct tree Public Function __construct () {}//@todo: Destroy Tree Public Function __destruct () {unset (
 $this->nodes);
 }//------------get the data class function--------------- Gets the depth of the tree, Public function gettreedepth () {return $this->depth;
  }//Get the number of nodes of the tree public Function GetCount () {return $this->nodescount;
  }//Get the degree public Function Getdegree () {//@todo of the tree: Get the degree of the tree (because the degree of alignment is not needed for a while) return $this->degree; }//Get the specified node public function getnode ($nodeId) {if (Isset ($this->nodes[$nodeId))) {return $this->nodes[$nodeId]
  ;
  } else{return false;
  }//Get Latest ID Public Function getId () {return $this->nodeid; }//Get the specified node height public function getnodeheight ($nodeId) {if (Array_key_exists ($nodeId, $this->nodes)) {//This node is already in the tree
   , a height of at least 1, each found a parent node +1 $height = 1;
   Records the nodes that have been visited in this tree to prevent the node from interacting with each other when they are constructed to cause this function to die and to end the lookup $visitedNodesIds =array ();
   Records the ID $cid of the current operation node = $nodeId; The parent node of the current node must exist in this tree/without recursion while (Isset ($cid)) {if (!in_array ($cid, $visitedNodesIds)) {if ($this->r
     ootid=== $cid) {//top, return $height;
     } $visitedNodesIds []= $cid; $cid = $this->nodes[$cid]->parentid; 
    $height + +;
    } else{return false;
  return false;
  } else{return false; }//Get root node Public function getroot () {return (!is_null ($this->rootid)) && $this->nodes[$this->r
  Ootid]; //Gets an array of the specified node and all its child nodes//This is a key underlying operation to get the subtree public function getsubnodes ($nodeId) {if isset ($this->nodes[$nodeId
   ]) {$result =array ();
   $toVisitNodeIds =array (); 
   $toVisitedNodeIds []= $nodeId;
   $result []= $this->nodes[$nodeId]->id; 
   Array_shift ($TOVISITEDNODEIDS);
   $toVisitedNodeIds =array_merge ($toVisitedNodeIds, $this->nodes[$nodeId]->childrenids);
    while (!empty ($toVisitedNodeIds)) {$toVisitNodeId =array_shift ($toVisitedNodeIds);
    $result []= $this->nodes[$toVisitNodeId]->id;
   $toVisitedNodeIds =array_merge ($toVisitedNodeIds, $this->nodes[$toVisitNodeId]->childrenids);
  return $result;
  } else{return false; }//@todo: Gets the subtree constructed by the specified node and all its child nodes PubliC function Getsubtree ($nodeid) {}//----------------Data Update-----------------public function setId ($nodeId) {$this
   ->nodeid= $nodeId;
  return $this;
  //Create a new ID that is not duplicated (unused in the tree) public Function Seekid () {$this->nodeid++;
  return $this->nodeid;
  The Public Function setvisitfunction ($userFunction) {$this->uservisitfunction= $userFunction; ///Insert child nodes, default to the Public function Insertnode ($parent _id=null, $data =null) inserted under the root node {//Note node is not class tree $node = new STD 
  Class
  $node->data = $data;
  The number of nodes in the tree increases $this->nodecount++;
  Assign Node ID $this->seekid ();
  $node->id = $this->getid ();
   Insert root node if ((Is_null ($parent _id)) && is_null ($this->rootid)) {$node->parentid = null;
   $node->childrenids = Array (); 
   $this->depth=1;
   $this->rootid= $node->id;
   $this->nodes [$node->id]= $node;
  return $this; } elseif (Isset ($this->nodes[$parent _id]) | | is_null ($parent _id)) {//inserted under existing node in this tree if (Is_null ($parent _ID) {$parent _id= $this->rootid;
   } $node->parentid = $parent _id;
   $node->childrenids = Array ();
   Update the maximum depth of the tree $depth = $this->getnodeheight ($parent _id);
   $this->depth=max ($depth +1, $this->depth);
   $this->nodes[$parent _id]->childrenids []= $node->id;
   $this->nodes [$node->id]= $node;
  return $this; 
  else{return $this; //insert node alias public function append ($parent _id=null, $data =null) {return $this->insertnode ($parent _i
  D, $data); }//---------------data access-----//Breadth First traverse the alias of the node, the full name is too long. Public Function B ($nodeId =null) {return $this->breadthtrav
  Ersal ($NODEID);
  }//breadth-first traversal node public function breadthtraversal ($nodeId =null) {if is_null ($this->rootid)) {die ("This tree is empty tree, inaccessible"); else{//All Traversal if (Is_null ($nodeId) | | ($this->rootid=== $nodeId))
   {$nodeId = $this->rootid;
   } $toVisitNodeIds =array (); 
   $toVisitedNodeIds []= $nodeId; $this->visit ($this->nodes[$nodeId]); 
   Array_shift ($TOVISITEDNODEIDS);
   $toVisitedNodeIds =array_merge ($toVisitedNodeIds, $this->nodes[$nodeId]->childrenids);
    while (!empty ($toVisitedNodeIds)) {$toVisitNodeId =array_shift ($toVisitedNodeIds);
    $this->visit ($this->nodes[$toVisitNodeId]);
   $toVisitedNodeIds =array_merge ($toVisitedNodeIds, $this->nodes[$toVisitNodeId]->childrenids);
  } return $this;
  }//Depth-first alias public function D ($nodeId =null) {return $this->depthtraversall ($nodeId);
  }//Depth first traversal//and breadth first different implementations only depend on the order of the Array_merge (PHP array is easy to use) public function Depthtraversall ($nodeId =null) {
  if (Is_null ($this->rootid)) {die ("This tree is an empty tree, inaccessible");
   else{//All Traversal if (Is_null ($nodeId)) {$nodeId = $this->rootid;
   } $toVisitNodeIds =array (); 
   $toVisitedNodeIds []= $nodeId;
   $this->visit ($this->nodes[$nodeId]); 
   Array_shift ($TOVISITEDNODEIDS); $toVisitedNodeIds =array_merge ($this->nodes[$nodeId]->childrenids, $toVisitedNodeIds );
    while (!empty ($toVisitedNodeIds)) {$toVisitNodeId =array_shift ($toVisitedNodeIds);
    $this->visit ($this->nodes[$toVisitNodeId]);
   $toVisitedNodeIds =array_merge ($this->nodes[$toVisitNodeId]->childrenids, $toVisitedNodeIds);
  } return $this;
  ///access to a single node public function visit ($node) {if is_null ($this->uservisitfunction)) {return $node->id;
  else{return Call_user_func ($this->uservisitfunction, $node, $this);

 }}}?>

I hope this article will help you with your 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.