<p>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.</p>

<p>A couple of solutions:</p>

<ol>
<li>Each node can store a reference to the root directly (wastes memory)

<ul>
<li>I can compute the root at runtime by "going up" (wastes cycles)</li>
<li><s>I can use static fields (but this amounts to globals)</s></li>
</ul></li>
</ol>

<p>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?</p>

<p><strong>Edit:</strong> 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)</p>

[{'answer_id': 68171, 'author': 'Niall', 'author_id': 6049, 'author_profile': 'https://Stackoverflow.com/users/6049', 'pm_score': 4, 'selected': True, 'text': "<p>Pass the root as a parameter to whichever functions in the node that need it.</p>\n\n<p>Edit: The options are really the following:</p>\n\n<ol>\n<li>Store the root reference in the node</li>\n<li>Don't store the root reference at all</li>\n<li>Store the root reference in a global</li>\n<li>Store the root reference on the stack (my suggestion, either visitor pattern or recursive)</li>\n</ol>\n\n<p>I think this all the possibilities, there is no option 5.</p>\n"}, {'answer_id': 68193, 'author': 'David J. Sokol', 'author_id': 1390, 'author_profile': 'https://Stackoverflow.com/users/1390', 'pm_score': 2, 'selected': False, 'text': '<p>Why would you need to do away with globals?  I understand the stigma of globals being bad and all, but sometimes just having a global data structure with all elements is the fastest solution.</p>\n\n<p>You make a trade-off:  code clarity and less future problems for performance.  That\'s the meaning being \'Don\'t optimize yet\'.  Since you\'re in the optimize stage, sometimes it\'s necessary to cut out some readability and good programming practices in favor of performance.  I mean, bitwise hacks aren\'t readable but they\'re fast.</p>\n\n<p>I\'m not sure how many tree objects you have, but i\'d personally go with option one.  Unless you\'re dealing with thousands+ of trees, the pointers really won\'t amount to much more then a few strings.  If memory really is a super-important issue, try both methods (they seem fairly simple to implement) and run it through a profiler.  Or use the excellent <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="nofollow noreferrer">Process Explorer</a>.</p>\n\n<p>Edit:  One of the apps I\'m working on has a node tree containing about 55K nodes.  We build the tree structure but also maintain an array for O(1) lookups.  Much better then the O(m*n) we were getting when using a recursive FindNodeByID method.</p>\n'}, {'answer_id': 68241, 'author': 'munificent', 'author_id': 9457, 'author_profile': 'https://Stackoverflow.com/users/9457', 'pm_score': 1, 'selected': False, 'text': '<p>Point #1 is a premature memory optimization. #2 is a premature performance optimization. Have you profiled your app to determine if memory or CPU bottlenecks are causing problems for you? If not, why sacrifice a more maintainable design for an "optimization" that doesn\'t help your users?</p>\n\n<p>I would strongly recommend you go with #2. Whenever you store something you could instead calculate, what you are doing is caching. There\'s a few times when caching is a good idea, but it\'s also a maintenance headache. (For example, what if you move a node from one tree to another by changing its parent but forget to also update the root field?) Don\'t cache if you don\'t have to.</p>\n'}, {'answer_id': 68248, 'author': 'Phil Wright', 'author_id': 6276, 'author_profile': 'https://Stackoverflow.com/users/6276', 'pm_score': 0, 'selected': False, 'text': '<p>You could derive a class from TreeView and then add a singleton static property. That way you are effectively adding a global field that references the single instance of the class but have the benefit of it being namespace scoped to that class.</p>\n'}, {'answer_id': 68270, 'author': 'Allain Lalonde', 'author_id': 2443, 'author_profile': 'https://Stackoverflow.com/users/2443', 'pm_score': 0, 'selected': False, 'text': "<p>Ignoring the distaste for inner classes, I could define a Tree class and define the nodes as Inner classes.  Each of the nodes would have access to its tree's state including its root.</p>\n\n<p>This might end up being the same as #1 depending on how Java relates the nodes to their parents.  (I'm not sure and I'll have to profile it)</p>\n"}, {'answer_id': 68277, 'author': 'Daniel James', 'author_id': 2434, 'author_profile': 'https://Stackoverflow.com/users/2434', 'pm_score': 2, 'selected': False, 'text': "<p>Passing the root as a paramter is generally best. If you're using some kind of iterator to navigate the tree, an alternative is to store a reference to root in that.</p>\n"}]
['https://Stackoverflow.com/questions/68156', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']