Php uses the pre-order traversal tree to implement non-recursive infinitus classification and Recursion

Source: Internet
Author: User

Php uses the pre-order traversal tree to implement non-recursive infinitus classification 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']. '<br/>' ;}}/***** the insert operation is easy to find its parent node, add 2 to the left and right values of the node with the left and right values greater than the left values of the parent node, and then insert the node, the left and right values are the parent node's left values plus one and add 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.

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.