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
SortedList is not a good container to use in this context. It is O(n) for insertion operations (the repeated calls to Add()), as it is internally represented as a flat list. Using Dictionary instead of SortedList will be a large improvement, as it is O(1) amortized insertion time.
Put the nodes into a sorted list or dictionary.
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.