簡體   English   中英

二進制樹子計數php mysql

[英]binary tree child counting php mysql

在某些情況下,我不得不數一棵二叉樹的左子節點和右子節點。我的數據庫結構如下。

SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'"

其中rid是=推薦ID,而pid =父ID,

例如,我需要計算給定父母ID的所有葉子

如果id 1有左2個和右3個直系孩子,則我需要知道左成員總數和右成員總數。

                                         1
                                       /   \
                                     2       3
                                    / \     / \
                                   4   5   6   7
                                 /      \       \
                               8          9      11
                              /                   \
                            10                      12
                           /  \
                         13    14

我需要計算1的所有孩子。我正在使用此功能,但它只計算最左邊的孩子,請對其進行修改或自己解釋

  function leftcount($id)   //Function to calculate leftcount
  {
    $sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    //var_dump($array);
    (array_count_values($array));
    if(!empty($array['l_mem']))
    {
      $count += leftcount($array['l_mem']);  
    } 


    $totalcount = 1 + $count;
    return $totalcount  ;

  }

        $left = leftcount($id);
        doing -1 because in function 1 + $count.
        $left = $left-1;

如果您沒有解決方案,請不要標記為重復或任何其他標記

您需要使用這3個函數來計算任何元素的左右和所有子元素。

function leftcount($id)   //Function to calculate all left children count
{
    $sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    (array_count_values($array));
    $count = 0;
    if(!empty($array['l_mem']))
    {
        $count += allcount($array['l_mem']) +1;
    }
    return $count;
}
function rightcount($id)   //Function to calculate all right children count
{
    $sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    (array_count_values($array));
    $count = 0;
    if(!empty($array['r_mem']))
    {
        $count += allcount($array['r_mem']) +1;
    }
    return $count;
}
function allcount($id)   //Function to calculate all children count
{
    $sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    (array_count_values($array));
    $count = 0;
    if(!empty($array['l_mem']))
    {
        $count += allcount($array['l_mem']) +1;
    }
    if(!empty($array['r_mem']))
    {
        $count += allcount($array['r_mem']) +1;
    }
    return $count;
}

如果將1傳遞給這些函數。 這些的答案如下

echo leftcount(1); // 8
echo rightcount(1); // 5
echo allcount(1); // 13

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM