Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

求通过部分树和结构获取完整树数据的算法

有这样一个需要,求算法
有一个树,初始时不完整,如下:

    let tree = [
        id: 1,
        children: [
            {
                id: 2,
                children: null
            },
            {
                id: 3,
                children: null
            }
        ]
    ]

然后这棵树其实还是下面几层,需要通过对应的id获取并插入到children字段。
现在有个结构如下:

    let construction = [
        {
            id: 1,
            children: [
                {
                    id: 2,
                     children: [
                        {
                            id: 6,
                            children: [
                                {
                                    id: 10,
                                    children: null
                                }
                            ]
                        }
                     ]
                }
            ]
        }
    ]

construction树每个children就一个元素,直到children为null就是最后一个节点。
现在通过construction去遍历树,通过id匹配,到第二层匹配到之后就要通过id异步获取树的children数据添加到tree数据中,并且继续此操作,直到construction中的children为null,结束遍历,拼接出通过construction结构的tree数据。
求一个算法,谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...