Binary tree - how to count all left and right side nested child.

Hello Friends,
It is very easy before you create a table in your database.
Example: Table name 'tree_class' and create class name.
  1. snc
  2. Prenticeclass
  3 chillclasslt
  4. Chillclassaright
This screenshot of the database.



You create a function that counts all the children on the left and right side like this.

$memid="95000";   // Your Parent id
function getTotalLeg($memid,$leg){ 
      global $conn;
     $sql="select child_class from tree_class where parent_id='".$memid."' and position='".$leg."'";
      $res=mysqli_query($conn,$sql);
      $row=mysqli_fetch_array($res);
      global $total;
       $total=$total+mysqli_num_rows($res);
         if($row['child_class']!=''){
           getTotalLeg ($row['child_class'],'L');
           getTotalLeg ($row['child_class'],'R');
          } 
          return $total;      
        }      

     $total=0; 
    $left=getTotalLeg($memid,"L");    
    echo $total.""; 
     $total=0;
    $right=getTotalLeg($memid,"R");  
    echo $total; 

Your result will come like this.


No comments:

Post a Comment

Python in AI: Getting Started with TensorFlow & PyTorch

  Artificial Intelligence (AI) is no longer just a buzzword. It powers recommendation systems, chatbots, self-driving cars, and more. At the...