Select a result to preview
Trees represent hierarchical data with parent-child relationships. In .NET, tree-like behavior commonly appears through SortedSet<T> (red-black tree), SortedDictionary<TKey, TValue>, custom recursive node models, and expression trees in the compiler pipeline. A concrete use case: an autocomplete service maintains a trie (prefix tree) of 500K product names; lookup for any prefix completes in O(k) where k is prefix length, regardless of dataset size — a Dictionary scan would be O(n).
A tree organizes nodes so each node has at most one parent (except the root) and any number of children. Balanced trees keep height at O(log n), making insert, delete, and search O(log n). Unbalanced trees degrade toward O(n) — inserting already-sorted values into a naïve BST produces a linked list.
Traversal orders define processing sequence:
SortedSet<T> enumeration.Queue<T> to visit nodes level by level, natural for shortest-path in unweighted trees.SortedSet<T> in .NET uses a red-black tree internally, guaranteeing O(log n) insert and lookup regardless of insertion order.
graph TD
R[root]
R --> L[left child]
R --> M[right child]
L --> LL[left left]
L --> LR[left right]
M --> RL[right left]
M --> RR[right right]var ids = new SortedSet<int> { 5, 1, 3, 3 };
// Stored sorted and unique: 1, 3, 5
Stack<T> for unknown-depth trees.SortedSet<T> which guarantees O(log n) regardless of insertion order.SortedSet<T> gives sorted uniqueness with O(log n) operations.
SortedSet<T> (and SortedDictionary<TKey, TValue> for key-value scenarios).
On unknown/deep depth, where iterative traversal with an explicit stack is safer.
Parent
02 Computer Science
Pages