C algorithm for generating hierarchy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Generating a hierarchy usually means turning a flat list of items with Id and ParentId fields into a tree. In .NET applications this comes up in menus, category trees, organization charts, and comment threads. The best algorithm is usually not recursive searching over the whole list, but a two-phase pass that builds lookup structures first and then links parents to children in O(n) time.
Start From a Flat Model
Suppose each record looks like this:
- '
Id: unique identifier' - '
ParentId: nullable identifier of the parent' - '
Name: display label'
A simple C# model is enough:
The goal is to transform a flat List<Node> into a set of roots whose Children collections contain the full hierarchy.
Use a Lookup, Not Repeated Scans
A common slow approach is: for each node, scan the whole list to find its children. That costs O(n^2) and becomes painful on large inputs.
A better approach is:
- build a dictionary from
Idto node - loop once through the nodes
- if
ParentIdexists, append the node to its parent’sChildren - otherwise, treat it as a root
That turns the linking phase into near-constant-time dictionary lookups per node.
This is usually the right baseline algorithm because it is clear, fast, and easy to test.
Displaying the Result
Once the tree exists, traversal is straightforward.
And a runnable example:
Handle Invalid Data Explicitly
Real data often contains problems:
- a node points to a missing parent
- the same id appears twice
- a cycle exists, such as
Abeing a descendant of itself
The dictionary approach already catches duplicate ids because ToDictionary fails. Missing parents should also fail fast unless your application explicitly wants to keep such nodes as extra roots.
Cycle detection is a separate concern. If your input might be corrupted, add a traversal step that tracks visited ids along the current path. That is worth doing when data comes from users or external imports.
When Ordering Matters
Tree construction and child ordering are separate decisions. If the UI expects alphabetical or numeric order, sort either before linking or after building each Children list.
Do not mix sorting logic into the core hierarchy algorithm unless ordering is part of the domain model. Keeping those concerns separate makes the code easier to maintain.
Common Pitfalls
- Building the hierarchy by repeatedly scanning the full list, which leads to
O(n^2)behavior. - Recursing before building a lookup and making child lookup unnecessarily expensive.
- Ignoring invalid input such as missing parents or duplicate ids.
- Forgetting that a hierarchy may have multiple roots, not just one.
- Assuming the input is acyclic when imported data can contain loops.
Summary
- The usual input is a flat list with
IdandParentId. - The practical algorithm is to build an
Idlookup and link children in one pass. - That approach runs in roughly
O(n)time for tree construction. - Validate duplicates, missing parents, and possible cycles explicitly.
- Treat sorting and rendering as separate concerns from hierarchy generation.

