Php unordered tree implementation method

Source: Internet
Author: User
This article mainly introduces the implementation method of the php unordered tree. The example analyzes the implementation skills of the php unordered tree and has some reference value, for more information about how to implement the php unordered tree, see the example in this article. Share it with you for your reference. The details are as follows:

Shows the running effect:

The php code is as follows:

<? Php/* unordered tree written in php */class unorderedTree {// Node id counter protected $ nodeId = 0; // The depth of the tree protected $ depth = 0; // number of nodes in the tree, protected $ nodesCount = 0; // tree degree @ todo: make it take effect public $ degree = "to be implent "; // root node id // because the tree has multiple operations starting from the root node, you do not want to traverse the tree to the top to find the root node. always point to the root node protected $ rootid = null with a variable; // subnode set. k-v is nodeid => (stdclass) node // Some tree implementations usually use the same class as the tree. here the node uses stdclass {data, parent, id, childrenIds}, because I think the node and tree should be two objects, and s Tdclass 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 node id // childrenIds: The child node id does not want to determine the hierarchy for each traversal tree. // Note: in the node, # only the set of its own data and its subnode id is saved #, the data of the child node is accessed by using the tree $ tree-> nodes [$ node-> childrenIds [a_child_id]/data: the data contained in the node, for example, the node name and other attribute data protected $ nodes = array (); // User-Defined Access Node protected $ userVisitFunction = null;/* group: basic function of the class * // @ todo: constructor p Ublic function _ construct () {}// @ todo: destroy the tree public function _ destruct () {unset ($ this-> nodes );} // ------------ obtain the data Class function --------------- // Obtain the depth of the tree. public function getTreeDepth () {return $ this-> depth;} // Obtain the number of nodes in the tree. public function getCount () {return $ this-> NodesCount;} // obtain the degree of the tree public function getDegree () {// @ todo: returns $ this-> degree;} // gets the public function getNod of the specified node. E ($ nodeId) {if (isset ($ this-> Nodes [$ nodeId]) {return $ this-> Nodes [$ nodeId];} else {return false ;}/// obtain the latest id public function getId () {return $ this-> nodeId;} // Obtain the public function getNodeHeight ($ nodeId) of the specified node height) {if (array_key_exists ($ nodeId, $ this-> nodes) {// The node is already in the tree and the height must be at least 1, every time a parent node + 1 $ height = 1 is found; // records the nodes already accessed in this tree, this function is used to prevent parent nodes from leading to an endless loop during node construction and end searching in time $ visitedNodesIds = array (); // records the id of the current operation node $ cid = $ nod EId; // the parent node of the current node must exist in this tree. // recursive while (isset ($ cid) {if (! In_array ($ cid, $ visitedNodesIds) {if ($ this-> rootid ===$ cid) {// to the top, return $ height ;} $ visitedNodesIds [] = $ cid; $ cid = $ this-> nodes [$ cid]-> parentId; $ height ++;} else {return false ;}} return false;} else {return false;} // Obtain the public function getRoot () {return (! Is_null ($ this-> rootid) & $ this-> nodes [$ this-> rootid];} // Obtain the array composed of the specified node and all its subnodes. // This is a key basic operation used to obtain 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); wh Ile (! 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: obtain the public function getSubTree ($ nodeid) {}// -------------- data update ----------------- public function setId ($ nodeId) {$ t His-> nodeId = $ nodeId; return $ this;} // create a non-repeated (unused in the tree) new id public function seekId () {$ this-> nodeId ++; return $ this-> nodeId;} public function setVisitFunction ($ userFunction) {$ this-> userVisitFunction = $ userFunction ;} // Insert a subnode. by default, the public function insertNode is inserted under the root node ($ parent_id = null, $ data = null) {// note that node is not a class tree $ node = new stdclass; $ node-> data = $ data; // increase in the number of nodes in the tree $ this-> nodeCount ++; // assign node id $ t His-> seekId (); $ node-> id = $ this-> getId (); // Insert the 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) {// insert it into an existing node of this tree if (is_null ($ parent_id )) {$ parent_id = $ this-> rootid;} $ node-> parentId = $ pa Required _id; $ node-> childrenIds = array (); // The maximum depth of the update 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_id, $ data);} // ------------- data access ----- // The width first traverses the node alias. The full name is too long. public function B ($ nodeId = null) {return $ this-> breadthTraversal ($ nodeId );} // the breadth-first traversal node public function breadthTraversal ($ nodeId = null) {if (is_null ($ this-> rootid) {die ("this tree is an empty tree, inaccessible ");} else {// traverse all 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 );} // different implementations of depth-first traversal // and breadth-first are different only in the order of 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 and cannot be accessed ");} else {// traverse all 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 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 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.