Php uses the pre-order traversal tree to implement infinite pole classification without recursion. recursive _ PHP Tutorial

Source: Internet
Author: User
Php uses the pre-order traversal tree to implement non-recursive Infinitus classification and recursion. Php uses the forward-order traversal tree to implement an infinite-pole classification without recursion. recursive examples in this article show that php uses the forward-order traversal tree to implement an infinitely-polar classification without recursion. For your reference, php uses the forward-order traversal tree to implement an infinite pole classification without recursion and recursion.

This example describes how php uses a forward-order traversal tree to implement Infinitely Polar classification without recursion. Share it with you for your reference. The details are as follows:

We usually use recursion to implement Infinitus classification. we all know that recursion is very inefficient. The following describes an improved forward-order traversal tree algorithm, which does not apply to recursion to implement Infinitus classification, it is more efficient to achieve tree-level structure with large data volumes.

The SQL code is as follows:

Create table if not exists 'Category '('id' int (11) not null AUTO_INCREMENT, 'title' varchar (50) not null, 'lft' int (11) not null, 'rgt 'int (11) not null, 'order' int (11) not null comment 'sorted', 'create _ time' int (11) not null, primary key ('id') ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 12; ---- 'Category '-- insert into 'category' ('id ', 'title', 'lft ', 'rgt', 'order', 'create _ time') VALUES (1, 'Top topic ', 1, 20, 1, 1261964806 ), (2, 'edited category', 16, 19, 50,126 4586212), (4, 'company product', 10, 15, 50,126 4586249), (5, 'Honorary qualifications ', 8, 9, 50,126 4586270), (6, 'data download', 6, 7, 50,126 4586295), (7, 'talent recruiters', 4, 5, 50,126 4586314), (8, 'guest', 2, 3, 50,126 4586884), (9, 'Presidents', 17, 18, 50,126 7771951), (10, 'New category subcategory', 11, 14, 0, 1400044841), (11, 'php point-to-http://www.phpddt.com ', 12, 13, 0, 1400044901 );

The php code is as follows:

<? Php/*** purely test ** @ author Mckee * @ link http://www.phpddt.com */class Category extends CI_Controller {public function _ construct () {parent: :__ construct (); $ this-> load-> database ();} public function view () {$ lists = $ this-> db-> order_by ('lft ', 'asc ') -> get ('Category ')-> result_array (); // the right value of the two adjacent records. the first right value is greater than the second value. this is his parent class. // we use an array to store the right value of the previous record, compare it with the right value of the record. if the former is smaller than the latter, it indicates that it is not a parent-child relationship. Then, use array_pop to pop up the array. Otherwise, the two loops are retained, no recursion $ parent = array (); $ arr_list = array (); foreach ($ lists as $ item) {if (count ($ parent )) {while (count ($ parent)-1> 0 & $ parent [count ($ parent)-1] ['rgt '] <$ item ['rgt']) {array_pop ($ parent) ;}$ item ['destpath'] = count ($ parent); $ parent [] = $ item; $ arr_list [] = $ item ;} // display the tree structure foreach ($ arr_list as $ a) {echo str_repeat ('--', $ a ['depath']). $ a ['title'].'
';}}/***** The insert operation simply finds its parent node, and adds 2 to the left and right values of the nodes whose left and right values are greater than the left values of the parent node, then insert the current node. the left and right values are the parent node's left values plus one and the plus two */public function add () {// get the parent class id $ parent_id = 10; $ parent_category = $ this-> db-> where ('id', $ parent_id)-> get ('Category ')-> row_array (); // 1. add 2 $ this-> db-> set ('lft ', 'lft + 2', FALSE) to the left and right values of a node greater than the left value of the parent node) -> where (array ('lft> '=> $ parent_category ['lft'])-> update ('Category '); $ this-> db-> set ('rgt ', 'rgt + 2', FALSE) -> where (array ('rgt> '=> $ parent_category ['lft'])-> update ('Category '); // 2. insert a new node $ this-> db-> insert ('category', array ('title' => 'subcategory' of the new category ', 'lft '=> $ parent_category ['lft'] + 1, 'rgt '=> $ parent_category ['lft'] + 2, 'order' => 0, 'create _ time' => time (); echo 'add success ';}/*** delete ** // 1. after the deleted node is deleted, the right value is subtracted from the left value and 1 is added. The value $ width = $ rgt-$ lft + 1 is obtained. * // 2. delete all nodes between left and right values * // 3. modify the condition to all nodes whose values are greater than the right of the current node and subtract the left and right values from $ width */public function delete () {// Obtain a category by category id $ id = 3; $ category = $ this-> db-> where ('id', $ id)-> get ('Category ') -> row_array (); // calculate $ width = $ category ['rgt ']-$ category ['lft'] + 1; // 1. delete this category $ this-> db-> delete ('Category ', array ('id' => $ id); // 2. delete all categories between left and right values $ this-> db-> delete ('Category ', array ('lft>' => $ category ['lft '], 'lft <'=> $ category ['rgt']); // 3. modify values of other nodes $ this-> db-> set ('lft ', "lft-{$ width}", FALSE) -> where (array ('lft> '=> $ category ['rgt'])-> update ('Category '); $ this-> db-> set ('rgt ', "rgt-{$ width}", FALSE) -> where (array ('rgt> '=> $ category ['rgt'])-> update ('Category '); echo 'delete success ';} // edit, public function edit () {// needless to say, use id to edit $ id = 2; $ this-> db-> update ('Category ', array ('title' => 'edited category'), array ('id' => $ id); echo 'edit success ';}}

I hope this article will help you with php programming.

Examples in this article describes how php uses the forward traversal tree to implement Infinitely Polar classification without recursion. Share it with you for your reference...

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.