dslk6326846 2017-11-06 11:22
浏览 29

邻接列表排序算法

I have this kind of adjacency list:

Binary tree

I need to sort it to this: 2, 7, 9, 10, 8, 16, 17, 11

Basiccaly I have table where I have stored posts with parent ids. I can have unlimited number of levels.

I need just algorithm or full code in PHP.

I tried to use variations of row/column prefixes and some recurrent functions.

  • 写回答

1条回答 默认 最新

  • doudi1750 2017-11-06 13:29
    关注

    Okay, i finally did it. I was idi*t.

    public function makeTree($parent_id = 0, &$array = array())
    {
       // Get results where parent_id == $parent_id
      $recepts = $this->recept->get()->where("parent_id =" . $parent_id);
       // Foreach through them
       foreach ($recepts as $recept) {
         // Add this to array
         array_push($array, $recept);
         // Repeat
         $this->makeTree($recept["id"], $array);
       }
       return $array;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型