Most efficient way of creating tree from adjacency list

后端 未结 2 1762
说谎
说谎 2020-12-28 23:49

I have an adjacency list of objects (rows loaded from SQL database with the key and it\'s parent key) that I need to use to build an unordered tree. It\'s guaranteed to not

2条回答
  •  太阳男子
    2020-12-29 00:17

    1. Put the nodes into a sorted list or dictionary.

    2. Scan that list, pick up each node, find its parent node in the same list (binary search or dictionary lookup), add it to the Children collection of the parent node.

    There's no need for a Stack to put this into a tree.

提交回复
热议问题