【问题标题】:Tree data structure in phpphp中的树形数据结构
【发布时间】:2010-05-26 07:21:04
【问题描述】:

在我的应用程序中,用户启动一棵新树或添加到子用户下并继续以这种方式在分支中添加用户-
>树类型结构有 10 级。
@ 987654322@root 节点包含 1 用户,每个节点(用户)可以有 ma​​x 5 child-user 这样树就会像 level 0 = 1 user ,
level 1 = 5 user,level 2 = 25 user ,
level 3 = 125 user 等等。

我创建了一个 MySQL 表,其列如下-

User_idlevelsuper_idchild1_idchild2_idchild3_idchild4_idchild5_id

我的问题是如何获取任何级别的特定用户的所有子用户(child to child also)我需要在表中添加更多列吗??

【问题讨论】:

    标签: php data-structures hierarchical-data


    【解决方案1】:

    您可能会觉得阅读我上周 PHP TEK-X 会议上的演讲会很有趣:

    Models for Hierarchical Data with SQL and PHP

    本演讲描述了 SQL 中的替代解决方案,包括:

    • 邻接列表
    • 路径枚举
    • 嵌套集
    • 闭包表

    另请参阅我对 Stack Overflow 问题的回答:What is the most efficient/elegant way to parse a flat table into a tree?

    【讨论】:

      【解决方案2】:

      你应该看看Nested Sets

      在 PHP 中,您可以使用Doctrine Nested Sets,这将使您的生活更轻松。

      【讨论】:

      • -1 表示解决方案不仅需要一个非常具体和复杂的框架,而且是一种仅限面向对象的方法,并且没有解释其工作原理。虽然这个工具包可能有工厂支持重建树而不是直接的 ORM(这真的是低效的) - 你没有说是这种情况。
      【解决方案3】:

      这是我用来构建二叉树数据结构及其相应操作的完整代码:

      <?php
      class Node
      {
       public $data;
       public $leftChild;
       public $rightChild;
      
       public function __construct($data)
        {
         $this->data=$data;
         $this->leftChild=null;
         $this->rightChild=null;
        }
       public function disp_data()
        {
         echo $this->data;
        }
      
      
      }//end class Node
      class BinaryTree
      {
       public $root;
       //public $s;
       public function __construct()
        {
         $this->root=null;
         //$this->s=file_get_contents('store');
      
        }
      //function to display the tree
        public function display()
        {
         $this->display_tree($this->root);
      
        }
        public function display_tree($local_root)
        {
      
         if($local_root==null) 
           return;
          $this->display_tree($local_root->leftChild);
          echo $local_root->data."<br/>";
          $this->display_tree($local_root->rightChild);
      
        } 
      // function to insert a new node
        public function insert($key)
         {
          $newnode=new Node($key);
            if($this->root==null)
              {
               $this->root=$newnode;
               return;
              }
            else
              {
               $parent=$this->root;
               $current=$this->root;
                 while(true)
                   {
                     $parent=$current;
                       //$this->find_order($key,$current->data);
                      if($key==($this->find_order($key,$current->data)))
                        {
                            $current=$current->leftChild;
                             if($current==null)
                               {
                                $parent->leftChild=$newnode;
                                return;
                               }//end if2
                        }//end if1 
                      else
                        {
                            $current=$current->rightChild;
                             if($current==null)
                               {
                                $parent->rightChild=$newnode;
                                return;  
                               } //end if1                       
                        } //end else
                   }//end while loop 
              }//end else
      
         } //end insert function
      
      //function to search a particular Node
       public function find($key)
        {
          $current=$this->root;
           while($current->data!=$key)
                {
                  if($key==$this->find_order($key,$current->data))
                    {
                      $current=$current->leftChild;
                    }
                  else
                    {
                      $current=$current->rightChild;
                    }
                  if($current==null)
                    return(null);
      
                }
               return($current->data); 
        }// end the function to search
       public function delete1($key)
        {
          $current=$this->root;
          $parent=$this->root;
      
          $isLeftChild=true;
           while($current->data!=$key)
                {
                 $parent=$current;
                 if($key==($this->find_order($key,$current->data)))
                   {
                    $current=$current->leftChild;
                    $isLeftChild=true;
                   }   
                 else
                   {
                    $current=$current->rightChild;
                    $isLeftChild=false;   
                   } 
                  if($current==null)
                    return(null);
                }//end while loop 
      
            echo "<br/><br/>Node to delete:".$current->data;
           //to delete a leaf node 
           if($current->leftChild==null&&$current->rightChild==null)
             {
                 if($current==$this->root)
                    $this->root=null;  
                else if($isLeftChild==true)
                 {
                  $parent->leftChild=null;
                 }  
               else
                 {
                  $parent->rightChild=null;
                 }
               return($current);       
             }//end if1
           //to delete a node having a leftChild 
         else if($current->rightChild==null)
             {
                if($current==$this->root)
                 $this->root=$current->leftChild;
                else if($isLeftChild==true)
                 {
                  $parent->leftChild=$current->leftChild;
                 }
                else
                 {
                  $parent->rightChild=$current->leftChild;
                 }   
                return($current);
             }//end else if1
          //to delete a node having a rightChild
         else if($current->leftChild==null)
             {
               if($current==$this->root)
                 $this->root=$current->rightChild;
               else if($isLeftChild==true)
                 {
                  $parent->leftChild=$current->rightChild;
                 }  
               else
                 {
                  $parent->rightChild=$current->rightChild; 
                 }  
                 return($current);
             }  
         //to delete a node having both childs
          else
             {
              $successor=$this->get_successor($current);
              if($current==$this->root)
                {
                  $this->root=$successor; 
      
                }
              else if($isLeftChild==true)
                {
                 $parent->leftChild=$successor;
                }
              else
                {
                 $parent->rightChild=$successor;
                }     
               $successor->leftChild=$current->leftChild;
              return($current);
             }   
      
      
        }//end the function to delete a node
      //Function to find the successor node
       public function get_successor($delNode)
        {
         $succParent=$delNode;
         $successor=$delNode;
         $temp=$delNode->rightChild;
          while($temp!=null)
               {
                $succParent=$successor;
                $successor=$temp;
                $temp=$temp->leftChild;
               }
         if($successor!=$delNode->rightChild)
           {
            $succParent->leftChild=$successor->rightChild;
            $successor->rightChild=$delNode->rightChild;
           }
        return($successor);
        }
      //function to find the order of two strings
       public function find_order($str1,$str2)
        {
           $str1=strtolower($str1);
           $str2=strtolower($str2);
           $i=0;
           $j=0;
      
           $p1=$str1[i];
           $p2=$str2[j]; 
        while(true)
         {  
             if(ord($p1)<ord($p2)||($p1==''&&$p2==''))
               {
      
                 return($str1);
               }
            else
               {
                 if(ord($p1)==ord($p2))
                   {
                    $p1=$str1[++$i];
                    $p2=$str2[++$j];
                    continue;
                   }
                return($str2); 
               }
         }//end while
      
        } //end function find string order
      
       public function is_empty()
        {
          if($this->root==null)
            return(true);
          else
            return(false);
        }
      }//end class BinaryTree
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-18
        • 2011-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 1970-01-01
        相关资源
        最近更新 更多