phpは,再帰を必要としない無制限極分類を前順遍歴ツリーにより実現する

5148 ワード

本明細書の例では、phpが、再帰を必要としない無制限極分類を、プリアンブルループツリーを介して実現することを説明する.皆さんの参考にしてください.具体的には以下の通りです.
一般的に再帰実装無制限極分類を用いることで再帰効率が低いことが知られているが,再帰実装無制限極分類を適用せず,ビッグデータ量でツリー階層構造を実現する際に効率が高い改良された前順遍歴ツリーアルゴリズムを紹介する.
sqlコードは以下の通りです.

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 '  ',
 `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, '    ', 1, 20, 1, 1261964806),
(2, '      ', 16, 19, 50, 1264586212),
(4, '    ', 10, 15, 50, 1264586249),
(5, '    ', 8, 9, 50, 1264586270),
(6, '    ', 6, 7, 50, 1264586295),
(7, '    ', 4, 5, 50, 1264586314),
(8, '   ', 2, 3, 50, 1264586884),
(9, '  ', 17, 18, 50, 1267771951),
(10, '        ', 11, 14, 0, 1400044841),
(11, 'PHP   -http://www.phpddt.com', 12, 13, 0, 1400044901);


phpコードは次のとおりです.

load->database();
  }
  public function view()
  {
    $lists = $this->db->order_by('lft', 'asc')->get('category')->result_array();
    //                              
    //                  ,             ,        ,        ,  array_pop    ,     
    //      ,    
    $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['depath'] = count($parent);
      $parent[] = $item;
      $arr_list[]= $item;
    }
    //      
    foreach($arr_list as $a)
    {
      echo str_repeat('--', $a['depath']) . $a['title'] . '
'; } } /** * * , 2, , */ public function add() { // id $parent_id = 10; $parent_category = $this->db->where('id', $parent_id)->get('category')->row_array(); //1. 2 $this->db->set('lft', 'lft + 2', FALSE)->where(array('lft >' => $parent_category['lft']))->update('category'); $this->db->set('rgt', 'rgt + 2', FALSE)->where(array('rgt >' => $parent_category['lft']))->update('category'); //2. $this->db->insert('category', array( 'title' => ' ', 'lft' => $parent_category['lft'] + 1, 'rgt' => $parent_category['lft'] + 2, 'order' => 0, 'create_time' => time() )); echo 'add success'; } /** * * * //1. , 1, $width = $rgt - $lft + 1; * //2. * //3. , $width */ public function delete() { // id $id = 3; $category = $this->db->where('id', $id)->get('category')->row_array(); // $width $width = $category['rgt'] - $category['lft'] + 1; //1. $this->db->delete('category', array('id' => $id)); //2. $this->db->delete('category', array('lft >' => $category['lft'], 'lft $category['rgt'])); //3. $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'; } // , public function edit() { // , id $id = 2; $this->db->update('category', array( 'title' => ' ' ), array( 'id' => $id )); echo 'edit success'; } }

本稿で述べたphpプログラム設計に役立つことを願っています.