deep mann
deep mann

Reputation: 25

how to write recursion using loops

I want to write recursive code using loops instead of recursion function. because recursion take too much time to execute and even fails.This is counting user in both side of binary tree using recursion.i want to achieve this task using loops. please help me to achieve this task.thanks in advance.I will very thank full to you.

    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;

}

Upvotes: 0

Views: 31

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

I hope you can appreciate that this is difficult to test, so hopefully you can understand it enough to help.

One of the big performance issues in any system is file/database access and opening and closing connections etc. is always a slow process. This routine loads all the members in the start and passes the data around rather than continually using the database...

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

Upvotes: 1

Related Questions