I have a set of tree objects with a depth somewhere in the 20s. Each of the nodes in this tree needs access to its tree's root.
A couple of solutions:
- Each node can store a reference to the root directly (wastes memory)
- I can compute the root at runtime by "going up" (wastes cycles)
I can use static fields (but this amounts to globals)
Can someone provide a design that doesn't use a global (in any variation) but is more efficient that #1 or #2 in both memory or cycles respectively?
Edit: Since I have a Set of Trees, I can't simply store it in a static since it'd be hard to differentiate between trees. (thanks maccullt)