1
I would like to make a function in PHP that goes through the entire binary tree, so I made this code.
function getTree($id, $count, $conn) {
if ((!isset($count)) || ($count = null)) {
$count = 1;
}
$query = $conn->prepare("SELECT login FROM usuarios WHERE ID = :id");
$query->execute(array("id" => $id));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
if (count($rows) > 0) {
for ($i = 0; $i < $count; $i++) {
echo "-";
}
echo $rows[0]["login"]."<br />";
$query = $conn->prepare("SELECT id FROM dados WHERE upline = :id");
$query->execute(array("id" => $id));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
$nivel++;
getTree($row["id"], $count + 1, $conn);
}
}
}
He takes all the people in there, perfect. But I would like him to search as the image shows, but, looking at the image below, this function behind, 1, 2, 4, 8, 16, 17, 9, 18, 19, and so on.
The function would look for the second line (numbers 2 and 3) and based on id 1; Then it would return and travel bringing Id 4 and 5, related to 2, and 6 and 6 related to 3 and so on.
The logic is clear: the number unfolds into two, one being twice that number and the other the double plus one, and so on. Fucking is doing a function for that. :)
– Sam
You need to iterate on being in preorder (center, left tree sub, right tree sub)
– Jefferson Quesado
Could you give me an example of this?
– Diego Ananias