如何使用循环编写递归

ma8fv8wu  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(267)

我想用循环而不是递归函数来编写递归代码。因为递归执行时间太长,甚至会失败。这是使用递归计算二叉树两边的用户数。我想使用循环来完成这个任务。请帮我完成这个任务。提前谢谢。我会非常感谢你的。

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;

}
wnrlj8wa

wnrlj8wa1#

我希望你能理解这是很难测试,所以希望你能理解它足够的帮助。
在任何系统中,一个大的性能问题是文件/数据库访问,打开和关闭连接等总是一个缓慢的过程。这个例程在开始时加载所有成员并传递数据,而不是继续使用数据库。。。

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);

相关问题