1

二分木で父親のすべてのダウンラインを取得したい、各父親には左腕と右腕があり、各腕には左腕と右腕があります .次の画像のように. 私のデータベースにはusersというテーブルがあり、各ユーザーには父親のIDとLまたはRの位置があります。

これが私の機能です..しかし、それでもすべてのダウンラインを取得するわけではありません。 次の画像のように

4

1 に答える 1

1

私には2つのことが際立っています:

  1. $i引数と使用法$this->downline_id_arr

次のことを検討してください。

$children = array();
foreach($data as $row) {
    $child_id = $row->id;
    $children[$child_id] = array(/**/);
    $children = array_merge($children, $this->getAllDownline($child_id);
}
return $childen;

$iこれで、変数 orは必要なくなりました$this->downline_id_arr

  1. 各ノードを 1 つずつクエリしています。

代わりにレベルによるクエリを検討してください。

function getAllDownlines($fathers) {
    $data = "SELECT * FROM users WHERE father_id IN (/*fathers*/)";
    $new_father_ids = array();
    $children = array();
    foreach ($data as $child) {
        $children[$child->id] = array(/**/); // etc

        $new_father_ids[] = $child->id;
    }
    $children = array_merge($children, $this->getAllDownlines($new_father_ids);
    return $childen;
}

通常、クエリが少ないほど高速になるため、パフォーマンスが向上するはずです。

于 2017-06-01T10:20:15.577 に答える