如何使用循环编写递归

时间:2018-12-22 17:41:49

标签: php mysql

我想使用循环而不是递归函数来编写递归代码。因为递归要花费太多时间才能执行甚至失败。这是使用递归在二进制树的两边计算用户的数量。我想使用循环来实现此任务。请帮助我完成这项任务。在此先感谢您。

    function countActiveMembers($mid){
    if ($mid == null) {
        return 0;
    }
    $c = 0;

    include("conn.php");
    $query = $conn->prepare('SELECT leftm, rightm, package_choose FROM member WHERE user_id = ?');
    $query->bind_param('s', $mid);
    $query->execute();
    $query->bind_result($leftm, $rightm, $package_choosen);
    if ($query->fetch()) {
        $query->close();
        $conn->close();

        if ($package_choosen == 1) {
            $c = 0;
        } else {
            $c = 1;
        }

        if (isset($leftm) == false && isset($rightm) == false) {
            return $c;
        }


        if (isset($leftm)) {
            $c = $c + countActiveMembers($leftm);
        }
        if (isset($rightm)) {
            $c = $c + countActiveMembers($rightm);
        }
    } else {
        $query->close();
        $conn->close();
    }
    return $c;
}

    function countLeftActive($mid){
    if ($mid == null) {
        return 0;
    }
    $c = 0;
    include("conn.php");
    $query = $conn->prepare('SELECT leftm, rightm, package_choose FROM member WHERE user_id = ?');
    $query->bind_param('s', $mid);
    $query->execute();
    $query->bind_result($leftm, $rightm, $package_choosen);
    if ($query->fetch()) {
        $query->close();
        $conn->close();

        if (isset($leftm) == false && isset($rightm) == false) {
            return 0;
        }

        if (isset($leftm)) {
            $c = $c + countActiveMembers($leftm);
        }
    } else {
        $query->close();
        $conn->close();
    }
    return $c;
}
function countRightActive($mid){
    if ($mid == null) {
        return 0;
    }
    $c = 0;
    include("conn.php");
    $query = $conn->prepare('SELECT leftm, rightm, package_choose FROM member WHERE user_id = ?');
    $query->bind_param('s', $mid);
    $query->execute();
    $query->bind_result($leftm, $rightm, $package_choosen);
    if ($query->fetch()) {
        $query->close();
        $conn->close();
        if (isset($leftm) == false && isset($rightm) == false) {
            return 0;
        }

        if (isset($rightm)) {
            $c = $c + countActiveMembers($rightm);
        }
    } else {
        $query->close();
        $conn->close();
    }
    return $c;

}

1 个答案:

答案 0 :(得分:1)

我希望您能体会到这很难测试,因此希望您能理解它足以为您提供帮助。

任何系统中的主要性能问题之一是文件/数据库的访问以及打开和关闭连接等总是很慢的过程。此例程从一开始就加载所有成员并传递数据,而不是连续使用数据库...

function countActiveMembers( $members, $mid){
    if ($mid == null) {
        return 0;
    }
    $c = 0;

   // Fetch the data from the $members list, using $mid as the index
   $leftm = $members[$mid]['leftm'];
   $rightm = $members[$mid]['rightm'];
   $package_choosen = $members[$mid]['package_choose'];
   if ($package_choosen == 1) {
        $c = 0;
    } else {
        $c = 1;
    }

    if (isset($leftm) == false && isset($rightm) == false) {
        return $c;
    }

    if (isset($leftm)) {
        $c = $c + countActiveMembers($members, $leftm);
    }
    if (isset($rightm)) {
        $c = $c + countActiveMembers($members, $rightm);
    }
    return $c;
}

function countLeftActive($members, $mid){
    if ($mid == null) {
        return 0;
    }
    $c = 0;
    $leftm = $members[$mid]['leftm'];
    $rightm = $members[$mid]['rightm'];

        if (isset($leftm) == false && isset($rightm) == false) {
            return 0;
        }

        if (isset($leftm)) {
            $c = $c + countActiveMembers($members, $leftm);
        }
    return $c;
}
function countRightActive($members, $mid){
    if ($mid == null) {
        return 0;
    }
    $c = 0;
    $leftm = $members[$mid]['leftm'];
    $rightm = $members[$mid]['rightm'];
    if (isset($leftm) == false && isset($rightm) == false) {
            return 0;
        }

        if (isset($rightm)) {
            $c = $c + countActiveMembers($members, $rightm);
        }
    return $c;

}

// Use your own database credentials
$conn = mysqli_connect("172.17.0.3", "root","a177fgvTRw", "test" );
$result = $conn->query('SELECT user_id, leftm, rightm, package_choose 
         FROM member');
$members = [];
// Read all the members in and index them by the user_id
while ($row = $result->fetch_assoc()) {
    $members[$row["user_id"]] = $row;
}

// Not entirely sure how you use it,but this shows passing the members into the start function
echo countActiveMembers($members, 1);