0

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;

}
4
  • How many member records do you have? Having to repeatedly query the database is always a thing to try and reduce. So if you can read all the members and work from memory it would probably improve performance. Commented Dec 22, 2018 at 18:06
  • i have 3000 users only sir.but this functionalty halt script to run more then 2 minutes Commented Dec 22, 2018 at 18:11
  • As a suggestion, try loading all of the members into an array indexed my the user_id column. Then use this array rather than use your SQL statements. See how this performs. Commented Dec 22, 2018 at 18:26
  • i dont understand please can u give me any example if u can.pls Commented Dec 22, 2018 at 18:35

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

highly appreciating man.u are awsome.mindblowing person.thanks alot nigel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.