【问题标题】:binary tree child counting php mysql二叉树子计数php mysql
【发布时间】:2014-12-05 11:16:05
【问题描述】:

我遇到了一些情况,我必须计算二叉树的左子和右子我的数据库结构如下。

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;

如果您没有解决方案,请不要标记重复或任何其他内容

【问题讨论】:

    标签: php mysql tree binary-tree


    【解决方案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
    

    【讨论】:

    • 嗨 Ashok 感谢您的回答,但出了点问题!左右计数不起作用。
    • allcount 工作正常吗?因为所有其他功能都依赖于它。
    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多